Skip to content

Commit 3bfe0aa

Browse files
committed
Optimize the enrichWithTrends function in MeasurementFacade.kt by replacing the $O(n^2)$ filter operation with a pre-computed map to group differences by measurement ID.
1 parent ae224ea commit 3bfe0aa

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

android_app/app/src/main/java/com/health/openscale/core/facade/MeasurementFacade.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ class MeasurementFacade @Inject constructor(
9999
val differenceValues = enricher.enrichWithDifferences(measurements, types)
100100
val projectedValues = enricher.enrichWithProjection(measurements, types)
101101

102+
val differencesByMeasurementId = differenceValues.groupBy {
103+
it.currentValue.value.measurementId
104+
}
105+
102106
measurements.mapIndexed { index, current ->
103-
val trendsForCurrent = differenceValues.filter {
104-
it.currentValue.value.measurementId == current.measurement.id
105-
}
107+
val trendsForCurrent = differencesByMeasurementId[current.measurement.id] ?: emptyList()
106108
val projectedForCurrent = if (index == 0) projectedValues else emptyList()
107109
EnrichedMeasurement(
108110
measurementWithValues = current,

0 commit comments

Comments
 (0)