Repository navigation
perf(c): a spin that meets a shared cell borrows the rest instead of taking it apart - #2134
Closed
nicolas-abril wants to merge 2 commits into
Closed
nicolas-abril wants to merge 2 commits into
nicolas-abril wants to merge 2 commits into
Conversation
…taking it apart A spin that matches a boxed parameter and loops on itself gets a second copy of its body, compiled with that parameter borrowed. The owned copy calls it when the parameter's cell is shared: everything under a shared cell is shared too, so the borrowed copy reads fields without touching counts, copies only what escapes, and the owned copy releases the cell once. A final return of the root itself hands that hold over. An arm that rebuilds a borrowed scrutinee from its own fields returns the scrutinee. String.drop matches its count first (in String.drop.go), so dropping nothing never reads the string. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…reuse, no root hand-over Co-Authored-By: Claude Fable 5.2 <[email protected]>
This was referenced Nov 12, 2026
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 13, 2026
A self-recursive def that owns a boxed parameter takes it apart as it walks. When the caller keeps the argument (uses it after the call) it shares it first, so the def walks a shared value: a count bump and a drop per cell. Where a call's boxed arguments are all kept, owned by the caller and data, and the def owns one of them, the call runs k~lend: the same def, its parameters borrowed, a part of them that escapes copied (val_own, as in a core). The caller seals those arguments first (the variant may copy one whole) and their types are hot, as sharing them would have made them; the def keeps the lends it had, so its own inference is as on main. A variant is emitted where a call first runs it, so a program without such a call emits main's C. A 1M-char string kept by its caller, String.drop half of it 300 times: 33.4G -> 19.0G instructions, 7.68G -> 5.71G cycles; the drop alone (the suffix then dropped) 16.3G -> 1.90G. The rest is the suffix's consumer walking it shared (bendlang#2134's runtime switch is the answer there). No perf bench calls one: all 17 emit as with the cores alone. 44 tests emit different C (23 run a variant); they print as on main and are clean under -DBEND_POISON, ASan and UBSan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 24, 2026
Collaborator
Author
|
Moved to nicolas-abril#7, 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.
The compiler-side answer to #2131: instead of a hand-written
String.dropintrinsic, the emitted C stops paying per cell when it walks shared data. Draft:comp.tsis over its cap, and CUDA and Linux are not run.The problem
A spin that consumes a boxed parameter takes each cell apart with
ctr_take. On a shared cell that goes throughspan_fade: bump every boxed field, then drop the cell through aFARterm_drop. The tail it just bumped is shared too, so every later step pays the same. #2131'sstr_dropavoids this by borrowing from the first shared cell on.What changes
KEPT). Fields are peeked, nothing is freed or reused, and a value that escapes is copied with one count bump (term_copy, which keepsspan_fade'sERR_RFCSfail-stop for an unsealed field). Before the owned copy's match,if (term_shr(e, s))calls the borrowed copy and then releases the cell once. The switch is in the loop, so a unique prefix is still consumed and freed, and the walk turns borrowed where sharing starts.seg_pure). That is what lets a def that matches aNatfirst get a borrowed copy.String.dropmatches its count first, in a helperString.drop.go(n, s), so dropping nothing never reads the string. Bend matches binders in order, so this needs the helper. This is the only Base change;String.dropstays an ordinary def and still folds at compile time.Measured
On this Mac (M5 Max),
clang -O3, one thread, whole-process medians of interleaved runs, against main f311527 and #2131 at b5ca688. Every build prints the same checksum.Map.get, 4,096 keys with a 256-char shared prefix, 50,000 lookups (15 runs)The Base change gains nothing on main by itself. With the compiler change and without it,
Map.getread 176 ms; the two together reach 138 ms.Verification
gates/test.ts: 1,855 / 1,855, with the newtests/run/string_drop_shared.bend(adapted from perf(c): borrow shared tails in String.drop #2131's fixture; it runs the shared path on 16 Metal lanes).#|lines under-DBEND_POISONwith ASan and UBSan, leak detection off. An earlier version of this branch, with two more pieces since removed, passed the same sanitizer run on all 21 tests that got a borrowed copy and print a result; that sweep was not repeated on this version.gates/perf.ts, twice on this version and twice on main, alternating: 117 and 114 of 124 here, 115 and 115 on main.gates/repo.ts: 55 / 56.comp.tsis 66,796 ttok, 796 over the 66,000 cap.Limits
String.eq, which perf(c): borrow shared tails in String.drop #2131 names as a large remaining cost, does not get a borrowed copy.span_fadealready checks, that every field under a shared cell is sealed or trivial.🤖 Generated with Claude Code