Performance - #18
Merged
Merged
Conversation
The immutable applyCommand clones the whole items/edges Map per command. Callers that fold N commands paid that clone N times, so replay, import, and cascade were all O(N^2). - reducer: add applyCommandInPlace (the single source of command semantics) and retractItemsInPlace; applyCommand now clones only the touched map(s) once and delegates, preserving immutability and the structural sharing of the untouched map. - replay: replayCommands / replayFromEnvelopes fold into one pair of maps in place -> O(N). applyCommandInPlace is atomic per command, so the skip-on-error contract is preserved. - transplant: importSlice imports the memory graph in place -> O(M). - integrity/query: add buildChildrenIndex; cascadeRetract and getDependents walk it instead of rescanning the graph per node, and cascadeRetract retracts in a single clone -> O(N). Post-order cascade ordering is unchanged. Measured: replay 16k 5831ms -> 1.3ms; importSlice 8k 1284ms -> 2.6ms; cascadeRetract 4k 399ms -> 2.0ms. Add bench/memex.bench.ts (npm run bench) and tests/in-place-fold.test.ts covering applyCommand immutability, applyCommandInPlace/replay equivalence with the immutable fold, retractItemsInPlace edge cascade, and a 50k-item scaling guard. 556 tests pass.
There was a problem hiding this comment.
No issues found across 11 files
You’re at about 96% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact contact@cubic.dev.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Measured impact (benchmark, since deleted)
replayCommands(16k)importSlice(8k)cascadeRetract(4k)Scaling went from quadratic to linear (16k replay now costs the same as 2k did). All 546 existing tests pass; only the pre-existing, unrelated
zodoptional-peer-dep build error remains.Summary by cubic
Make replay, import, and cascade operations run in linear time by folding commands in place instead of cloning per command. Replay of 16k events drops from 5831 ms to 1.3 ms, with similar wins for import and cascade.
applyCommandInPlaceandretractItemsInPlace; immutableapplyCommandnow clones only touched maps and delegates.replayCommands/replayFromEnvelopesfold into shared maps (O(N)), preserving skip-on-error semantics.importSliceimports memories/edges in place (O(M)); intents/tasks stay on immutable reducers.cascadeRetractandgetDependentsusebuildChildrenIndexand batch retract once (post‑order) to avoid rescans.bench/memex.bench.ts,tests/in-place-fold.test.ts), newbenchscript, andbench/excluded from publish.Written for commit 414ef81. Summary will update on new commits.