Skip to content

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
bendlang:mainfrom
nicolas-abril:opt/match-columns
Draft

nicolas-abril wants to merge 4 commits into
bendlang:mainfrom
nicolas-abril:opt/match-columns

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 68,120 ttok, 2,120 over the 66,000 cap (main 65,995), so repo-gate fails 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: the test and perf gates 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 if and 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's Map does this; so does the checked rbtree port of the Perceus set):

def ins(t: Tree, o: Cmp, +k: U32, +v: Bool) -> Tree:
  match t o:
    case Node{Red{}, Node{c, a, +ky, vy, b}, kx, vx, r} LT{}:
      Node{Red{}, ins(Node{c, a, ky, vy, b}, U32.cmp(k, ky), k, v), kx, vx, r}
    case Node{Red{}, l, kx, vx, Node{c, a, +ky, vy, b}} GT{}:
      Node{Red{}, l, kx, vx, ins(Node{c, a, ky, vy, b}, U32.cmp(k, ky), k, v)}
    ...

bend.ts' match_flatten compiles 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 put o first. The tree the emitter receives therefore opens t, then t.l (five loads, and the node taken), then t.r (the same), and only then tests o; on the LT path the opened right child is re-formed, on GT the left one, on EQ both. 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's args), 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 in emit_match, nothing else changed:

  1. A match whose every arm re-forms the node is no match (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.
  2. A later enumeration is tested first (mat_cols, mat_pick) when the match is on a box and, for some value of a later argument that is an enumeration (a Bool, a Cmp, any type of field-less constructors) or an enumeration field of a one-constructor record argument (Map's String & 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.OP match 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.

ins above becomes: test t, its colour, o; on LT open only l, on GT only r, on EQ neither. 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:

  • owned, unique, uncounted (checked rbtree on 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, x is passed on; nothing is loaded, freed or allocated.
  • owned, counted (a hot type): before, ctr_take frees 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.
  • lent: before, the fields are peeked and the re-form builds a new owned node from lent fields (so the def owns its parameter, as val_own records). 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 same val_own rules 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 generic term_sink replaces 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:

  • Base (8): 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).
  • Benches: hashmap (has), tree-bitonic (flow, warp_zip), tree-radix (merge), all rule 1.
  • Kernels: textstats (through String.contains.pre).
  • Demos: pure_hvm5_mini (Map, str_find, str_skip, str_word), io_rollback_netcode/server (Map).
  • Perceus ports: rbtree, rbtree-ck (ins both rules, the balance defs rule 1), the @unsafe spellings (balance defs), deriv (add, mul).

Measured

One thread, GPU off, instructions retired (least of 3 interleaved runs; the noise is about 0.1%):

program main this #2207 #2207 + this
rbtree (checked) 21.89G 12.15G -44.5% 14.67G 11.28G -23.1%
rbtree-ck (checked) 74.15G 47.72G -35.6% 30.59G 26.89G -12.1%
rbtree-unsafe 11.24G 11.19G -0.4% 10.42G 10.40G -0.3%
rbtree-ck-unsafe 49.15G 48.96G -0.4% 25.46G 25.53G +0.3%
deriv 12.293G 12.299G +0.05% 10.886G 10.896G +0.09%
hashmap 18.501G 18.314G -1.0% 14.783G 14.612G -1.2%
tree-radix 13.829G 13.510G -2.3% 13.722G 13.353G -2.7%
tree-bitonic 148.74G 148.16G -0.39% 148.64G 148.16G -0.33%
textstats (kernel) 73.32M 73.30M 0 73.03M 73.07M 0

Other 14 benches, cfold, nqueens and 21 kernels: identical C.

The bound written by hand before this (the selector-first spelling's ins and its continuations, from an @unsafe copy of the port with o first, transplanted into the checked build's C, parameters swapped back): rbtree 21.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 in balance_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):

build instructions cycles
main 21.85G 8.68 / 8.90 / 9.73G
this 12.12G 6.23 / 6.44 / 6.94G
hand bound (selector-first ins transplanted) 13.06G 5.49 / 5.68 / 6.09G
main, fresh cells 9 words apart 21.87G 5.00 / 5.14 / 5.70G
this, 9 words apart 12.12G 3.44 / 3.64 / 4.14G
hand bound, 9 words apart 13.06G 3.46 / 3.63 / 4.06G
#2207 14.64G 2.89 / 3.10 / 3.57G
#2207 + this 11.25G 2.46 / 2.61 / 3.05G
#2207 + hand bound 11.60G 2.48 / 2.64 / 3.17G

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 rbtree is 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

  • Memory: the 35 programs and the new test, emitted with main and with this, built with -DBEND_POISON and a count of live heap words at exit (heap_alloc / heap_free patched): same output, exit code and live count (0) on every one, at one thread and at eight.
  • The same memory check for #2207 + this against 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).
  • Native and JS lanes on the 22 changed tests and the new one: no status differs from main (io/args and io/args_bytes fail the same way on main and here: they want arguments).
  • Interpreter, JS and C (1 and 8 threads, poisoned, live count) on five adversarial programs (an owned, a lent and a shared tree; a selector computed from the tree; arms reading a child on one value and keeping it on another; default rows; the node's colour matched first; three-deep patterns; an erased field before the children; a record of two enumerations; a tuple selector; lists; arms that re-form the node but answer differently; a node re-formed twice or dropped): equal everywhere.
  • Metal: tests/base/map_leaf_eq (the one changed test with a ! call) prints its #| lines with the GPU on.
  • Wrong versions: 18 edits of the rule, each run against the adversarial programs and the new test (C at 1 and 8 threads with poisoned frees and the live count, and JS). Caught (8): a pick that takes the first arm, a record field one off, no arm comparison, field-less arms not compared, the default arm not compared, the colour split re-forming the wrong colour, the hoisted test's values reversed, the hoisted copies not pruned (the emitter recurses until the stack overflows). Sound and only slower or larger (4): a node used twice let through (term_keep), an owned node dropped whole (term_sink), every node treated as lent, a default arm left unpruned. Not caught (6): position errors in mat_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, on comp.ts alone): no new errors.
  • New test 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.
  • Five small Perceus ports (rbtree, rbtree-ck and their @unsafe spellings at 3,000 inserts, deriv at 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.
  • Emit time: unchanged on the 17 benches (2.97 s against 3.07 s in total, noise); +0.1 to +0.2 s (1.4x to 2.1x) on programs that use Map (tests/base/map_ops 0.25 s to 0.40 s, pure_hvm5_mini 0.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

  • A match that tests the selector only after a match on another argument that is not a box (a word, an Array) is left as it is past that match.
  • Rule 1 compares arms structurally: an arm with a match on another argument that the other arms bind as a plain variable (a default row t0 _ beside rows that open a record) is not the same term, so such a match is kept.
  • An enumeration nested two records deep (Bool & Bool & U32 as a tuple of a tuple) is not a candidate; a flat record is.
  • Dependent types: arms are compared with their type annotations ignored, as term_same does; an argument whose type depends on the scrutinee's constructor would take the first arm's annotations.
  • Measured on an M4 under load; x86, Linux, CUDA, the cluster gates not run.

Stacks cleanly on #2207 (branch opt/match-columns-stacked on nicolas-abril/bend: this diff on #2207's tip, the duplicate path_use removed; 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 keeps l whole sinks it with term_sink and then stores its fields. Reproducer (38 lines):

import Base

type E is Data:
  EL{}
  EN{-n: Nat, l: E, b: Bool, k: U32, r: E}

def edig(t: E, acc: U32) -> U32:
  match t:
    case EL{}:
      U32.add(U32.mul(acc, 29), 5)
    case EN{n, l, b, k, r}:
      edig(r, U32.add(U32.mul(edig(l, U32.add(U32.mul(acc, 29), Bool.to_u32(b))), 137), k))

def eins(t: E, o: Cmp, +k: U32) -> E:
  match t o:
    case EL{} _:
      EN{0n, EL{}, True{}, k, EL{}}
    case EN{n, EN{m, a, b2, +ky, c}, b, kx, r} LT{}:
      EN{n, eins(EN{m, a, b2, ky, c}, U32.cmp(k, ky), k), b, kx, r}
    case EN{n, EL{}, b, kx, r} LT{}:
      EN{n, EN{0n, EL{}, False{}, k, EL{}}, b, kx, r}
    case EN{n, l, b, kx, EN{m, a, b2, +ky, c}} GT{}:
      EN{n, l, b, kx, eins(EN{m, a, b2, ky, c}, U32.cmp(k, ky), k)}
    case EN{n, l, b, kx, EL{}} GT{}:
      EN{n, l, b, kx, EN{0n, EL{}, True{}, k, EL{}}}
    case EN{n, l, b, kx, r} EQ{}:
      EN{n, l, Bool.not(b), kx, r}

def ebuild(+n: Nat, t: E) -> E:
  match n:
    case 0n:
      t
    case 1n+m:
      +x = U32.mod(U32.mul(U32.from_nat(n), 2654435761), 1000)
      ebuild(m, eins(t, U32.cmp(x, 500), x))

def main() -> U32:
  edig(ebuild(5n, EL{}), 2)

🤖 Generated with Claude Code

nicolas-abril and others added 4 commits November 19, 2026 02:18
…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>
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