Skip to content

match on U32 literal patterns emits a 32-bit decomposition tree, not a lookup table (256 arms: 3.5 GiB, 1.46 MB C; the same table on Nat: 0.24 GiB, 87 KB) #867

Closed

Description

@moorbrook

What you did

bend tbl_u32.bend -o tbl_u32.c
bend tbl_nat.bend -o tbl_nat.c

What happened

A match on U32 literal patterns lowers to a nested test over all 32 bits of the scrutinee. The same table written with contiguous Nat patterns lowers to a native lookup table. Both give the same answers, but the U32 form costs emitter memory, emission time and output size that grow with the arm count, while the Nat form stays flat.

Measured on main at 15ae0c8 (bend 2.0.16); max RSS from /usr/bin/time -l, size is the emitted .c.

arms U32 -o .c Nat -o .c
8 0.26 GiB, 133 KB 0.11 GiB, 86 KB
32 0.64 GiB, 267 KB 0.12 GiB, 86 KB
64 1.02 GiB, 442 KB 0.12 GiB, 86 KB
128 1.91 GiB, 786 KB 0.18 GiB, 86 KB
256 3.5 GiB, 1.46 MB (5 s) 0.24 GiB, 87 KB (under 1 s)
512 6.84 GiB, 2.77 MB (8 s) 0.39 GiB, 89 KB (1 s)

The JS lane shares the lowering: at 256 arms, -o .js is 1.40 GiB and 2.67 MB against 0.22 GiB and 15 KB.

At 256 arms the U32 version emits one 26,148-line function:

FAR Term spin_5(Env e, THR Term* o, u32 r0) {
  ...
  u32 s_0 = ((x_6 >> 0) & 1);
  u32 s_1 = ((x_6 >> 1) & 1);
  ...
  u32 s_31 = ((x_6 >> 31) & 1);
  if (s_0 == 0) {
    if (s_1 == 0) {
      if (s_2 == 0) {
        ...

All 32 bits are decomposed although no arm is above 255, and the if tree that follows is the whole body. The Nat version emits, for the same table:

CONSTV u64 TAB_0[] = { 11ull, 48ull, 85ull, 122ull, ... };
#define TAB_AT(T, S, I) T[S < I ? S : I]
...
v_9 = TAB_AT(TAB_0, x_6, 256);

The two sit next to each other in emit_match: comp.ts:2632 sets word for U32/F32 and comp.ts:2634 builds the scrutinee as 32 one-bit words (U32{data: Word(32n)} in base.bend really is a Word.Con chain, so the match is over that ADT and the emitter is lowering it faithfully), while comp.ts:2641 gates the table on adt.k === "Nat":

const ls = adt.k === "Nat" ? emit_nat(x) : null;
const id = emit_tab(fl, ls, all.B(DUMMY));

The JS emitter has the same gate at comp.ts:3046. A U32 match whose arms are dense literals returning constants seems to be the same shape emit_tab already handles for Nat.

Correctness is not affected: 1,500 random draws through each binary, every observed index returned (i * 37 + 11) % 251 on both lanes (the U32 run happened to miss 1 of the 256 indices, the Nat run hit all 256).

The file

Both files are generated; this shell writes the 256-arm pair.

for M in u32 nat; do
  { echo "import Base"; echo
    if [ $M = u32 ]; then echo "def tbl(x: U32) -> U32:"; else echo "def tbl(x: Nat) -> U32:"; fi
    echo "  match x:"
    for i in $(seq 0 255); do
      if [ $M = u32 ]; then echo "    case $i:"; else echo "    case ${i}n:"; fi
      echo "      $(( (i * 37 + 11) % 251 ))"
    done
    echo "    case _:"; echo "      0"; echo
    echo "law main:"; echo "  IO(Unit)"; echo
    echo "def main():"; echo "  do IO<Unit>:"
    echo "    r : U32 <- IO.try(U32, IO.random_u32())"
    if [ $M = u32 ]; then
      echo "    IO.print(U32.show(tbl((r % 256 : U32))))"
    else
      echo "    IO.print(U32.show(tbl(U32.to_nat((r % 256 : U32)))))"
    fi
  } > tbl_$M.bend
done

It came up as a 256-entry byte table in a codec; rewriting the scrutinee as a Nat was the workaround, at the cost of a conversion on every lookup.

bend --version

bend 2.0.16

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang version 21.0.0 (clang-2100.3.34.2)

Activity

  1. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.19.

    You were right about the cause. A U32 is a chain of bits, so a literal pattern became a 32-deep decision tree, while a Nat match with dense literal arms already compiled to a CONSTV table. The emitter now reads the literals back off that bit tree and builds the same table for U32, on the C lane and the JS lane.

    Your 256-arm file, on this machine: 1.46 MB of C and 3.5 GB of RSS became 80 KB and 1.6 GB, and the compile went from about 5 s to 1.3 s.

    The table is built when the arms fill over half of 0..max. A sparse set of literals, or a default that reads the scrutinee, keeps the old lowering, and an index past the last arm takes the default.
    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

  2. VictorTaelin commented on Sep 20, 2026

    @VictorTaelin
    Contributor

    A follow-up landed in 2.0.20: the table in 2.0.19 filled its gaps with the last default, so a hand-written bit pattern between literal cases (case U32{WCon{True{}, r}}: 12, every odd word) read the _ case instead. The walk now carries the default each bit falls off to, and builds the table only when they all agree; a match with such an arm keeps the if tree. Pinned as tests/compile/u32_table_bitvar.bend.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions