Skip to content

perf(c): a host lane's chain count is kept less KEEP_WORDS, so a free tests its sign - #2161

Closed
nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/free-count
Closed

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/free-count

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 66,074 ttok, 74 over the 66,000 cap (main is 65,966; this adds 108), 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 buys: fewer instructions on six benches (tree-bitonic -8.1%, tree-radix -6.2%, lexer -4.8%), none more on any. In cycles only lexer gains (-2.5%); tree-bitonic runs 1.2% more of them, by where its code lands (see Cycles).

Independent of the other open drafts: it branches from main f0cf0a7c and touches only the allocator's count. It was found while breaking down what lexer loses to its C twin, where a line of source is about 60 nodes allocated and 60 freed.

What it does

A lane keeps, per size class, a free chain and the number of words in it. heap_free adds to the count and hands the chain off when it reaches KEEP_WORDS. KEEP_WORDS is a static the host sets once at start (CHUNK with a GPU, CAP_WORDS without), so every free loaded it and compared twice: six instructions on arm64 after the add.

Now the host keeps the count less KEEP_WORDS: an empty chain's count is ALC_NONE (-KEEP_WORDS on the host, 0 on the device), and the hand-off test is the sign of the count after the add, which the add's flags already hold. A free on arm64 goes from 12 instructions to 8 (adds, str, b.mi in place of add, str, adrp, ldrb, cmp, b.ne, cmp, b.hs).

  • heap_hand and heap_alloc_miss set the count from ALC_NONE.
  • corpus_setup gives every lane's row its start, after it sets KEEP_WORDS.
  • heap_alloc is unchanged, and so is the device: there ALC_NONE is 0, the test is compiled out as before, and dev_cut reads the count as it did.

Nothing about the allocator's behaviour changes: the same chains, the same generations handed off at the same frees, the same address from every heap_alloc. That is what the checks below test.

Results

One M4, one thread, GPU off, instructions retired from /usr/bin/time -l, the least of three interleaved runs. The runtime is part of every program's C, so all 17 bench/runtime programs were built with main and with this branch and run; every one prints the same result.

bench main, instructions this
bfs 28.746G 28.742G -0.02%
editdist 49.777G 49.776G -0.00%
gameoflife 172.349G 172.349G +0.00%
hashmap 18.496G 18.187G -1.67%
histogram 44.369G 44.369G +0.00%
kmeans 41.577G 40.600G -2.35%
lexer 53.407G 50.870G -4.75%
mandelbrot 43.446G 43.446G +0.00%
merkle 47.321G 47.304G -0.04%
nbody 39.787G 39.787G -0.00%
queens 89.816G 89.816G +0.00%
raytrace 82.953G 82.953G +0.00%
symreg 50.767G 50.732G -0.07%
terrain 44.568G 44.568G -0.00%
tree-bitonic 148.712G 136.634G -8.12%
tree-matmul 62.582G 61.614G -1.55%
tree-radix 13.791G 12.940G -6.17%

No program runs more instructions.

Cycles: the gain in time is lexer's alone. The six instructions a free no longer runs were a load that always hits and two compares that are always predicted, so they cost little time, and on this machine where the code lands moves a run by a percent or two on its own. So each bench whose instruction count changed was built at 16 placements (the segments shifted in 4-byte steps), every placement run one to three times, alternating with main; the table has the least, the median and the most of the per-placement minimum, in cycles.

bench main: least / median / most this: least / median / most median
lexer (3 rounds) 8.398G / 8.449G / 8.938G 8.141G / 8.236G / 8.649G -2.5%
tree-radix (1) 15.336G / 15.888G / 16.308G 15.423G / 15.699G / 15.841G -1.2%
kmeans (1) 8.274G / 8.285G / 8.321G 8.270G / 8.282G / 8.311G -0.0%
hashmap (1) 9.526G / 9.683G / 9.941G 9.525G / 9.671G / 9.896G -0.1%
tree-matmul (1) 8.144G / 8.197G / 8.244G 8.199G / 8.273G / 9.395G +0.9%
tree-bitonic (2) 20.317G / 20.373G / 20.605G 20.486G / 20.622G / 20.740G +1.2%

tree-bitonic runs 8% fewer instructions and 1.2% more cycles, at every placement. That loss follows the code's offsets inside the functions (which shifting whole segments cannot vary), not the test: with two nops after the count's store in heap_free it runs 142.0G instructions in 20.32G cycles, main's speed; with one, 20.52G; with three, 20.48G; with five (main's instruction count), 20.37G. Marking the hand-off unlikely (__builtin_expect), which moves its call out of line, gives 20.51G / 20.70G / 20.74G, so that is not the cause and it is not in this change. The same padding on lexer gives 8.11G to 8.28G, under main's 8.38G every time, so lexer's gain is the work removed. Branch mispredictions on tree-bitonic (Instruments' CPU counters) go from 99.1M to 92.4M.

editdist's instruction count does not change. A plain run of it is 20% faster here, which is main's binary sitting on the one bad placement in 16 (10.62G against 8.45G at the other 15, in both builds): not a gain.

Checks

All local, against main f0cf0a7c:

  • The allocator behaves the same. Each program is built with both runtimes and a hash over every heap_alloc result, every free, every hand-off and every miss, in order; at one thread that is deterministic. Of the 883 tests that pass on main and the 17 benches at full size, 885 give the same hash. The other 15 are IO tests (files, sockets, processes) whose hash changes from run to run on main itself; nine vary within three runs, and six runs of each of the other six on both runtimes give hashes from the same set. 25 programs hand chains off at one thread, 56,472 times in all.
  • The count, at any thread count. A build that keeps main's plain count beside the new one and aborts if the two ever disagree, or if a hand-off happens when main's rule would not hand off (or the reverse): 899 programs at 1, 4 and 8 threads, no abort. tree-bitonic hands off 272,015 times at 4 threads. (The 900th is heap_large_reuse, whose private lane rows that build cannot index.)
  • Memory: each of the 900 with -DBEND_POISON and a count of live heap words at exit: the same output and the same count as main.
  • Native lane: every test that passes on main passes here, the GPU off; the 22 that do not pass on main in this local lane end the same way.
  • tests/io/heap_large_reuse.c reads the allocator's cells: it built its private lane rows as zeros and tested a count for 0. It now starts a row's counts at ALC_NONE and tests for that. It passes with the GPU off and on (KEEP_WORDS is CHUNK there).
  • New test: tests/run/heap_generations.bend frees several generations of 2-word, 4-word and 8-word nodes on one lane (by a walk and by a drop), builds again from what was handed off, frees blocks of 2^16 words, and forks so that one lane frees what another allocated. 620 hand-offs at one thread. It passes on the C lane at 1 and 8 threads, on Metal (its last round is round!) and in JS.
  • Wrong versions. A miss that sets the plain count, a hand-off that resets to 0 and a bias off by two words each change the hash on the new test; the first two also fail heap_large_reuse. A lane row left at zero passes at one thread (a lane's first event in a class is a miss, which sets the count) and aborts the shadow build at 8 threads, where a lane frees what another allocated. So the new test alone does not prove the count; the hash and the shadow build do.
  • Metal: all 17 benches print the same results with --gpu on.
  • repo.ts fails only on the ttok cap.

Not covered

  • Cluster gates, CUDA, Linux and x86 have not run.
  • heap_alloc's side of the count (a load, a subtract and a store per allocation) is still there. With no count at all, by hand edit of the emitted C, lexer runs 47.50G instructions (-11%); this collects 2.54G of that 5.91G.

🤖 Generated with Claude Code

… tests its sign

heap_free compared the lane's word count with KEEP_WORDS, a static the
host sets at start, so every free loaded it and compared twice (six
instructions on arm64). The count now starts at ALC_NONE, which is
-KEEP_WORDS on the host and 0 on the device, and the hand-off test is
the sign of the count after the add. heap_hand and heap_alloc_miss set
the count from ALC_NONE; corpus_setup gives every lane's row its start.
The chains, the generations and the order of every allocation are
unchanged.

tests/io/heap_large_reuse.c builds its private lane rows with the new
start. tests/run/heap_generations.bend frees several generations of
three classes on one lane and across forked lanes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

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

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