Repository navigation
Description
What you did
bend repro.bend -o repro
./repro --threads 1
./repro --threads 4
./repro --threads 16
repro.bend (below) builds the same ~3M-char String 24 times in one process: 512 rows of number text, the rows forked in halves, then measures its length and prints ms per round.
What happened
The work is identical every round, but each round is slower than the one before, and it levels off at 3-17x. With threads it gets worse than with one: past round ~10, 4 and 16 threads are slower than 1.
t=1: 72 70 66 62 70 77 90 105 135 157 175 204 207 215 232 229 243 249 252 247 252 243 250 236
t=4: 33 34 37 44 53 73 88 113 144 157 189 216 253 263 279 314 355 364 413 432 457 498 531 550
t=16: 35 30 33 42 47 65 84 114 125 152 184 199 229 257 282 298 357 349 353 407 442 479 464 489
It is not a leak: RSS stays flat (159 MB after the first round, 167 MB peak over all 24 at 4 threads). It is not the number of IO steps either: 2,200 empty IO.write("") steps between rounds change nothing.
What the counters say
perf stat on repro.bend at --threads 1, per round, a 3-round run vs the 24-round run:
| per round | 3 rounds | 24 rounds |
|---|---|---|
| instructions | 1.12e9 | 1.11e9 |
| cycles | 3.14e8 | 9.21e8 (2.9x) |
LLC misses (cache-misses) |
2.31e5 | 2.47e6 (10.7x) |
Same instructions, 2.9x the cycles: the program does the same work but waits on memory. perf record samples per round, 3-round vs 24-round run: String.length 19 -> 119 (6.3x; it only walks the cells), String.append 267 -> 672 (2.5x), U32.show 15 -> 41 (2.7x).
Likely cause
The HOT chains are LIFO, and a generation is parked (heap_hand) and later reused (COLD, then the bank) in whatever order its slots were freed. Nothing restores address order, so over rounds a fresh String's cells come from slots scattered across the heap, and walking it misses cache on almost every cell. Counting in the generated C of a variant (same rows/cells shape), over 24 rounds at --threads 1: 45.5k heap_hand, 18k COLD adoptions, 27k bank pops, but only 2.3k fresh quanta, so nearly every cell comes from a recycled chain.
Plain List<U32> churn (build 2^22 in parallel, take its length, repeat) does not degrade (flat 40-46 ms at 16 threads over 20 rounds), and neither does text built from literals. It takes U32.show, whose short-lived temporaries interleave with the string cells in the chains.
Evidence it is the chain order
In the generated C of that variant, sorting the chain by address inside heap_hand before it becomes COLD makes the curve flat:
t=1 baseline: 121 117 118 129 132 145 162 180 206 229 283 281 319 341 361 394 398 382 428 405 422 418 441 429
t=1 sorted: 518 668 674 671 665 679 670 677 673 675 671 677 676 680 676 695 688 674 679 684 684 681 673 679
t=4 baseline: 65 69 73 87 104 127 153 186 224 262 306 344 378 434 461 528 547 615 647 743 736 805 837 870
t=4 sorted: 324 357 354 370 365 368 363 367 358 364 378 369 373 379 372 375 376 377 368 380 385 384 382 381
The qsort itself is far too slow to be the fix (~0.5 s a round), and sorting only every 16th hand-off bounds the growth (t=4: 90 -> 213 ms) at a ~40% cost on a fresh run, so I'm not sending a PR. An allocator that keeps a page's slots together (per-page free lists, as in mimalloc's free-list sharding) would probably get the flat curve without the sort, but that is a design call for you.
Why it matters: this is the shape of any long-running Bend program (an App.run loop, a server, a renderer writing frames), and it shows up after a few seconds.
The file
import Base
### one "frame": 2^9 rows x 768 cells of number text, rows built in parallel
def cell(+x: U32, +y: U32) -> String:
U32.show(x) ++ " " ++ U32.show(y) ++ "\n"
def row(n: Nat, +x: U32, +y: U32) -> String:
match n:
case 0n:
""
case 1n+p:
cell(x, y) ++ row(p, (x + 1 : U32), y)
def rows(+d: Nat, +y0: U32) -> String:
match d:
case 0n:
row(768n, 0, y0)
case 1n+p:
+half = (1 << p : U32)
a b = rows(p, y0) rows(p, (y0 + half : U32))
a ++ b
### the same pure work 24 times; prints ms per round
def loop(n: Nat) -> IO(Unit):
match n:
case 0n:
IO.pure(Unit, Unit{})
case 1n+p:
do IO<Unit>:
t0 : Nat <- IO.now()
IO.write(Nat.show(String.length(rows(9n, 0))) ++ ":")
t1 : Nat <- IO.now()
IO.print(Nat.show(Nat.sub(t1, t0)))
loop(p)
def main() -> IO(Unit):
loop(24n)bend --version
2.0.26
uname -sm
Linux x86_64 (AMD Ryzen 7 7700, 8 cores / 16 threads)
clang --version
clang version 22.1.8
Activity
oxura commentedon Sep 23, 2026
Reproduced on Linux x86_64 (i7-11700, clang 22) using the issue's 24-round program. With
--threads 4 --gpu off, the first round took 61 ms and the last 1013 ms; each round still returned length 3004928.I tested whether smaller free generations would preserve page locality without a new allocator structure. In the generated C only, I changed the CPU
KEEP_WORDSvalue from 32768 to 256, 512, 1024, and 4096. The first→last round times at 4 threads were respectively 167→1329, 113→1285, 95→1297, and 76→1202 ms (baseline 61→1013 ms). All four made total performance worse. This was an experiment, not a source patch; page-sized generations alone do not resolve the address-order degradation.oxura commentedon Oct 9, 2026
The degradation is still present on main
8578b7cin a new variant (512 rows × 640 cells, numeric offsets starting at 1000, 32 rounds): every round returns length 3276800; native--threads 4 --gpu offgoes from 78 ms to 1313 ms. This is a new consumer variant, not another confirmation run of the original report.I tested generated-C-only controls; no repository source patch or claimed fix:
These are single exploratory, noninterleaved runs, not a qualified benchmark or a claim of portable speedup. The radix scratch is fixed-size stack storage, not dynamically allocated. It supports the report's locality diagnosis, but sorting/reversing generations is not yet an acceptable production proposal.
@VictorTaelin: the report explicitly leaves per-page free-list sharding as an allocator design call. Is that the direction you want explored? The current compiler is 63905 / 64000 ttok (95 tokens free), so a genuine per-page lifetime/recycling implementation needs an explicit integration/budget direction rather than moving runtime code out or deleting unrelated code to evade the cap. I will not change the cap or ship the slower sorting control as the fix.
MattCozendey commentedon Oct 13, 2026
Another data point, from a real long-running program rather than a loop: a linter for Bend, written in Bend, where one native process lints many files in turn. For each file it receives a check's answer over TCP (a 10 KB–3.5 MB text dump), splits it into fields, and decodes it into syntax trees and type facts. The work per byte is the same for every file.
In one process linting 45 files, cost per byte of answer (
IO.nowaround each stage):The working set stayed between 106 and 133 MB after the first large check, so it is not a leak.
A controlled version: receiving and splitting the same 2.4 MB answer took 24–54 ms in a fresh process, and 203–226 ms in a process that had first decoded two copies of that answer and kept the results alive.
We did not count cache misses, but the shape matches the diagnosis here: the same work costs more the more the process allocated and freed before it.
Workaround: we lint each file in a child process (
Process.run, so a fresh heap), several at once. A warm run of the 45 files went from 14.4 s to 2.4 s; part of that is parallelism, but the per-byte growth is gone.Setup: Windows 11 x64, native build of a fork (
feature/cuda-and-windowsatf23c62bf). Its host free chains,heap_handand bank are the same asmainat213cd5e; the fork only adds#if BEND_SPLIT/TWIN_SPLITbranches for its CUDA heap. Not re-run onmainitself.lmtriet commentedon Nov 5, 2026
Same slowdown here on Bend 2.0.39, C backend, on a much larger and longer-lived process of HTF quant trading. Numbers below in case they help size the problem; they are from a market-data loader that processes one day per iteration over a window of 5,974 days, with a checkpoint written at the end of each day.
Symptom. A day that takes 10 s in a fresh process takes 85 s once the process has run about 200 days. On a 60-day window the same curve shows up already: iterations 2-5 run at about 3.5 instructions per
cycle, iterations 51-59 at 1.5-1.8. Memory stays flat the whole time (resident set 5.2-5.4 GB aged, 4.0-5.1 GB fresh; the long run peaked at 7.4 GB), and the output files are byte-identical, so no extra work is
being done.
Counters for one and the same day (2010-11-12), measured with hardware counters:
Same instruction count, 8.6× the cycles, same clock (3.13 vs 3.17 GHz): the aged process is waiting on memory.
+RTS --threads 1 -RTSmakes no difference (measured side by side on the 60-day window, equaltimings and equal instructions-per-cycle).
What we read in the emitted C, which matches the cause described in this issue:
heap_freepushes the freed block on the head of the lane's chain andheap_allocpops the head, so reuse islast-in-first-out;
heap_handparks a chain once it reachesKEEP_WORDS(32,768 words) and banks the old parked one;heap_alloc_misstakes the parked chain, then a banked chain, then fresh pages. Only freshpages come out in address order. Day after day the reused blocks lose address order, and the per-day lists end up scattered over a ~5 GB heap.
Workaround we settled on, same idea as the subprocess-per-file comment above: run the window as segments of 20 days, each a fresh process resumed from the last checkpoint. Over 157 days this gave 9.0 s/day
instead of 38.8 s/day in the single process, with every parquet file, every checkpoint and the joined log byte-equal to the single-process run. The live 16-year run is on this now: segments of 21 days at 8.4-9.4
s/day with a peak resident set of 6-9 GB, against 48 s/day on average (up to 99 s on heavy days) before the switch.
We did not try the address-sort or FIFO variants from this thread; the qsort cost reported here would be paid on every parked chain of a ~5 GB heap, so we assume it would not scale for us.
fix(comp): a host miss reuses banked cells in address order (#1007)
refactor(comp): a smaller free map