Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

perf(watch): heart-rate readings inserted in order instead of scan + sort

Duarte Santosadd5ddff7edc
diff --git a/frontend/ios/App/Watch/WorkoutManager.swift b/frontend/ios/App/Watch/WorkoutManager.swift
index 9e974389c..741b99bc0 100644
--- a/frontend/ios/App/Watch/WorkoutManager.swift
+++ b/frontend/ios/App/Watch/WorkoutManager.swift
@@ -214,8 +214,16 @@ final class WorkoutManager: NSObject, ObservableObject, HKWorkoutSessionDelegate
private var shownAt = Date.distantPast
private func add(_ readings: [(Date, Double)]) {
- for r in readings where !samples.contains(where: { abs($0.0.timeIntervalSince(r.0)) < 0.5 }) { samples.append(r) }
- samples.sort { $0.0 < $1.0 }
+ // Kept in time order by inserting each reading where it belongs: a duplicate (the same reading
+ // from the query and from the builder) can then only be the neighbour on either side, instead
+ // of a scan of the whole workout's readings and a full sort for every new one.
+ for r in readings {
+ var lo = 0, hi = samples.count
+ while lo < hi { let mid = (lo + hi) / 2; if samples[mid].0 < r.0 { lo = mid + 1 } else { hi = mid } }
+ if lo < samples.count && abs(samples[lo].0.timeIntervalSince(r.0)) < 0.5 { continue }
+ if lo > 0 && abs(samples[lo - 1].0.timeIntervalSince(r.0)) < 0.5 { continue }
+ samples.insert(r, at: lo)
+ }
guard let last = samples.last?.1 else { return }
let now = Date()
if heartRate == nil || abs((heartRate ?? 0) - last) >= 5 || now.timeIntervalSince(shownAt) >= 4 {