Skip to content

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
mainfrom
ai/shared-borrow
Closed

nicolas-abril wants to merge 2 commits into
mainfrom
ai/shared-borrow

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

The compiler-side answer to #2131: instead of a hand-written String.drop intrinsic, the emitted C stops paying per cell when it walks shared data. Draft: comp.ts is 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 through span_fade: bump every boxed field, then drop the cell through a FAR term_drop. The tail it just bumped is shared too, so every later step pays the same. #2131's str_drop avoids this by borrowing from the first shared cell on.

What changes

  1. A borrowed copy of the spin, and a run-time switch to it. A spin that matches a boxed parameter and tail-calls itself gets a second copy of its body, compiled with that parameter borrowed (root KEPT). Fields are peeked, nothing is freed or reused, and a value that escapes is copied with one count bump (term_copy, which keeps span_fade's ERR_RFCS fail-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.
  2. The switch may sit after pure tests. The borrowed copy starts the body over, so the switch is emitted only when the lines that ran before it tested and named words (seg_pure). That is what lets a def that matches a Nat first get a borrowed copy.
  3. String.drop matches its count first, in a helper String.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.drop stays 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.

Workload main main + the Base change only #2131 this PR
Map.get, 4,096 keys with a 256-char shared prefix, 50,000 lookups (15 runs) 197.7 ms 201.5 ms 138.0 ms 138.3 ms
Drop half of a retained 1M-char string, 300 times (7 runs) 956.6 ms 919.6 ms 377.7 ms 376.3 ms
Control: drop from a unique 262k-char string, 300 times (9 runs) 490.7 ms not run 477.1 ms 479.6 ms

The Base change gains nothing on main by itself. With the compiler change and without it, Map.get read 176 ms; the two together reach 138 ms.

Verification

  • gates/test.ts: 1,855 / 1,855, with the new tests/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).
  • That fixture also matches its #| lines under -DBEND_POISON with 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.
    • hashmap is faster in both pairs: SEQ-CPU 2.19 s → 2.02 s, PAR-CPU 0.355 s → 0.331 s, PAR-GPU 1.21 s → 1.15 s.
    • bfs, lexer, merkle, symreg, tree-bitonic and tree-radix, compared cell by cell, do not move beyond run-to-run noise.
    • The pass counts differ through builds that fail on a node with an empty message. tree-radix fails to build in all four runs, main included. merkle's SEQ-CPU cell failed to build in one of the two runs here and in none on main; merkle gets no borrowed copy and builds and runs on this Mac from this branch, but the cause on the node is not known. histogram's PAR-GPU cell failed to build once here.
  • gates/repo.ts: 55 / 56. comp.ts is 66,796 ttok, 796 over the 66,000 cap.
  • Not run: CUDA, Linux, and a comparison of JS output after the Base change.

Limits

  • Only spins (flat defs) that loop on themselves, and only when the match comes first or after pure tests. String.eq, which perf(c): borrow shared tails in String.drop #2131 names as a large remaining cost, does not get a borrowed copy.
  • The borrowed copy relies on the invariant span_fade already checks, that every field under a shared cell is sealed or trivial.

🤖 Generated with Claude Code

nicolas-abril and others added 2 commits November 12, 2026 16:13
…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]>
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]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Moved to nicolas-abril#7, which keeps it saved off the main queue until it can be accepted.

@nicolas-abril
nicolas-abril deleted the ai/shared-borrow branch November 24, 2026 11:41
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.

1 participant