Repository navigation
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
Closed
nicolas-abril wants to merge 1 commit into
nicolas-abril wants to merge 1 commit into
Conversation
… 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]>
This was referenced Nov 22, 2026
Collaborator
Author
|
Moved to nicolas-abril#16, which keeps it saved off the main queue until it can be accepted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
comp.tsis 66,074 ttok, 74 over the 66,000 cap (main is 65,966; this adds 108), sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates 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 onlylexergains (-2.5%);tree-bitonicruns 1.2% more of them, by where its code lands (see Cycles).Independent of the other open drafts: it branches from main
f0cf0a7cand touches only the allocator's count. It was found while breaking down whatlexerloses 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_freeadds to the count and hands the chain off when it reachesKEEP_WORDS.KEEP_WORDSis a static the host sets once at start (CHUNKwith a GPU,CAP_WORDSwithout), 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 isALC_NONE(-KEEP_WORDSon 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.miin place ofadd,str,adrp,ldrb,cmp,b.ne,cmp,b.hs).heap_handandheap_alloc_missset the count fromALC_NONE.corpus_setupgives every lane's row its start, after it setsKEEP_WORDS.heap_allocis unchanged, and so is the device: thereALC_NONEis 0, the test is compiled out as before, anddev_cutreads 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 17bench/runtimeprograms were built with main and with this branch and run; every one prints the same result.bfseditdistgameoflifehashmaphistogramkmeanslexermandelbrotmerklenbodyqueensraytracesymregterraintree-bitonictree-matmultree-radixNo 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.lexer(3 rounds)tree-radix(1)kmeans(1)hashmap(1)tree-matmul(1)tree-bitonic(2)tree-bitonicruns 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 twonops after the count's store inheap_freeit 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 onlexergives 8.11G to 8.28G, under main's 8.38G every time, solexer's gain is the work removed. Branch mispredictions ontree-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:heap_allocresult, 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.tree-bitonichands off 272,015 times at 4 threads. (The 900th isheap_large_reuse, whose private lane rows that build cannot index.)-DBEND_POISONand a count of live heap words at exit: the same output and the same count as main.tests/io/heap_large_reuse.creads 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 atALC_NONEand tests for that. It passes with the GPU off and on (KEEP_WORDSisCHUNKthere).tests/run/heap_generations.bendfrees 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 isround!) and in JS.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.--gpu on.repo.tsfails only on the ttok cap.Not covered
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,lexerruns 47.50G instructions (-11%); this collects 2.54G of that 5.91G.🤖 Generated with Claude Code