Repository navigation
perf(base): Map.set updates an existing key in one walk down the trie - #2203
Merged
nicolas-abril merged 2 commits intoNov 18, 2026
Merged
Conversation
Contributor
Author
|
@nicolas-abril could you review this PR? |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Map.seton a key that is already in the map now walks the trie once instead of twice. Updates get 34-44% faster and use 22-29% less peak memory. A word-frequency count over 16 MB of text gets 19-21% faster.What
Before,
Map.setalways ranMap.seekto find the nearest leaf. If that leaf held the same key, it ranMap.put, a second walk from the root, to store the value.Now the first walk stores the value itself when the leaf's key matches. When the key is new, the walk hands the value back with the first bit where the two keys differ, and
Map.instakes it from there as before.Map.seek,Map.put,Map.set.goand the oldMap.set.finare removed.Map.setwas their only caller.git grepfinds no other use in tests, the guide,comp.tsorbendtt.lean. The tree shape does not change: an update rebuilds each node on the path with the same position and sibling, and only the leaf's value changes.Why
Counting words is the usual
Map.get+Map.setloop, and mostMap.setcalls hit a key that is already there. Asampleprofile of that loop over 16 MB of text putsMap.seekandMap.putat about 500 samples together, next to about 270 forMap.get.Results
Whole-process wall-time medians (p50) of 10 alternating pairs after 1 warm-up. Every run printed the same output digest on both sides. Peak RSS is the process high-water mark.
New keys still take two walks (find the nearest leaf, then
Map.ins), yet they also got faster and lighter. I measured that but did not dig into why. Word frequency RSS is unchanged; there the input string dominates memory.Workloads
String.trim,String.split(' '), thenMap.getandMap.setwith the count + 1. Print the wholeMap.to_list. The input is the repo's own text (GUIDE, README, WONTFIX, base.bend) repeated to 1, 4 and 16 MB.Map.setover n keys"item/" ++ U32.show(i * 2654435761), then the same n keys again. Print the size and the sum of values.C output of
bun bend2/main.ts <file> -o x.c, built withclang -std=c11 -O3 x.c -lpthread -lm(Apple clang on macOS, Debian clang 19 in Docker).How to test
bun gates/repo.tsMap.orSet.(git grep -l -E 'Map\.|Set\.' -- 'tests/*.bend') match their expected#|output on the interpreter,-o x.jsunder bun, and-o x.cnatively, before and after.Map.setof 5000 random keys over{\0, a, b, U+10FFFF}with lengths 0 to 6, so many repeat, then print the wholeMap.to_list. The output is identical before and after on the interpreter, bun, node and C.Risk
The change is limited to
Map.setand its private helpers inbase.bend.comp.tsandbend.tsare untouched. Reverting this one commit restores the old path.