Repository navigation
A heap node's U32 and tag fields share a word when that drops its size class (rbtree ports -9% to -31% cycles) - #2218
Closed
nicolas-abril wants to merge 4 commits into
Closed
nicolas-abril wants to merge 4 commits into
nicolas-abril wants to merge 4 commits into
Conversation
…ts size class
A node of a user type keeps each field in its own 64-bit word, so a Bool or
an enumeration tag takes a word, and rbtree's Node{color, l, key, value, r}
fills 5 words of an 8-word block. node_pack lays such a node's w32 and tag
words into shared words under 2^56 (the top byte zero, so the runtime reads
them as trivial terms: drop, sharing and span_fade skip them), at most one
U32 per word, and is used only when that drops the node a size class
(rbtree's Node: 5 words in 8 to 3 in 4). ctr_build writes the shared words,
node_fields reads them back, and c_header gives the runtime the new arity
and spine. Base's runtime types and any type foreign C names or takes (and
what it holds) keep their layout.
Tests: tests/run/node_pack.bend (three packed types through builds,
matches, reuse, sharing, closures, a List, a Maybe, a fork, a `!` call),
tests/io/node_pack_foreign.bend (foreign C writes a packable type itself).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The wrong version that lets two U32 share a word (its top byte then holds bits of the second) printed the right hashes when Qn nodes were only built and matched; sharing and dropping them make the runtime read that word as a term, and it faults. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A drop that walks the wrong slot as the spine leaks 102,260 words or overwrites the live tree; the earlier spellings of the test did not show it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 22, 2026
… fields share a word when that drops its size class Conflict in comp.ts, node_fields' reads (bendlang#2218 against bendlang#2207): bendlang#2207 takes a node with ctr_take only when its constructor is hot (a static one is read in place), bendlang#2218 reads each field from its packed word. Both kept: the word a field lives in is bendlang#2218's (slot, shift, bits), read from the take's buffer when hot and from the node otherwise, as bendlang#2207 decides. Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 22, 2026
…d word Interaction of bendlang#2207 and bendlang#2218. bendlang#2207 splits an arm that opens with a match on the node's first field, an enumeration, and tests that field through the reference as `(u32)e.mem[term_peek(e.mem, t)]`, word 0 of the node. bendlang#2218 packs a node's U32 and tag fields into shared words when that drops the size class, so for a 5-field Node{color, l, key, val, r} word 0 is `key | color << 32 | val << 34`: the split read the key's low bits as the colour and took the wrong arm. Checked rbtree-shaped programs printed wrong numbers on C (both drafts alone, main, JS and the interpreter agree). The read of a field's word is now one function, node_word (slot, shift and mask from node_pack), used by node_fields and by the split; for a node that is not packed the split's C is unchanged. Tests tests/run/mix_pack_split.bend (unique trees) and mix_pack_split_shared.bend (shared, so ctr_take) fail without the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 22, 2026
mix_sink_calls: bendlang#2217's U32 lets that sink below a non-tail call, in a tree recursion whose returning arms run at the call (bendlang#2177), in sum loops of two and three self calls and one whose arm answers 0 (bendlang#2175), next to a fixed call (bendlang#2180), a let the call reads (stays), and a parallel call. mix_reform_slots: trees in an Array's slots (bendlang#2135: read borrowed, taken and written back) walked by a selector-first lookup and insert (bendlang#2211), split by colour and re-formed (bendlang#2207), lent to a flat def, with the node's U32, Bool and colour in one word (bendlang#2218), in @unsafe helper cycles. Both print main's numbers on C at 1 and 8 threads and on JS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 22, 2026
Collaborator
Author
|
Moved to nicolas-abril#26, which keeps it saved off the main queue until it can be accepted. |
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.
comp.tsis 66,856 ttok, 856 over the 66,000 cap (main 65,995, +861), sorepo-gatefails until that is settled; every otherrepocheck passes locally (55 / 56). Measured on one Mac (M4) under load from other sessions, not on the cluster: thetestandperfgates have not been run.What it does
A heap node keeps each field in its own 64-bit word, so a
Boolor an enumeration tag takes a whole word, and the node's block is the next power of two. The Perceusrbtree'sis 5 words in an 8-word block, 3 of them for 34 bits. This PR lays
color,keyandvaluein one word,key | color << 32 | value << 33, soNodeis 3 words in a 4-word block. The program cannot tell: the layout is per constructor and every build and read goes through the same two functions.node_pack(k)(lay_bitsgives each word's bits: a tag'sceil(log2 arms), 32 for aw32, 64 for a box orw64): thew32and tag words are packed first-fit, widest first, into words of at most 56 bits (so at most oneU32per word), the shared words first, then the boxes in order. It is used only when that lowers the node's size class.ctr_buildwrites each shared word as(u64)((u32)(x) & mask) << shift | ...(the static image too);node_fieldsreads a field back as(u32)(w >> shift) & mask;c_headergives the runtime the new arity and spine.OWNED), nor for a type that foreign C names (CID(..)in an effect source) or takes (in a foreign def's signature), nor anything those hold (ty_fix): such C writes the words itself.The condition, and why it suffices
The layout is a function of the constructor alone, the same in every def, closure, fork task,
!call and polymorphic caller of one binary, so a node built anywhere is read correctly everywhere. Where a node's words are observed:ctr_buildwrites them and onlynode_fieldsreads them (fresh, shared throughctr_take, or peeked through a lent root), and both usenode_pack. A generic field is a box (64 bits) and is never packed, so instances agree.term_drop,span_fadeunderctr_take,rfc_seal): they read each word as a term. A shared word is below 2^56 (masks on write keep it there even if a tag were out of range), so its tag byte is 0 and its RFC bit clear: a trivial term, skipped like anyU32field today.CID_Tcarries the new arity (the block's class) and the spine at its new slot.OWNED) andList'sConandNil, whose fields are boxes.emit_ctr,val_arms,emit_zeroor a comparison), below 2^bits; aw32is au32.Reach, counted before building
A hook over the checked terms (Base, the tests, the demos, the 17 benches, the 22 koru kernels and their 17 forked ports, the 7 Perceus ports; 862 files that check) and a run-time count of every node built, per constructor, in the 63 runnable programs. Branch
bench/representation(nicolas-abril/bend) has both, the scripts, the other candidates and their counts, and every measurement below.Node,tests/reg/show_shared_layout'sOther(25 words to 1)On a hot path: only the rbtree ports, 340M to 504M
Nodes per run. No bench or kernel builds a node this changes: their hot nodes are boxes only (tree-bitonic'sNode, 4.26G per run;tree-radix,tree-matmul), a word and boxes (merkle'sMn,hashmap'sCCons), or threeU32(kmeans'Sl, 168M per run, which two shared words cannot hold under 2^56).As built, the C changes in 4 Perceus ports, raytrace (a
CID_Tentry only:Hitis never boxed at run time), 4 demos (pong'sPong, slash boss'sCue,Sound,Voice, the netcode walkers test'sKey, pure_hvm5_mini'sBind), 9 tests and the 2 new tests. Every other bench, kernel, forked port, Perceus port, test and demo emits byte-identical C.Results
M4, one thread, GPU off,
cc -O3. Instructions (median), and P-core cycles least / median / most over 16 placements (0 to 15nops before the first segment), 3 rounds in all, the least per placement; load average 6 to 18 from other sessions.rbtreerbtree-ckrbtree-unsaferbtree-ck-unsafeEvery placement is faster on all four. The
-ckports keep every fifth tree alive, so they are bound by memory and their ranges are wide under load; the median gain there is about 30%.Maximum resident set: rbtree 270 MB to 132 MB (one run each).
Where the gain comes from,
rbtreeandrbtree-ck(a variant that shares the words but pads the node back to 5 words, so the block stays at 8):rbtreerbtree-ckSharing the words alone saves instructions (one load for three fields) and no cycles; the cycles come with the smaller block. That is why the rule asks for a lower size class, and leaves the 15 constructors where it would not drop alone.
The rest of the suite: the 16 other benches, the 22 kernels, the 17 forked ports and the other 3 Perceus ports emit byte-identical C, so they are not rerun. raytrace (its
CID_Tdiffers in one entry): 83.02G / 83.24G instructions on main, 83.20G / 83.16G here, two runs each.Checks
-DBEND_POISONand a live-word count at exit (heap_alloc/heap_freepatched) at 1 and 8 threads. All the same on both, every test's#|lines met, 0 live words at exit everywhere. The other 919 tests and demos that build emit byte-identical C, so their status cannot differ.tests/run/node_pack.bend(three packed types built, matched, rebuilt in place, shared, captured by a closure and a partial application, held in aListand aMaybe, dropped, handed to a fork and to a!call, read down a deep spine, and laid out in the static image; keys with the top bit set),tests/io/node_pack_foreign.bend(foreign C writes a type that would pack). Interpreter, JS and C agree; also with a 1,000,000-node spine (C only, the interpreter overflows), and on Metal (--gpu on) fornode_pack.bst_insert)CID_Tkeeps the old word countU32in one word (the top byte then holds data)bst_insert)The spine and two-
U32versions passed the first spellings ofnode_pack.bend; the test now shares and drops nodes and drops a big tree next to a live one.tsc: no new errors incomp.ts(the same 3 as main).bun gates/repo.ts: 55 / 56, the ttok cap.Not covered
test,perf,ping,safe); CUDA; Linux.Stats, lexer'sMode, raytrace'sHit, tree-radix'sChkare on hot paths, none bounded by hand).U32cannot share a word: the word would no longer look like a trivial term toterm_dropandspan_fade. Allowing it (and sokmeans'Sl) needs the runtime to know a node's raw words.🤖 Generated with Claude Code