Skip to content

Optimize memo sync: lazy dirty flags instead of eager cloning#8

Merged
jensens merged 1 commit intomainfrom
optimize/lazy-memo-sync
Feb 27, 2026
Merged

Optimize memo sync: lazy dirty flags instead of eager cloning#8
jensens merged 1 commit intomainfrom
optimize/lazy-memo-sync

Conversation

@jensens
Copy link
Member

@jensens jensens commented Feb 27, 2026

Summary

  • Follow-up to #18 fix (819b21b) which introduced decode performance regression
  • Replaces eager sync_memo_top() (clones full container after every SETITEMS/APPENDS) with lazy dirty-flag approach
  • Memo entries marked dirty after mutations, resolved only when actually read via BINGET or when the owning stack slot is popped

Benchmark results (PGO build, 5000 iterations)

Decode median (us)

Category Baseline Eager (#18 fix) Lazy (this PR) vs Baseline
simple_flat_dict 1.0 1.5 1.3 +30%
nested_dict 1.6 3.2 2.5 +56%
large_flat_dict 18.0 31.5 25.1 +39%
special_types 3.8 6.8 5.6 +47%
wide_dict 244.5 585.8 348.6 +43%
deep_nesting 6.4 32.4 20.8 +225%

FileStorage (1,692 real records)

Metric Baseline Eager Lazy
Decode median 23.6 us 43.7 us 34.5 us
Encode median 4.0 us 4.3 us 4.1 us

Recovers ~half the regression. Remaining overhead is inherent to value semantics — when a dirty memo'd container is popped from the stack, we must clone it into memo. Only reference semantics (Rc) could eliminate this, but that was previously tried and regressed worse overall.

Test plan

  • cargo test — 200 tests pass (incl. 2 regression tests from #18)
  • pytest tests/ — 182 Python tests pass
  • PGO benchmark comparison against baseline

🤖 Generated with Claude Code

The fix in 819b21b cloned the full container into memo after every
SETITEMS/APPENDS (sync_memo_top), causing significant decode regression.
Replace with lazy approach: mark memo entries as dirty after mutations,
resolve only when BINGET reads a stale entry or when pop_value/pop_mark
consume the owning stack slot. Most memo entries are never re-read via
BINGET, so this eliminates the majority of unnecessary clones.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jensens jensens merged commit 0b462d6 into main Feb 27, 2026
5 checks passed
@jensens jensens deleted the optimize/lazy-memo-sync branch February 27, 2026 18:55
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.

1 participant