Skip to content

Grid: per-frame work no longer scales with the whole sheet (#1098) - #1136

Merged
aiongg merged 1 commit into
storytold:mainfrom
vangel76:perf/ui-per-frame-work-1098
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
vangel76:perf/ui-per-frame-work-1098

Conversation

@vangel76

Copy link
Copy Markdown
Contributor

Part of #1098. Fixes #1053 (nobody had claimed it or opened a PR for it).

What was O(sheet) per frame

paint in crates/ui-egui/src/grid.rs makes a fresh CfCache::new() every frame (so nothing survives from frame to frame). In CfCache::look (crates/engine/src/cf.rs), for every visible cell and every conditional-format rule that covers it:

  1. the rule's statistics (numbers_in) were built on first use, for every rule kind, even ones that read none (expression, cell value, text, blanks, errors, dates). That is a pass over the rule's range clipped to the used range, with a display().to_lowercase() string per non-empty cell and a sort. In the issue's workbook the alternate-row rule covers the whole data area (~8,276 x 44 cells), so every frame did that scan, and trimming the range to the data area (as the reporter tried) cannot help;
  2. the statistics were then .clone()d for every cell lookup: the sorted number vector and the whole text-count map (fix(cf): each cell lookup clones the full conditional-format statistics cache #1053).

So the per-frame cost was O(rule range) once per rule plus O(rule range) per visible cell, instead of O(visible cells).

What changed (engine only, crates/engine/src/cf.rs)

  • stats_need(rule): statistics are gathered only for rules that read them: numbers/mean/deviation for top-N, above average, colour scales, data bars, icon sets; the text counts for duplicate/unique. Every other rule kind scans nothing. Each rule gathers only the part it reads (previously both parts, always).
  • The statistics are borrowed from the cache (&RuleStats) instead of cloned per cell.
  • Behaviour is unchanged: the same code computes the same numbers for the rules that read them.

Tests (cargo test -p gridcraft-engine --lib cf::, 6 pass)

  • needed_statistics_equal_the_full_ones: equality, old vs new: a reference copy of the previous one-pass computation vs RuleStats::build for each need, on several ranges (partly empty, text, errors, blank-looking text, outside the used range); numbers and counts compared exactly (floats by bits).
  • rules_that_read_statistics_still_match: duplicate (case-insensitive), top-N and colour scale results through look.
  • lookups_do_not_scan_the_range_for_rules_without_statistics: counter (cfg(test) thread-local of cells visited by numbers_in) on a 10,000-row sheet: 100 lookups with expression, cell-value and no-blanks rules over A1:B10000 visit 0 cells (before: 20,000 per fresh cache); with a top-N rule they visit its 10,000 cells once, not per lookup.

Commands run

cargo fmt -p gridcraft-engine, cargo clippy -p gridcraft-engine --all-targets -- -D warnings, cargo test -p gridcraft-engine --lib cf:: (plus icon), cargo check -p gridcraft-engine --target wasm32-unknown-unknown, cargo xtask layers. Not run: cargo xtask ci, the ui-egui tests (no ui-egui code changed), a run of the app on a large workbook.

NOT fixed (findings, by reading the code; no timings were taken)

  • Rules that do read statistics (colour scale, data bar, icon set, top/bottom N, above average, duplicate/unique) still scan their whole range once per frame, because the cache is rebuilt each frame. Fixing that needs a cache that outlives a frame and a reliable invalidation key (DocState::revision only counts undoable commands; recalculation and Arc::make_mut in place don't change the Arc pointer), so it was left out.
  • Charts: chartview::paint_objects calls gridcraft_chart::resolve and render for every chart intersecting the viewport on every frame, with no cache, so a chart with thousands of points is O(points) per frame (relevant to the chart-heavy workbook if charts are in view; not to a chart-free scroll).
  • Geo::new (several times a frame) builds LineIndex and outline Axis over all custom row/column sizes, and both scrollbars call Sheet::used_range (walks all stored rows). All are O(custom lines / stored rows) per frame; small next to the above, not changed.
  • fix(ui): frozen panes enumerate offscreen frozen cells before clipping #1034 (frozen panes enumerate offscreen frozen cells) is a different path (the frozen/frozen quadrant); the reporter has only one frozen row, so it does not apply to them. Untouched; no claim or PR existed.

Unverified

No timing or profile was taken, so this does not claim the issue is resolved. Whether the conditional-format work was the dominant cost in the reporter's workbook is an inference from the code (a whole-area rule re-scanned every frame); a run with their file or a similar synthetic one would confirm it.

🤖 Generated with Claude Code

…ed not cloned per cell (storytold#1098)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@aiongg
aiongg merged commit e51bc1a into storytold:main Oct 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(cf): each cell lookup clones the full conditional-format statistics cache

2 participants