From 56932f7f2546839227440c58bb8beb0d91566a8a Mon Sep 17 00:00:00 2001 From: Davide Scaini Date: Sat, 23 May 2026 21:05:00 +0200 Subject: [PATCH] perf: add patch_index flag to recalculate_elevation_hysteresis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows bulk callers to skip per-activity index.json rewrites and batch the update themselves, reducing O(n²) index churn to O(n). --- bincio/extract/dem.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/bincio/extract/dem.py b/bincio/extract/dem.py index 4f9eeb5..7ebacbe 100644 --- a/bincio/extract/dem.py +++ b/bincio/extract/dem.py @@ -299,7 +299,9 @@ def recalculate_elevation( } -def recalculate_elevation_hysteresis(user_dir: Path, activity_id: str) -> dict: +def recalculate_elevation_hysteresis( + user_dir: Path, activity_id: str, *, patch_index: bool = True +) -> dict: """Recompute elevation gain/loss from the original recorded elevation data. Algorithm @@ -370,17 +372,18 @@ def recalculate_elevation_hysteresis(user_dir: Path, activity_id: str) -> dict: detail["elevation_loss_m"] = loss_r json_path.write_text(json.dumps(detail, indent=2, ensure_ascii=False), encoding="utf-8") - # Patch index.json summary - index_path = user_dir / "index.json" - if index_path.exists(): - index = json.loads(index_path.read_text(encoding="utf-8")) - for s in index.get("activities", []): - if s.get("id") == activity_id: - s["elevation_gain_m"] = gain_r - break - index_path.write_text( - json.dumps(index, indent=2, ensure_ascii=False), encoding="utf-8" - ) + # Patch index.json summary (skip for bulk callers who batch this themselves) + if patch_index: + index_path = user_dir / "index.json" + if index_path.exists(): + index = json.loads(index_path.read_text(encoding="utf-8")) + for s in index.get("activities", []): + if s.get("id") == activity_id: + s["elevation_gain_m"] = gain_r + break + index_path.write_text( + json.dumps(index, indent=2, ensure_ascii=False), encoding="utf-8" + ) return { "elevation_gain_m": gain_r,