Skip to content

mem: intern builds a node fact only for a new int-keyed node, and reads var names from a sorted build - #70

Merged
vic merged 1 commit into
mainfrom
mem-intern-lazy
Nov 12, 2026
Merged

vic merged 1 commit into
mainfrom
mem-intern-lazy

Conversation

@vic

@vic vic commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Three costs in the intern step of --mem, measured with callgrind on s2c compiling itself:

  • Mem.idOf built the enode fact of a node on every lookup, also when the node was already known (106,936 calls, 15,927 new var nodes).
  • The var map (id to def and name) was an IMap that got one insert per var node, in id order (43.5 M).
  • Each var node got an enode fact. Mem.ofKind (the only reader of enode) reads site and call kinds only, and Mem.nVar is the only node keyed by a name, so these 15,927 facts were dead input to the solve.

Now the enode fact is built only for a new int-keyed node, and the var map is a list consed in id order, built once into a BMap (Coll.bFromSorted, one run) before the sharing pass reads it.

Measured on s2c compiling itself, both compilers on the same input (perf stat -e instructions:u):

run before after
normal compile 535,526,147 535,526,180
--mem 1,515,904,996 1,426,105,986 (-5.9%)

Checks:

  • --mem output on s2c is byte-identical between the two compilers, and every tests/s2/mem test matches its .out.
  • The C, stderr and exit code of 830 outputs (prog, cok, cneg, neg, self) are identical.
  • s2c reaches its fixpoint and the seed is updated.

Mutant (one line, builds): the var map keyed next + 1 instead of next changes the reuse lines of s2c --mem (5 extra reuses), but no tests/s2/mem test catches it: whether the off-by-one name hits a shared var depends on which node gets the next id. Three test shapes did not catch it. The class (a wrong shared set gives an unsound reuse) is caught at run time by the M7b differential and poison oracles, which come with the codegen.

@vic
vic merged commit 71bec38 into main Nov 12, 2026
3 checks passed
@vic
vic deleted the mem-intern-lazy branch November 17, 2026 16:48
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