Repository navigation
A match tests a later enumeration first and does not open a node every arm re-forms - #2211
Draft
nicolas-abril wants to merge 4 commits into
Draft
nicolas-abril wants to merge 4 commits into
nicolas-abril wants to merge 4 commits into
Conversation
…then re-formed, and a match whose arms all re-form its node is no match Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ger, a guard on the selector's type, and a test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…reflowed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 68,120 ttok, 2,120 over the 66,000 cap (main 65,995), sorepo-gatefails until that is settled. Measured on one Mac (M4) under heavy load from other jobs (load average 7 to 25 throughout), not on the cluster: thetestandperfgates have not been run. Instruction counts are the measure here; cycle figures are marked where they come from a placement sweep.What it does
Bend has no
ifand cannot match on a local, so checked code that compares a key and then descends a tree passes the comparison as a parameter and matches it together with the tree, with one level of lookahead (Base'sMapdoes this; so does the checkedrbtreeport of the Perceus set):bend.ts'match_flattencompiles the rows into a tree of lambda-matches in binder order: a constructor column decides at its binder's position, its fields take the binder's place, and "a ctr row makes its column strict even under an earlier catch-all". The termination rule wants the shrinking parameter first, so the source cannot putofirst. The tree the emitter receives therefore openst, thent.l(five loads, and the node taken), thent.r(the same), and only then testso; on theLTpath the opened right child is re-formed, onGTthe left one, onEQboth. On main every such open of an owned node frees its cell before the recursive call and allocates a new one after it.The emitter is free to do otherwise without touching
bend.ts: at every match it holds all the pending arguments as already-evaluated values (emit_body'sargs), so it can test a later word argument first, and it can decline to open a box whose arms all re-form it. Two rules inemit_match, nothing else changed:mat_eta). If each constructor arm uses its fields only to rebuild that same node (arm_self), the rebuilt node is used at most once on every path (exactly once if the node is owned), and the arms are then the same term (term_same; a field-less arm compared with the others' body at that constructor, a default arm applied to the node), the node is bound whole and the body emitted once. An arm that opens with a match on its first field, an enumeration (a node's colour), counts as one arm per value.mat_cols,mat_pick) when the match is on a box and, for some value of a later argument that is an enumeration (aBool, aCmp, any type of field-less constructors) or an enumeration field of a one-constructor record argument (Map'sString & Bool), the match would then be no match by rule 1. The emitter tests that word first and emits, per value, the tree with that value's arm taken at every test of it (mat_pick: positions follow the arguments each arm consumes, erased binders consume none, a word /Array/IO.OPmatch ends the walk, a test of another type is left alone). Inside each copy rule 1 then removes the opens no row of that value reads.insabove becomes: testt, its colour,o; onLTopen onlyl, onGTonlyr, onEQneither. The decision per match is memoised per match, lent-ness and argument layout.Ownership and frees, before and after
For a removed open of node
x, on each path:rbtreeon main): before,x's fields are loaded, its cell becomes the arm's spare (or is freed), and the re-form allocates (taking the spare) and stores the same fields. After,xis passed on; nothing is loaded, freed or allocated.ctr_takefrees the count cell (count 1) or copies the node and drops one count (shared), and the re-form allocates and seals a node holding the same fields. After, the node and its count cell are passed on unchanged.val_ownrecords). After, the lent node is passed where the rebuilt one was: a lend to a borrowing callee, or an own fact at an owning position, by the sameval_ownrules as any lent variable. A lent node may also be used on no path (a read-only lookup that ignores the child), which costs nothing; an owned node must be used on every path, so no genericterm_sinkreplaces an inline free.In every case the values reachable from the result are the same; what goes away is a take and a rebuild of the same node (and, for a shared node, a copy). No value is dropped on a path where it was not dropped before, which is why the rule requires the node used on every path when owned. The live heap word count at exit matches main on every program whose C changes (below).
Reach
Over every program that builds among the tests, the demos,
bench/runtime, the 22 koru kernels and the Perceus ports (979 programs, 991 more do not build an executable): 35 emit different C, through 32 defs:Map.ins.go,Map.get.go,Map.has.go,Map.pop.go,Map.upd.go(both rules),String.contains.pre,String.order.go(both),List.merge.go(its lent copies; rule 1 only).hashmap(has),tree-bitonic(flow,warp_zip),tree-radix(merge), all rule 1.textstats(throughString.contains.pre).pure_hvm5_mini(Map,str_find,str_skip,str_word),io_rollback_netcode/server(Map).rbtree,rbtree-ck(insboth rules, the balance defs rule 1), the@unsafespellings (balance defs),deriv(add,mul).Measured
One thread, GPU off, instructions retired (least of 3 interleaved runs; the noise is about 0.1%):
rbtree(checked)rbtree-ck(checked)rbtree-unsaferbtree-ck-unsafederivhashmaptree-radixtree-bitonictextstats(kernel)Other 14 benches,
cfold,nqueensand 21 kernels: identical C.The bound written by hand before this (the selector-first spelling's
insand its continuations, from an@unsafecopy of the port withofirst, transplanted into the checked build's C, parameters swapped back):rbtree21.89G to 13.09G on main and 14.67G to 11.63G on #2207. This change goes past it (12.15G, 11.28G) because rule 1 also removes re-forming opens inbalance_left/balance_right.rbtree-ck's counts include kernel work that grows under memory pressure (main read 74.1G to 81.1G across sessions on the same binary); the figures above are from the quietest session for each.Cycles, checked
rbtree, 16 placements of the segments (4-byte steps), 2 interleaved rounds, least / median / most (load average 9 to 10, so wide):instransplanted)On main the change's cycles sit above the hand bound's at the normal stride and level with it at the odd one: main's checked
rbtreeis the one program with the L1D set conflicts described in section 25's cause 4, and moving allocations changes how hard they hit. With them out of the way (odd stride) the change takes main from 5.14G to 3.64G median (1.41x); on #2207, which has no such conflicts, 3.10G to 2.61G (1.19x).Checks
-DBEND_POISONand a count of live heap words at exit (heap_alloc/heap_freepatched): same output, exit code and live count (0) on every one, at one thread and at eight.#2207 + thisagainst Datatype nodes: an arm that re-forms its node keeps it whole, the counted take and wrap inline, a recursive drop, and flat defs lent a box #2207: same on all 36, at one thread and at eight (the new test faults the same way on both: see the end).io/argsandio/args_bytesfail the same way on main and here: they want arguments).tests/base/map_leaf_eq(the one changed test with a!call) prints its#|lines with the GPU on.term_keep), an owned node dropped whole (term_sink), every node treated as lent, a default arm left unpruned. Not caught (6): position errors inmat_pick(fields not counted, erased binders counted, a default shifted, a binder treated as a test), the record field index one off, and the type guard removed; in every test program these make the rule fire less, never wrongly, since a wrong position meets no test of the selector's type. The field-index error would be unsound on a record whose two fields are enumerations of one type when pruning one field's test by the other's values leaves the match with nothing to open; no program here has that shape and the test does not catch it.tsc(strict, oncomp.tsalone): no new errors.tests/run/match_columns.bend(interpreter 14 s): every shape above, on owned, lent and shared trees, met at run time on leaves and nodes.rbtree,rbtree-ckand their@unsafespellings at 3,000 inserts,derivat 6): interpreter, JS and C (1 and 8 threads, poisoned, live count 0) equal, on main and on Datatype nodes: an arm that re-forms its node keeps it whole, the counted take and wrap inline, a recursive drop, and flat defs lent a box #2207.Costs
comp.ts+2,125 ttok.Map(tests/base/map_ops0.25 s to 0.40 s,pure_hvm5_mini0.50 s to 0.60 s). The cost is the arm comparisons (term_same) repeated at each level of a deep match; memoising per match and building the picked trees eagerly took it from about 2x to about 1.5x.Not covered
Array) is left as it is past that match.t0 _beside rows that open a record) is not the same term, so such a match is kept.Bool & Bool & U32as a tuple of a tuple) is not a candidate; a flat record is.term_samedoes; an argument whose type depends on the scrutinee's constructor would take the first arm's annotations.Stacks cleanly on #2207 (branch
opt/match-columns-stackedon nicolas-abril/bend: this diff on #2207's tip, the duplicatepath_useremoved; 69,551 ttok). Found on the way, in #2207 itself (not this change): a type with an erased field before its children (EN{-n: Nat, l: E, b: Bool, k: U32, r: E}) with the lookahead insert above faults on #2207 (memory fault, also with-DBEND_POISON) and runs on main; the arm that keepslwhole sinks it withterm_sinkand then stores its fields. Reproducer (38 lines):🤖 Generated with Claude Code