Skip to content

Memory: undo steps share picture and macro bytes instead of copying them - #1629

Merged
aiongg merged 1 commit into
storytold:mainfrom
arcanii:undo-shares-picture-bytes
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
arcanii:undo-shares-picture-bytes

Conversation

@arcanii

@arcanii arcanii commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

What it does

Each undo step keeps the workbook as it was before the edit. Sheets and their 64-row bands of cells are shared between steps (copy-on-write), but two byte blobs were not:

  • a floating picture's bytes (Image.data), copied with its sheet at every edit on that sheet;
  • the VBA project (Workbook.vba_project), copied with the workbook at every edit.

So one 20 MB picture cost another 20 MB for every cell typed on its sheet, up to the 100 steps of the history (2 GB). Both are now Arc<Vec<u8>>, as cell pictures (Arc<CellPicture>) already are, so the steps share one payload.

How

  • Image.data and Workbook.vba_project are Arc<Vec<u8>>.
  • gridcraft_model::b64::shared (new) and b64::opt (now for Option<Arc<Vec<u8>>>) keep their JSON exactly as before: base64 text. XLSX reading and writing are unchanged.
  • The rest is call sites: .into() where a Vec becomes the field, .to_vec() where the XLSX writer copied the bytes into a part before.

How it was measured:

  • macOS 27.0.1 on an Apple M3 Max, release builds of main (bb685e3) and of this branch.
  • Physical footprint (Activity Monitor's "Memory") through proc_pid_rusage: the kernel's lifetime peak for gridcraft-cli run --script.
  • A synthetic 2600 × 2600 noise PNG of 19 MB, inserted with insert.picture, then N cell.set on the same sheet:
Cell edits after the picture Before After
0 21 MB 21 MB
10 215 MB 21 MB
50 992 MB 22 MB

Not in this pull request

  • Undo after commands over whole ranges. A sort or a format over every row copies every band, so every cell. On a 2-million-cell sheet that is about 110–140 MB a step (20 sorts peaked at 3 GB). That is Undo history keeps ~16 MB per bulk edit on a large sheet: ~1.45 GB for 100 steps on one workbook #1227, and Bound Undo history growth after bulk edits #1263 caps the history by an estimate of the cells it keeps. That estimate counts cells only, so it wouldn't have caught the pictures; the two fit together.
  • Other fields of a touched sheet are still copied at every edit: the spill map, comments, conditional formats, validations, tables and others. Sharing them behind Arc would be a larger change.
  • One XLSX media part shown by several pictures is still read into one copy per picture. With this change those copies could share one Arc.

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,612 tests. Not run on Windows or Linux.

New test in crates/engine/src/tests.rs: undo_steps_share_picture_and_macro_bytes. It inserts a picture with insert.picture, sets a VBA project and types three cells. It then checks that every undo step's picture bytes and VBA project are the same allocation as the live ones (Arc::ptr_eq), and that undo and redo give the picture back.

🤖 Generated with Claude Code

Every edit copies the workbook for its undo step (copy-on-write: sheets
and 64-row bands of cells are shared). A floating picture's bytes
(Image.data) and the VBA project (Workbook.vba_project) were plain
Vec<u8>, so each edit on a sheet with a picture, and every edit in a
workbook with macros, kept another full copy of them, up to the 100
steps of the history. They are Arc<Vec<u8>> now, as cell pictures
already are, with base64 helpers for serde (b64::shared, b64::opt). JSON
and XLSX are unchanged.

Measured on macOS 27 (Apple M3 Max), gridcraft-cli, kernel peak
footprint: a 19 MB picture and 50 cell edits on its sheet, 992 -> 22 MB;
10 edits, 215 -> 21 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 one changes the Image::data/Workbook::vba_project field types directly (Vec<u8> → Arc<Vec<u8>>), which cascades through every call site across 4 crates — too interlinked to isolate a "fails without the fix" test, so I applied the full diff and checked the mechanism directly plus ran every affected crate's suite:

  • Confirmed the premise first: Doc/undo-history entries hold Arc<Workbook> (engine/src/lib.rs:62/70/98), and edits go through Arc::make_mut, which clones the Workbook struct's fields shallowly on write. Any field that isn't itself behind an Arc gets byte-for-byte copied on every edit — which is exactly the mechanism the PR describes for why a 20 MB picture cost another 20 MB per cell edit.
  • Ran the new test alone: undo_steps_share_picture_and_macro_bytes passes, and its Arc::ptr_eq assertions across every undo step (not just one) are the right check for this — they prove sharing, not just equal bytes.
  • Ran the full suite of every touched crate: gridcraft-engine 288/288, gridcraft-xlsx 174/174, gridcraft-model 14/14, gridcraft-ui-egui --test interaction 35/35.
  • cargo clippy -p gridcraft-engine -p gridcraft-xlsx -p gridcraft-model -p gridcraft-ui-egui --lib --tests -- -D warnings and cargo fmt --check on the same four crates: both clean.
  • Spot-checked the write-side changes: sheet_write.rs/write.rs correctly switch from .clone() to .to_vec() on the now-Arc-wrapped data before handing it to out.part(...) (which wants an owned Vec<u8> for the XLSX part), so the on-disk format is genuinely unchanged — only the in-memory representation moved.
  • The new b64::shared module mirrors b64::opt's existing JSON shape exactly (base64 text either way), so the control-channel/save-prefs JSON format isn't affected either.

Correct, mechanically thorough (every call site updated, not just the hot ones), and the measured numbers' mechanism checks out structurally even without re-running the macOS memory profiling. Good to merge.

aiongg added a commit to aiongg/gridcraft that referenced this pull request Oct 22, 2026
…ter storytold#1629)

shared_media.rs predates the Arc change; literals and comparisons use .into() and to_vec().
@aiongg
aiongg merged commit 8e2974c 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