Make AnalyzedSeries change points lazy properties - #169
Open
MrlixiangWE wants to merge 2 commits into
Open
Conversation
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.
Constructing an
AnalyzedSeriesruns change point detection for every metric up front, even when the caller never reads the results. #79 asks forchange_pointsandchange_points_by_timeto become properties instead.change_points,weak_change_points,change_points_by_timeandchange_points_timestampare now read-only properties backed by private fields. The first access computes everything once and caches it;append()andfrom_json()write the backing fields directly. Two behavior notes:change_points_timestampnow records when the change points were actually computed rather than when the object was constructed. A timestamp stored in JSON still takes precedence on deserialization.change_pointsargument used to leaveweak_change_pointsunset, so reading it raisedAttributeErrorunlessfrom_jsonpatched it afterwards. It now defaults to an empty collection.Cost measured before (master, cd5bc1e) and after (this branch) with the same script on the same machine (Linux, Xeon @ 2.8GHz, 8 GB RAM, Python 3.11), seeded random data. Every number is the median of 11 runs and is copied verbatim from the millisecond output of the script below; six decimals in milliseconds is exact nanosecond resolution, so nothing is rounded away, and the script prints the raw nanosecond medians alongside:
The cost moves from the constructor to the first read; the total for callers that do read change points stays in the same range, and repeated reads stay cached. Detection results are unchanged: no expected value in the test suite needed to change. The only adjusted test is
test_validate, which used to fake an uncomputed state by assigningchange_points = Noneand now constructs with an explicitly empty collection.Benchmark script
Verification:
pytest tests perf(non-container): 194 passed, including three new regression tests for laziness/caching,append()on a freshly constructed instance, and deserialization without recomputationflake8,ruff check,ruff format --check,isort --check-only: cleangit diff --check: cleanCloses #79