Skip to content

perf(ui-egui): cache PageView body ranges per DocLayout in CanvasState (#8194) - #8222

Merged
benandez merged 1 commit into
storytold:mainfrom
batistalucasv:perf/8194-resize-ribbon
Dec 2, 2026
Merged

benandez merged 1 commit into
storytold:mainfrom
batistalucasv:perf/8194-resize-ribbon

Conversation

@batistalucasv

@batistalucasv batistalucasv commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Caches the Vec<(f64, f64)> page body Y-ranges (bodies) passed to PageView::bodies in CanvasState::page_bodies, keyed by Weak<DocLayout> (Arc::ptr_eq).

Motivation

Part of #8194 (Phase 4 of #8112). canvas::show_pages previously re-allocated and rebuilt the full per-page bodies vector on every frame (including resize, scroll, and ribbon tab switches), even when the underlying DocLayout had not changed.

Testing

  • cargo fmt --all -- --check
  • cargo clippy -p wordcraft-ui-egui --all-targets -- -D warnings
  • cargo nextest run -p wordcraft-ui-egui --features craft/ui-testing --lib canvas::tests::cached_page_bodies_reuses_buffer_for_same_layout_and_rebuilds_on_change
  • cargo xtask layers

Summary by CodeRabbit

  • Performance
    • Reuses page layout bounds when the document layout is unchanged, reducing repeated work while displaying pages.
  • Tests
    • Added coverage to verify cached bounds are reused for the same layout and refreshed when the layout changes.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

CanvasState now caches page-body vertical bounds for a DocLayout and reuses them while that layout remains alive and pointer-identical. show_pages uses the cached bounds. A test checks reuse for the same layout and cache replacement after a layout change.

Changes: Canvas page-body bounds cache

Layer / File(s) Summary
Cache page-body bounds
crates/ui-egui/src/canvas.rs, crates/ui-egui/src/canvas/tests.rs
CanvasState stores a weak DocLayout reference and shared page-body bounds. It reuses the bounds for the same live layout, rebuilds them for a different layout, and initializes the cache as empty. show_pages uses the cache. The test checks cache reuse and replacement.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related issues

  • Issue 8112 — This PR implements a per-frame canvas allocation and memory work step identified as Phase 4 of the epic.

Suggested reviewers: benandez, vangel76

Merge Risk: Minimal · up to fef57

This change avoids rebuilding page-body ranges on every frame while the document layout is unchanged. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "perf(ui-egui): cache PageView body ranges per DocLayout in CanvasState (#8194)" names the main change. The PR adds a cache in CanvasState for the page-body Y-ranges passed to PageView, keye…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "perf(ui-egui): cache PageView body ranges per DocLayout in CanvasState (#8194)" names the main change. The PR adds a cache in CanvasState for the page-body Y-ranges passed to PageView, keyed by a weak reference to DocLayout and checked by pointer identity. The raw summary confirms this, along with the new cached_page_bodies method and the test. The title is specific and concise, and it follows a conventional commit style. The issue reference #8194 matches the PR description. The title does not mention that the cache is invalidated when the layout changes, but that detail is not needed for a title.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4daed0b and fef57ec.

Files selected for processing: 2

  • crates/ui-egui/src/canvas.rs
  • crates/ui-egui/src/canvas/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@benandez benandez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at fef57ec (static read, all changed files). Caching the PageView body Y-ranges per DocLayout in CanvasState is keyed on the layout Arc, so a relayout gives a fresh entry and nothing stale can be read. Approving.

@benandez
benandez merged commit 1b9b8b1 into storytold:main Dec 2, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants