Skip to content

fix(runtime): share large freed blocks across host lanes - #1869

Merged
Lorenzobattistela merged 2 commits into
bendlang:mainfrom
sebastian-griego:fix/large-block-retention
Oct 26, 2026
Merged

Lorenzobattistela merged 2 commits into
bendlang:mainfrom
sebastian-griego:fix/large-block-retention

Conversation

@sebastian-griego

Copy link
Copy Markdown
Contributor

Fixes #1856.

Each host lane currently keeps its last freed large block in COLD. Other lanes cannot reuse that block until the lane frees another block of the same class. Large temporary arrays can therefore increase peak memory with the worker count.

Send host blocks of at least CAP_WORDS (256 KiB) directly to the shared bank in heap_hand. Smaller blocks keep the existing HOT/COLD policy. The decision stays in the generation handoff, with no new branch in the inline heap_free path. Device frees still use dev_cut.

The cutoff uses CAP_WORDS because GPU mode sets KEEP_WORDS to 256 words. A normal Bend host loop that fills and drops 2 KiB arrays showed heavy lock contention when those blocks also went directly to the bank. With the fixed cutoff, its paired median time ratios are 1.016 at one thread and 0.930 at 16 threads.

The regression checks immediate reuse by another lane at 256 KiB and 1 MiB, no heap growth during reuse, live payload integrity, and smaller cached generations in both CPU and GPU modes. It fails on main. It also rejects the lower GPU cutoff.

A local Bend workload produces 32 arrays of 524,288 U32 cells on one lane, reads their endpoints on parallel lanes, and repeats this for 12 rounds. All runs return 2726963360. Medians of three runs on an Apple M5 Max:

CPU threads Peak RSS before (MiB) Peak RSS after (MiB)
1 65.59 65.59
4 72.02 65.95
16 96.73 66.92

At 16 threads, this workload uses about 31% less peak RSS. Large-block retention in lane caches is zero in all final runs.

Validation against dd7d74db with Bun 1.3.14 and Apple clang 21:

  • New regression: CPU at 1/16 threads, Metal, and BEND_POISON. Bun and interpreted IO also pass.
  • 55 existing fixtures: 110 native checks; 16 fixtures: 32 poison checks; nine Metal fixtures pass with the final compiler.
  • Concurrent allocation, payload, and heap growth checks pass with 16 threads under normal execution, poison, and UBSan, for the host cache sizes in CPU and GPU modes.
  • All 17 runtime benchmarks at 1/16 CPU threads, plus five Metal benchmarks: 384 output checks pass. Three-run median time ratios are 0.921–1.061. Five-run repeats of the largest increases are 0.964–1.030. Peak RSS ratios are 0.986–1.021. All ratios are after/before.
  • Repository gate: 54/54. TypeScript diagnostics match main exactly; the existing diagnostics remain in bend.ts, safe.ts, and documentation tools.

The cluster test/performance gates, CUDA, and BendTorch training were not run locally.

@nicolas-abril nicolas-abril self-assigned this Oct 26, 2026
@Lorenzobattistela Lorenzobattistela self-assigned this Oct 26, 2026
The heap comment again says a host chain is handed off at KEEP_WORDS and
that CAP_WORDS >= KEEP_WORDS, which keeps a large handoff to one block.
The small-class check fails cleanly instead of writing past its four
blocks.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Lorenzobattistela
Lorenzobattistela merged commit 378be5f into bendlang:main Oct 26, 2026
1 check passed
nuxyel added a commit to nuxyel/BendTorch that referenced this pull request Oct 29, 2026
luiscleto added a commit to luiscleto/framebender that referenced this pull request Nov 10, 2026
…published with their READMEs

Bend 2.0.40 hands large freed blocks to the shared pool itself (bendlang/bend#1869), so build.sh no
longer patches the generated runtime; it stops on a Bend older than 2.0.40 instead. Same output on
every suite (same, threads, par, out, index), same peak memory, same or slightly better time
(docs/BENCHMARK.md, "Bend 2.0.40").

Imports move to lawful-words, -arrays and -halves 0.1.0.1 and lawful-bytes and -fields 0.1.1.1
(the same code, now published with their READMEs). The gate passes on 2.0.40; test/bcheck.sh's cap
goes from 4 to 6 GB, as the --verdict of PROOF.bend now peaks near 3.8 GB.

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.

C runtime: each lane parks a freed big block as its COLD generation, so a training step's peak memory grows with the thread count

3 participants