Skip to content

perf(c): term_drop frees a node whose only box is its last field as it walks - #2151

Closed
nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/hashmap-rest
Closed

nicolas-abril wants to merge 1 commit into
bendlang:mainfrom
nicolas-abril:opt/hashmap-rest

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 66,105 ttok, 105 over the 66,000 cap (main is 65,962; this adds 143), 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.

Independent of #2135 and #2134: it branches from main and touches only term_drop. It was found while breaking down what hashmap still loses to its C twin on top of #2135, where the final drop of the table is half of the instructions left.

What it does

term_drop walks any value with one general loop: a node joins the cur chain, its fields are stepped one at a time, and it is freed on the way out. A list of words (a String, a List<U32>, a chain of keys) pays that for every node, over 100 instructions each.

Now a constructor node whose fields are all trivial but the last is freed at once and the walk goes on with that last field, never touching cur. The test is on the node's words at run time, so it holds for any type, and a node that fails it takes the general path as before. Nothing else changes:

  • Order: the node is freed before its last child is walked, which is the order the general path already gives such a node.
  • Shared cells: the walk goes back to the top of the loop for each node, so a count cell met on the way is counted down there as before, and the walk stops at a cell others still hold.
  • Device: the new path polls err_spun like the general loop does.

Results

One M4, one thread, GPU off. Instructions retired and cycles are the best of interleaved runs; the machine was shared with other benchmarks, so read the cycles as rough and the instructions as exact.

program before after
hashmap on main, instructions 18.50G 16.72G -9.6%
hashmap on main, its final drop alone 5.05G 3.18G -37%
hashmap on #2135, instructions 8.09G 6.22G -23%
hashmap on #2135, cycles 4.60G 4.18G -9%
hashmap on main, cycles (medians of 14 alternating runs, machine loaded) 11.27G 10.80G -4%
build a 1M-node list of words and drop it, 200 times: instructions 26.46G 13.64G -48%
the same, cycles 5.62G 2.85G -49%
a List<U32> of 1M and a String of 1M chars built and dropped, 100 times: instructions 37.46G 24.65G -34%

On #2135 the chains in the table are no longer shared, so the drop is a larger part of what is left and the change is worth more there.

Bench suite: 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.75G 28.76G +0.02%
editdist 49.78G 49.78G +0.00%
gameoflife 172.36G 172.36G -0.00%
hashmap 18.50G 16.71G -9.68%
histogram 44.37G 44.37G +0.00%
kmeans 41.61G 41.60G -0.00%
lexer 53.43G 53.43G -0.00%
mandelbrot 43.48G 43.48G -0.00%
merkle 47.36G 47.42G +0.13%
nbody 39.86G 39.85G -0.02%
queens 89.89G 89.89G -0.00%
raytrace 83.03G 83.04G +0.01%
symreg 50.81G 50.80G -0.03%
terrain 44.60G 44.62G +0.05%
tree-bitonic 148.84G 148.85G +0.01%
tree-matmul 62.61G 62.61G +0.00%
tree-radix 13.86G 13.80G -0.39%

No program runs more than 0.13% more instructions (merkle; a node that fails the new test still pays for the test). Cycles on this shared machine moved by several percent either way between runs of the same binary, so they are not listed; merkle alternated six times gave 20.47G on main and 20.78G here at best, with overlapping spreads.

Checks

All local, against main 011b54d3:

  • Native lane: every test that builds and passes on main (882) passes here, the GPU off; the 22 that do not pass on main in this local lane end the same way here.
  • Memory: each of the 882, 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 test: tests/rfc/drop_list.bend drops a list of words, a chain of one-field nodes, a list whose item is a box in every other node, a list with its box first, a node that ends in an Array of lists, lists shared between two holders, and two heads over one shared tail. With poisoned frees and the live count it fails five wrong versions of the new path that I tried on it (any node taken as a list, a wrong size class, the last field read after the free, a shared tail taken as owned, a boxed first field skipped). It passes on the C lane, on Metal with its rounds called as round!, and in bend file.bend.
  • JS: the JS emitted for that test is byte-identical to main's; the change is in the C runtime only.
  • Metal: hashmap and the new test run on the GPU and print the same results.
  • repo.ts fails only on the ttok cap.

Not covered

  • Cluster gates and CUDA have not run.
  • A node with its only box in another field (a list spelled Cons{tail, head}) takes the general path as before.
  • tsc was not run.

What is left in hashmap

On #2135 plus this, hashmap is at about 6.2G instructions against the C twin's 3.2G. A drop written for the element type (free the chain's nodes in a plain loop) would take the drop from 2.24G to 0.63G instructions; the generic walk cannot get there, since it reads the constructor table and tests each field. That needs the type at the drop site and is not attempted here.

🤖 Generated with Claude Code

…t walks

A list of words (a String, a List<U32>, a chain of keys) was dropped
through term_drop's general path: each node joined the cur chain, its
fields were stepped one at a time, and it was freed on the way out. Such
a node now goes at once: when every field but the last is trivial, the
node is freed and the walk goes on with the last field. Order of frees
is unchanged, and a shared cell met on the way goes through the count
test at the top of the loop as before.

hashmap, one thread, GPU off: 18.50G instructions to 16.71G on main;
on top of bendlang#2135, 8.09G to 6.22G. tests/rfc/drop_list.bend spells the
shapes around the new path.

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

Copy link
Copy Markdown
Collaborator Author

Moved to nicolas-abril#12, 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