perf: compute accuracy and reconstructed on first access#113
Conversation
tsam v4 defers metric computation until result.accuracy is read, but the wrapper materialized both fields eagerly during result conversion, paying the cost even for callers that never look at them. Store a factory per field and let cached_property populate it on first access; repr reports the metrics only once computed instead of forcing them. On tsam v3 the metrics are computed inside tsam.aggregate() either way, so this defers only the xarray conversion there. 730d x 256 cols, Distribution(scope=global) + extremes, metrics unread: tsam develop-v4 2.59s -> 1.83s (-29%); tsam 3.4.2 unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
BenchmarksΔ% vs base, one table sorted by biggest change first Full tablebenchmarks/test_bench_aggregate.py
|
First of the three perf drafts (this one is the wrapper-side lever unlocked by tsam v4's lazy metrics).
What
AggregationResult.accuracyand.reconstructedare now populated on first access viacached_propertyover a stored per-field factory, instead of being materialized during result conversion. All four construction paths (single, slice-concat, slice view,clustering.apply) defer consistently — for the sliced case even the per-slice materialization and concat are deferred.repr()no longer forces the computation (showsaccuracy=<not computed>until first read).Why / measured impact
tsam v4 computes metrics lazily, but the wrapper immediately materialized them — paying v4's deferred cost on every
aggregate()call even when nobody reads the metrics. Measured at 730 days × 256 columns,Distribution(scope="global")+ extremes replace, metrics unread:tsam.aggregate()regardless)Reading
result.accuracy/.reconstructed(orresiduals/compare()/to_dataframe(), which touch them) computes once and caches — behavior and values are unchanged, verified by the full suite plus new laziness tests (test/test_lazy_result.py).Notes
cached_property;AggregationResultconstruction is internal-only, so no public API changes.test_wrapper_result_conversion, not e2e).🤖 Generated with Claude Code