Repository navigation
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e path Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The recursion is now two module-level functions that return the removed value alongside the subtree, instead of local closures that capture the key and write the value into a captured variable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Switching on a tuple returned from a call allocates it; binding it with a let pattern doesn't. take() of an absent key now returns the map itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Switching on a tuple expression allocates the tuple. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The insertion is now a module-level function that returns the previous value alongside the subtree, instead of local closures, a captured variable and a clash record. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Switching on a tuple expression allocates the tuple. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he path Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…g the path Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tor result at a time Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ow instead of nested The first switch returns early, so the code stays flat without switching on a tuple. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…stead of nested switches Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
This PR makes the code less readable in places: it rewrites That's why we keep this PR as a draft for now. We hope to get caffeinelabs/motoko#6462 resolved first. Then the code in this PR can be made prettier again, back to switching on tuples, while keeping the same performance. 🤖 Generated with Claude Code |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Summary
pure/Mapandpure/Setallocated much more than the path copying of a persistent red-black tree requires. Four patterns, all removed here:lbalance/rbalance(every insertion) andbalLeft/balRight/append(every removal) didswitch (left, right), once per rebalanced level. They now switch on one subtree at a time:lbalance/rbalanceneed only one,balLeft/balRightuse two switches in a row (the first returns early), and onlyappend, whose cases depend on both subtrees, nests them. The same applies once per element or node topure/Map.equal/compareandpure/Set.equal/isSubset/compare, now written flat withlet-elseandlet (…) = switch …(neither allocates).insert/swap,remove/deleteandpure/Set.containswere local closures capturing the key (plus avarbox for the result and, inpure/Map.swap, anonClashclosure and a{ old; new }record per clash). They are now module-level functions returning(Tree, result).f(…).0,switch (f(…)) { case (t, null) … }) allocates it; alet (t, y) = f(…)pattern doesn't.The algorithms and the resulting trees are unchanged; no API or stable-type change.
Commits: one step each, benchmark first
Each bench file is added in its own commit before any code change, so every later commit can be checked out and benched against it (
mops bench PureMap,mops bench PureSet):pure/Mapremove: bench (18116a8) → early exit for absent keys → module-leveldel/delNode→ let pattern inremove/delete/take.pure/Mapadd: bench rowadd (existing)(c0825c8) →lbalance/rbalance→ module-levelins→ let patterns inadd/swap/insert→balLeft/balRight/append.pure/Set: bench (0612fb3) →containswithout closure →lbalance/rbalance→ module-levelins→ early exit for present elements → module-leveldel→ early exit for absent elements →balLeft/balRight/append→ let patterns.equal/compare(andisSubsetfor Set) (035b5e6) →pure/Map.equal/compare→pure/Set.equal/isSubset/compare.balLeft/balRightas two switches in a row instead of nested;equal/compare/isSubsetwithlet-elseinstead of nested switches.Measurements
moc 1.16.0, 64-bit heap,
n = 10000, with[optimize](wasm-opt 133, added locally, not committed); each row applies the operation to allnkeys (remove: every second key;misses:nabsent keys, each against the full structure).pure/Mapgetandmapare unchanged.equal/comparecompare with an equal copy built separately.add (existing)now allocates ≈1160 B per call, the copied path itself.pure/Set80 B is the bench's constant per-cell overhead. Without wasm-opt (the repo default) the changes are similar, and the Heap values are identical.
Not changed
entries,keys,values, …, used bypure/Map.equal/compareandpure/Set.compare) allocate about 275 B per element: three list cells and three variant objects per node. That is the remaining heap of theequal/comparerows; a stack-based iterator would be a separate change.🤖 Generated with Claude Code