Skip to content

perf(base): Map.set updates an existing key in one walk down the trie - #2203

Merged
nicolas-abril merged 2 commits into
bendlang:mainfrom
sebastianbreguel:perf/map-set-one-walk
Nov 18, 2026
Merged

nicolas-abril merged 2 commits into
bendlang:mainfrom
sebastianbreguel:perf/map-set-one-walk

Conversation

@sebastianbreguel

@sebastianbreguel sebastianbreguel commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Map.set on 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.set always ran Map.seek to find the nearest leaf. If that leaf held the same key, it ran Map.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.ins takes it from there as before.

Map.seek, Map.put, Map.set.go and the old Map.set.fin are removed. Map.set was their only caller. git grep finds no other use in tests, the guide, comp.ts or bendtt.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.set loop, and most Map.set calls hit a key that is already there. A sample profile of that loop over 16 MB of text puts Map.seek and Map.put at about 500 samples together, next to about 270 for Map.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.

Case macOS arm64, base → branch Docker Linux arm64 (4 CPUs, 2 GB), base → branch
word frequency, 16 MB 2.29 → 1.80 s (-21%) 4.00 → 3.24 s (-19%)
word frequency, 4 MB 0.589 → 0.475 s (-19%) 1.04 → 0.819 s (-21%)
word frequency, 1 MB 0.162 → 0.133 s (-18%) 0.253 → 0.205 s (-19%)
320k updates 1.29 → 0.789 s (-39%), RSS 124 → 88 MiB 1.39 → 0.916 s (-34%), RSS 134 → 96 MiB
80k updates 0.233 → 0.144 s (-38%), RSS 32 → 23 MiB 0.288 → 0.178 s (-38%), RSS 42 → 30 MiB
20k updates 0.0549 → 0.0338 s (-38%) 0.0613 → 0.0346 s (-44%)
320k new keys 0.606 → 0.482 s (-20%), RSS 124 → 88 MiB 0.736 → 0.610 s (-17%), RSS 134 → 96 MiB

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
  • Word frequency: read a file, and for each line String.trim, String.split(' '), then Map.get and Map.set with the count + 1. Print the whole Map.to_list. The input is the repo's own text (GUIDE, README, WONTFIX, base.bend) repeated to 1, 4 and 16 MB.
  • Updates: Map.set over n keys "item/" ++ U32.show(i * 2654435761), then the same n keys again. Print the size and the sum of values.
  • New keys: the first half of the above only.

C output of bun bend2/main.ts <file> -o x.c, built with clang -std=c11 -O3 x.c -lpthread -lm (Apple clang on macOS, Debian clang 19 in Docker).

How to test

  • bun gates/repo.ts
  • The 13 tests that use Map. or Set. (git grep -l -E 'Map\.|Set\.' -- 'tests/*.bend') match their expected #| output on the interpreter, -o x.js under bun, and -o x.c natively, before and after.
  • A differential check: Map.set of 5000 random keys over {\0, a, b, U+10FFFF} with lengths 0 to 6, so many repeat, then print the whole Map.to_list. The output is identical before and after on the interpreter, bun, node and C.

Risk

The change is limited to Map.set and its private helpers in base.bend. comp.ts and bend.ts are untouched. Reverting this one commit restores the old path.

@sebastianbreguel

Copy link
Copy Markdown
Contributor Author

@nicolas-abril could you review this PR?

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nicolas-abril
nicolas-abril merged commit ad61bc7 into bendlang:main Nov 18, 2026
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.

2 participants