Repository navigation
Critical fix
Sensor emission batching to 1Hz — All 8 sensor callbacks (power, HR, cadence, speed, altitude, grade, distance, GPS) previously called emitState() independently, creating ~8 emissions/second. Since each emission carried a fresh timestamp, StateFlow never deduplicated — all downstream engines fired on every callback.
This caused:
- ~8x overcounting of energy (kJ), Normalized Power, TSS, and match burn durations — MetricsEngine and MatchBurnEngine assumed 1 call = 1 second
- ~8x CPU overhead — PacingCalculator ran 20 Newton iterations with trig per call, all 6 engines processed redundantly
Now a single 1Hz timer emits state, and a sensorUpdatedSinceLastEmit guard prevents stale emissions during ride pause (which would inflate kJ/NP/TSS with phantom power data).
Performance improvements
-
TacticalAnalyzer caching —
getPrimaryInsight()was called 14×/sec (once per data field per second), each time rebuilding all insights with list allocations and sorting. Now cached with 1% progress threshold — recomputes only when the rider moves meaningfully along the climb. -
MetricsEngine running NP sum — Replaced
npWindow.sum()(O(30) per tick) with a running sum variable (O(1)). -
PacingCalculator physics cache —
calculateTargetPower()runsspeedFromPower()(Newton iterations with atan/sin/cos/exp) every tick. Now cached when grade (±0.1%), altitude (±5m), FTP, mode, and athlete profile haven't changed. On a steady 7% climb, physics runs once on entry instead of 3600×/hour. -
PowerZonesGlance composable reduction — Zone bar segments reduced from 20 to 10 in
ZonesLarge,ZoneBar, andProgressBar. 140 → 70 Box composables in the largest layout. Visual difference negligible at Karoo display sizes. -
ClimbDetector ArrayDeque — Replaced
mutableListOfwithArrayDequefor O(1)removeFirst()instead of O(n)removeAt(0). ReplacedtakeLast(N).average()(temporary list allocation) with index-based iteration. -
PhysicsUtils solver — Newton iterations reduced from 20 to 10, convergence tolerance from 0.1W to 1.0W. 1W precision is more than sufficient for pacing advice.
Minor improvements
- Cached
activeClimb.valueread in extension's main data flow (1 StateFlow read instead of 2 per tick) - Removed unnecessary
@Serializableannotations from 7 transient model classes (LiveClimbState, WPrimeState, ClimbStats, RideMetrics, PacingTarget, MatchBurnState/Match, NextClimbInfo) — none are ever serialized - Defensive
emitJob?.cancel()at start ofstartStreaming()prevents duplicate timers on Karoo reconnect edge case
Behavioral note
MatchBurnEngine's belowCpCounter >= 5 threshold now means 5 real seconds below CP (previously ~0.6s at 8Hz). This is an improvement: brief power dips during intervals no longer prematurely end a match, matching industry-standard match burn detection behavior.