Faster filterDateRange via binary search#31
Open
ps2 wants to merge 1 commit into
Open
Conversation
SampleValue.swift: add a filterDateRange overload for RandomAccessCollection where Element: TimelineValue, Index == Int. Returns the same result as the existing Sequence-based linear-filter implementation but uses two binary searches instead of a linear scan. Picks up automatically for Array-backed callers (which is every caller in this codebase via Swift protocol dispatch). Significant speedup for hot paths that call filterDateRange repeatedly on long schedules — for example, InsulinMath.glucoseEffectsMidAbsorptionISF and DoseMath.insulinCorrection when the sensitivity schedule has many segments. In a LoopEval 60-day per-step prediction sweep with a per-step ISF schedule, total sim wall-clock went from ~30 min to ~1 min (≈30× faster) with bit-identical output to the linear-filter path. Tests: FilterDateRangeTests.swift with 11 cases covering equivalence with the linear-filter reference: boundary cases (empty, both bounds nil, only start, only end), start-before-all, end-after-all, fully- outside, single-sample collections, exact-match-one-segment, and a 100-iteration randomized fuzz over a 200-element contiguous schedule.
dad0b81 to
31c533a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a binary-search
filterDateRangeoverload onRandomAccessCollection where Element: TimelineValue, Index == Int. Returns the same result as the existing Sequence-based linear-filter implementation but uses two binary searches instead of a linear scan. Picks up automatically for Array-backed callers (which is every caller in this codebase) via Swift protocol dispatch.Why
In LoopEval workloads that call
filterDateRangerepeatedly on long schedules — e.g.,InsulinMath.glucoseEffectsMidAbsorptionISFandDoseMath.insulinCorrectionwhen the sensitivity schedule has many segments — the linear filter was the dominant cost. A 60-day per-step prediction sweep with a per-step ISF schedule went from ~30 min to ~1 min (≈30× faster) with bit-identical output to the linear-filter path.Test plan
xcrun swift test)FilterDateRangeTests.swift(11 tests):empty input, both bounds nil, only-start, only-end, both-in-middle,
start-before-all, end-after-all, fully-outside, before-all, exact-match-one-segment