Repository navigation
Conversation
Painting asks `CfCache::look` for one cell's conditional look at a time, and the per-rule statistics (the sorted numbers and the duplicate counts) were cloned out of the cache on every lookup, so each lookup cost the size of the rule's range rather than a constant. A workbook with three rules over a 22k-cell range spent ~170 ms a frame, about 6 FPS; it now paints in ~9.8 ms. Holding the statistics behind an `Arc` makes the lookup a pointer clone. No behaviour change: the offscreen render of the workbook is byte-identical before and after. `cf_lookup_does_not_cost_the_size_of_the_rule_range` asserts the cost of 500 lookups does not grow with the rule's range (20x the rows was 15x the time before the fix, 1x after). It is `#[ignore]`d like the other timing tests; `cf_lookup_applies_the_rule_inside_its_range_only` checks the values behind it on every run. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Verified against
Correct, minimal, and well-measured. The honest "not addressed here" section (per-frame cache reconstruction, and the 3x duplicate-stats-per-shared-range opportunity) are real follow-ups but don't block this fix, which stands on its own. Good to merge. |
Contributor
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.
The problem
CfCache::lookis called once per cell while the grid paints. The per-rule statistics —sorted: Vec<f64>of every number in the rule's range, andcounts: HashMap<String, usize>of every distinct display value — are computed once and cached correctly, then cloned out of the cache on every lookup:So each lookup costs the size of the rule's range rather than a constant, and the cache is defeated by its own return value.
What it cost
Found while opening a trial-balance workbook: 2 sheets, ~80k cells, 947 rows — three
cellIsrules overJ11:BD413(~22.5k cells), plus 19 more rules on the second sheet. Measured with the offscreensnapshotharness on an M5, release build:--sample sales~170 ms a frame is about 6 FPS — the grid visibly lagged on any scroll or repaint.
sample(1)put 12,441 of 12,528 samples ongrid::show→paint_quadrant→grid::look→cf::CfCache::look, dominated byString::clone, hashbrown table construction andmemmove/malloc/free. Instrumenting the rule showed the cloned statistics held 18,894 numbers and 8,215 distinct strings, copied per painted cell per rule per frame.The fix
Hold the statistics behind an
Arcso the existing.clone()is a pointer clone. Deref coercion covers every use (st.sorted,st.counts,&stintothreshold), so the change is three lines.No behaviour change: the
snapshotrender of the workbook is byte-identical before and after (sha256 9f3017833b2510d7a88b3798…).Tests
cf_lookup_does_not_cost_the_size_of_the_rule_range— 500 lookups against a rule over 1k cells vs 20k cells; the cost must not grow with the range. Before the fix: 11.2 ms vs 171.0 ms (15x), fails. After: equal, passes.#[ignore]d with#[allow(clippy::disallowed_methods)], matchingrecalc_with_range_formulas_scales_linearly.cf_lookup_applies_the_rule_inside_its_range_only— the correctness behind it, run every time.cargo xtask cipasses: fmt, clippy, test, assets, layers, wasm.Not addressed here
grid::showconstructsCfCache::new()per frame, so the statistics never amortise across frames —numbers_inre-scans the range 3x per frame at ~2.55 ms each, which is ~7.7 ms of the remaining ~9.8 ms. Hoisting the cache into app state needs invalidation on edit, so I left it out of this change. Relatedly, statistics are keyed by rule index while all three rules here share one range and compute identical statistics; keying by range would cut that work 3x→1x. Happy to follow up on either.🤖 Generated with Claude Code