Skip to content

A value read from an Array is borrowed or moved, and a def may answer a borrowed part of its argument - #8

Draft
nicolas-abril wants to merge 2 commits into
mainfrom
opt/borrowed-reads
Draft

nicolas-abril wants to merge 2 commits into
mainfrom
opt/borrowed-reads

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Owner

On main (6180be5b)

This PR is a port onto main, on main: 2 commits of its own, with no integration history (head 9bd4aa94).

Port notes

  • Both commits applied unchanged (identical diffs).

Checks

  • Cluster gate (every test, every lane): 1877 / 1877 pass.
  • Locally: no new tsc errors against main, no new comp.ts lines over 80, its tests pass on the C and JS lanes (plus 40 lane runs of existing tests; run/array_slab fails on main too).
  • comp.ts: +2559 tokens (ttok -m gpt-3.5-turbo) over main, which is at 65,989 of the 66,000 cap.

Performance was measured on integration when this change merged there (see the original description or the replaced PRs); it has not been re-measured on main.

Original description (written for integration)

Moved from bendlang#2135 (opened 2026-11-12).

comp.ts is 68,402 ttok, 2,402 over the 66,000 cap, so repo-gate fails until that is settled. Measured on one Mac (M4), not on the cluster: the test and perf gates have not been run.

What it does

Three rules, all in comp.ts. Each falls back to today's copy when its condition fails.

  • Borrowed read: an Array.get of a boxed element reads the slot in place when its consumer only lends the element. The pair (array, element) a helper takes is a slot parameter: the array word is the helper's, the element is borrowed. A write, a free or a hand-on of the array while the element is live makes the helper own the element, and the read copies as before.
  • Take and put: a flat helper that takes an array read and calls itself nowhere is emitted where it is called, so the read, its uses and the write back share one scope. There, Array.set(a, i, v) where v holds the value lent from a[i], which nothing uses after, moves the value: the set drops nothing, and the element's family is no longer forced hot.
  • Borrowed result: a def whose every result is a part of one borrowed parameter answers a borrowed box, rooted where the caller's argument is. A probe pass over every unit finds the candidates before the fixpoint; a rown fact takes a result back (a result that is not a part, a tail call into the def from another def, a self call that passes something else).

Results

Wall time, one M4, GPU off, means of 7 runs.

program main this
hashmap, 1 thread 2.411 s 1.171 s 2.06x
hashmap, 8 threads 411 ms 207 ms 1.98x
lookups in a held tree (tests/run/borrowed_result_tree.bend at 10M lookups a pass) 2.189 s 0.259 s 8.5x

hashmap runs 18.50G instructions on main and 8.09G here; its C twin takes 0.58 s, so it is still about 2x behind C.

Bench suite: of the 17 bench/runtime programs only hashmap and histogram compile to different C. histogram's helpers are emitted inline and it takes the same time (1.573 s against 1.574 s). The other 15 are byte-identical.

The lookup program is written to show the borrowed result: the caller holds the tree across two passes. A tree handed over and never used again is owned by the callee, as on main, and gains nothing.

Checks

All local, against main f3115277:

  • Native lane: every test that builds and passes on main (881) passes here, the GPU off.
  • JS lane: the same 881 print their #| lines.
  • Memory: each of the 881 built with -DBEND_POISON and a count of live heap words at exit: the same output and the same count as main on every one.
  • New tests: tests/run/array_slot_borrow.bend, array_slot_take.bend, array_slot_part.bend, borrowed_result.bend and borrowed_result_tree.bend spell the shapes that must fall back (a slot overwritten or an array dropped while its value is read, a value written to another slot, two reads of one slot, a part that outlives its holder). They pass on the C lane, the JS lane and bend file.bend.
  • repo.ts fails only on the ttok cap.

Two bugs came up while writing those tests and are fixed here: a helper that dropped its array before reading the element, and the probe pass hiding layout facts. The first passed every existing test.

Not covered

  • Cluster gates, Metal and CUDA have not run.
  • A borrowed result is a bare box only (not a Maybe or a pair), and only where the caller's argument is itself borrowed or read from an array.
  • Take and put needs the same array and the same index variable in one scope after the helpers are emitted inline. A Map.get followed by a Map.set is untouched.
  • tsc was not run.

Cost

+2,440 ttok. Getting under the cap needs that many tokens cut elsewhere in comp.ts, or a raise.

🤖 Generated with Claude Code

@nicolas-abril

Copy link
Copy Markdown
Owner Author

Pushed 4a6843ca (+119 ttok).

  • The rets probe emitted every unit, which made c_units emit the book three times instead of main's two. It now emits only the units that can be in rets: a live def with a box result and a box parameter, or a unit that is not a live def.
  • Emit over the 17 runtime benches: 1.761 s to 1.680 s on this branch, and 2.171 s to 2.050 s on opt/integration 09d002f.
  • The C is the same up to declaration order and the numbering that follows it (STAT_OFF offsets, Clo_apply vs Clo_apply_1). The native lane's 1,123 tests keep every status.

nicolas-abril and others added 2 commits December 1, 2026 21:53
… a borrowed part of its argument

An Array.get of a boxed element reads the slot in place when its
consumer only lends the element; a write, a free or a hand-on of the
array while the element is live makes the read copy, as before. An
Array.set that stores the value read from the same slot moves it. A def
whose every result is a part of one borrowed parameter answers a
borrowed box.

hashmap: 2.41 s to 1.17 s on one thread. comp.ts is 68,402 ttok.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
(cherry picked from commit edadb78)
The probe (rets: a def whose every result is a part of its borrowed box
parameter) emitted every unit, a third emit of the book: c_units went from
two passes to three. A unit can be in rets only if its def is live with a
box result and a box parameter; the probe now skips the rest. Emit time
over the 17 runtime benches 1.761 s to 1.680 here (2.171 to 2.050 on
opt/integration 09d002f); the C is the same up to the order of its
definitions, and the native lane's 1,123 tests keep every status on
integration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 4a6843c)
nicolas-abril added a commit that referenced this pull request Dec 1, 2026
…33, #34, #36, #39, #44, #61-#68): record their heads

Each is a port onto main of integration's own branch, its content already
here (integration keeps the original commits; the ports add type fixes,
line wraps, moved pieces and #68's book_passes, applied in the previous
commit). Groups: #61 (#9 #11 #15 #32 #41 #42), #62 (#26 #56), #63 (#48 #50
and compile-time), #64 (#19 #20 #45), #67 (#55 #58), #68 (#54); par-defer
(#43) is now part of #34, lazy-startup's parts are in #16 and #34. This
merge records their heads (-s ours: no content change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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