Skip to content

Memory: floating pictures' textures are bounded, sized to what is drawn, and dropped with their workbook - #1631

Merged
aiongg merged 1 commit into
storytold:mainfrom
arcanii:picture-textures-bounded
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
arcanii:picture-textures-bounded

Conversation

@arcanii

@arcanii arcanii commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

What it does

Floating pictures (Insert › Pictures) were drawn from TEXTURES in chartview.rs, one process-wide map of textures that:

  • decoded every picture at full size, with no limits, and briefly in two or three copies (to_rgba8, then ColorImage);
  • never let go, not even when the picture's workbook was closed;
  • told pictures apart by id and byte length alone. Picture ids are per workbook, so a second open workbook could show the first one's picture: a new test shows it on main;
  • tried a picture it couldn't decode again every frame.

A new crates/ui-egui/src/floating_pictures.rs, built the way cell_pictures.rs is, replaces it.

How

  • A cache per egui context, keyed by workbook (DocState::uid), picture id and size.
  • Bounded at 128 MB, dropping the least recently drawn first. A picture drawn in the last frame is never dropped, so no picture turns into a placeholder, whatever the budget.
  • A workbook's textures go when it closes. This is checked every frame in SheetApp::logic, so it works even when no sheet is painted.
  • Decoded with limits: the header is read first (16,384 px a side, 64 megapixels at most), then the picture is decoded with image::Limits.
  • Decoded at about the size it is drawn: the next step of a power of two or three quarters of one (…, 1024, 1536, 2048), up to 4,096 px or the GPU's max_texture_side. It is decoded again only when drawn larger than that, so zooming in re-decodes at most every half or third larger.
  • A picture that can't be decoded is remembered, not retried every frame.

How it was measured:

  • macOS 27.0.1 on an Apple M3 Max, release builds from main (bb685e3) and from this branch.
  • A throwaway example, not in this pull request, using the snapshot example's harness. It opens an .xlsx with 12 or 24 copies of a 2600 × 2600 picture (each drawn 640 points square) beside a blank workbook. It scrolls to each picture in turn, rendering at each, holds, then closes the workbook and holds again.
  • "Textures" is what egui holds (tex_manager). "Footprint" is the settled physical footprint with MallocLargeCache=0. Opening a 243–487 MB file otherwise leaves freed buffers in the footprint; with the cache on, the savings were the same or larger.
Textures, scrolled Footprint, scrolled Textures, closed Footprint, closed
12 pictures 258 → 40 MB 738 → 509 MB 258 → 0 MB 504 → 236 MB
24 pictures 490 → 76 MB 1,211 → 779 MB 490 → 0 MB 745 → 236 MB

Not in this pull request

Checks

On macOS 27.0.1 with Rust 1.98.1, cargo xtask ci passes: fmt, clippy, test, assets, layers, wasm, parity and keytips, 1,616 tests. Not run on Windows or Linux.

New tests:

  • In crates/ui-egui/src/floating_pictures.rs:
    • pictures_are_decoded_at_about_the_size_drawn: the size steps, re-decoding only when drawn larger, the old texture's bytes given back, and the GPU's limit and the source size respected.
    • the_cache_stays_within_its_budget_but_keeps_what_is_on_screen: 40 pictures of 2048 × 2048 stay within the budget, and 12 on screen at once, over the budget, keep their textures frame after frame.
    • a_closed_workbook_takes_its_pictures_with_it: closing drops a workbook's textures, and two workbooks' picture 1s get separate textures.
    • broken_and_oversized_pictures_are_refused_once: garbage is remembered and not retried, and a PNG whose header says 20,000 × 20,000 is refused before decoding.
  • In crates/ui-egui/tests/floating_pictures.rs, each_workbook_shows_its_own_picture: two workbooks, each with a picture 1 of the same byte length, render with different textures, and closing one leaves the other's picture as it was. On main it fails: the second workbook shows the first one's picture.

🤖 Generated with Claude Code

…wn, and dropped with their workbook

Floating pictures were decoded at full size, with no limits, into one
process-wide map of textures that never let go, not even when their
workbook closed. Its key was the picture's id and byte length, and ids
are per workbook, so a second open workbook could show the first one's
picture.

The new floating_pictures module, built like cell_pictures, keeps a
cache per egui context keyed by workbook, picture id and size:
- bounded at 128 MB, least recently drawn first, never evicting a
  picture drawn in the last frame, so none turns into a placeholder;
- a workbook's textures go when it closes (checked every frame);
- a picture's header is read first (16,384 px a side, 64 MP at most),
  it is decoded with limits, and to the next size step of what is
  drawn (a power of two or three quarters of one, up to 4,096 px or the
  GPU's limit); it is decoded again only when drawn larger;
- a picture that can't be decoded is remembered, not retried each frame.

Measured on macOS 27 (Apple M3 Max): 12 pictures of 2600 x 2600 opened
from an .xlsx and scrolled through, textures held by egui 258 -> 40 MB,
settled footprint 738 -> 509 MB; after closing the workbook, textures
258 -> 0 MB, footprint 504 -> 236 MB. 24 pictures: textures 490 -> 76 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
arcanii added a commit to arcanii/DevCraft that referenced this pull request Oct 16, 2026
…old#1629, storytold#1630 and storytold#1631 (aniefork)

The sister fork's memory work on WordCraft, done for GridCraft: upstream
bb685e3 measured through the CLI and the offscreen snapshot with
wordcraft's sampler and a new generator (docs/memory-aniefork/), and the
code traced. Findings in docs/memory-aniefork.md. Offered upstream from
branches cut from upstream/main: undo steps share picture and macro
bytes (storytold#1629), interface fonts shared with egui (storytold#1630), floating
pictures' textures bounded and kept per workbook (storytold#1631). Docs and tools
only on the fork's main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MichaelMuClaude

Copy link
Copy Markdown

Verified against main (applies clean). This is a large, well-measured PR (new module replacing the old process-wide TEXTURES map), so I focused on the concrete, testable bug it calls out plus the new module's own test suite:

  • Isolated just the new integration test (each_workbook_shows_its_own_picture) and ran it alone against unpatched main (it only exercises existing infrastructure, no new module needed to compile): fails exactly as described — both workbooks' picture 1 render with the identical texture ID (Managed(1) both sides), confirming the cross-workbook leak is real, not a hypothetical.
  • Applied the full patch: the same integration test passes, plus all 4 of the new module's own tests (pictures_are_decoded_at_about_the_size_drawn, the_cache_stays_within_its_budget_but_keeps_what_is_on_screen, a_closed_workbook_takes_its_pictures_with_it, broken_and_oversized_pictures_are_refused_once).
  • cargo test -p gridcraft-ui-egui --lib → 98/98, --test interaction → 35/35 (no regressions elsewhere).
  • cargo clippy -p gridcraft-ui-egui --lib --tests -- -D warnings and cargo fmt --check -p gridcraft-ui-egui both clean.
  • Read the eviction logic (floating_pictures.rs:86): e.used.saturating_add(1) < frame protects anything drawn in the current frame or the previous one from eviction regardless of budget — actually a frame more conservative than "never drops what was drawn last frame," which matches (and slightly exceeds) the stated guarantee that nothing on screen turns into a placeholder.

Didn't re-run the macOS memory profiling, but the mechanism (per-context cache keyed by workbook+id+size, replacing a process-wide id+length key) is a sound, directly verifiable fix for the described leak, and the new test proves the specific bug it targets rather than just asserting the refactor "works." Good to merge.

@aiongg
aiongg merged commit fccca92 into storytold:main Oct 22, 2026
1 check passed
arcanii added a commit to arcanii/DevCraft that referenced this pull request Oct 22, 2026
…rk's hooks kept

Upstream a1c8454, 610 commits since the merge before (68e58a2), 345 of them not merges, with
release 0.10.0 and the fork's memory pull requests storytold#1629, storytold#1630 and storytold#1631.

Conflicts:
- README.md: the fork's kept (upstream added its app icon and fixed a link).
- crates/ui-egui/src/grid.rs, the cell editor: upstream's wrapping editor for a Wrap Text
  cell, the fork's growth a whole cell at a time for any other; paint_editor gets the
  wrapped width there.
- crates/ui-egui/src/lib.rs: upstream's autosave, then the fork's field, in UiState and its
  Default; aniefork::poll right after control::poll, then upstream's guard_window_close.
- crates/ui-egui/src/ribbon.rs: upstream paints a title cut to fit between the controls; the
  fork's window_title goes through it, so the unsaved dot follows the application's name.

The fork's own code follows upstream: vba_project is an Arc (storytold#1629) in the macros tests, and
the panels' batch estimates its undo step's retained_bytes (storytold#1227) as the engine does.
Upstream fixed the clippy lint the fork had patched in desktop_theme.rs: the patch is gone.
Two upstream tests that measure the editor's painting, which the fork replaces, are ignored
(tests/in_cell_editor_size.rs). Cargo.lock takes the fork's crate to 0.10.0.

Every hook in ROADMAP-aniefork.md's table is present; docs/brand/ holds only
LICENSE-brand.txt. cargo xtask ci on macOS: all eight steps, 2,456 tests, 3 ignored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants