Skip to content

Live variables

Which values must a compiler keep at each block boundary? On a loop header that can enter its body or return a result, the header needs the guard, the value the body will read, and the result the exit will return. This workload computes those sets twice and measures how many blocks each schedule evaluates.

Which values must a compiler keep at each block boundary? On a loop header that can enter its body or return a result, the header needs the guard, the value the body will read, and the result the exit will return. This workload computes those sets twice and measures how many blocks each schedule evaluates.

A backward worklist saves 510 of 768 block evaluations on a 256-block control-flow graph

  • knotkit/graph: Provides breadth-first search, Dijkstra, topological order, and connected components over integer-vertex graphs, stored as adjacency lists, compressed sparse rows, or an adjacency matrix
  • knotkit/bitset: Provides exact membership, cardinality, set algebra, and order statistics over unsigned 32-bit ids, in one flat bitmap or 65,536-id chunks
  • knotkit/deque: Queues elements at both ends in O(1) amortized time, over array and Float64Array ring buffers, with an optional fixed capacity that turns the queue into a sliding window
{"v":1,"box":[38.2274,94.1597,835.3333,437.3555],"x":{"s":"log","lim":[3.31727818326,308.686803889],"inv":false,"g":0,"t":"Control-flow blocks (log scale)","tk":[[4,"4"],[8,"8"],[16,"16"],[32,"32"],[64,"64"],[128,"128"],[256,"256"]],"b":10},"y":{"s":"linear","lim":[0,3.15],"inv":false,"g":0,"tk":[[0,"0"],[1,"1"],[2,"2"],[3,"3"]]},"ti":"Full sweeps evaluate each block 3 times; the worklist 1.01 times","a":[{"k":"line","c":"#fffff8","s":"x","d":[4,3,8,3,16,3,32,3,64,3,128,3,256,3],"p":[71.14,110.5,193.02,110.5,314.9,110.5,436.78,110.5,558.66,110.5,680.54,110.5,802.43,110.5]},{"k":"line","c":"#1d5fa6","s":"x","d":[4,3,8,3,16,3,32,3,64,3,128,3,256,3],"p":[71.14,110.5,193.02,110.5,314.9,110.5,436.78,110.5,558.66,110.5,680.54,110.5,802.43,110.5]},{"k":"line","c":"#fffff8","s":"x","d":[4,1.5,8,1.25,16,1.125,32,1.0625,64,1.03125,128,1.015625,256,1.0078125],"p":[71.14,273.93,193.02,301.17,314.9,314.79,436.78,321.6,558.66,325,680.54,326.7,802.43,327.55]},{"k":"line","c":"#c4501b","s":"x","d":[4,1.5,8,1.25,16,1.125,32,1.0625,64,1.03125,128,1.015625,256,1.0078125],"p":[71.14,273.93,193.02,301.17,314.9,314.79,436.78,321.6,558.66,325,680.54,326.7,802.43,327.55]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#c4501b"}],"lg":[]} 4 8 16 32 64 128 256 Control-flow blocks (log scale) 0 1 2 3 two passes to converge, a third to confirm 258 evaluations for 256 blocks 18 for 16: the body and header run twice Evaluations per block Full sweep Predecessor worklist Full sweeps evaluate each block 3 times; the worklist 1.01 times
{"v":1,"box":[38.2274,122.3921,387.3333,437.3555],"x":{"s":"log","lim":[3.31727818326,308.686803889],"inv":false,"g":0,"t":"Control-flow blocks (log scale)","tk":[[4,"4"],[8,"8"],[16,"16"],[32,"32"],[64,"64"],[128,"128"],[256,"256"]],"b":10},"y":{"s":"linear","lim":[0,3.15],"inv":false,"g":0,"tk":[[0,"0"],[1,"1"],[2,"2"],[3,"3"]]},"ti":"Full sweeps evaluate each block 3 times;\nthe worklist 1.01 times","a":[{"k":"line","c":"#fffff8","s":"x","d":[4,3,8,3,16,3,32,3,64,3,128,3,256,3],"p":[52.64,137.39,106.02,137.39,159.4,137.39,212.78,137.39,266.16,137.39,319.54,137.39,372.92,137.39]},{"k":"line","c":"#1d5fa6","s":"x","d":[4,3,8,3,16,3,32,3,64,3,128,3,256,3],"p":[52.64,137.39,106.02,137.39,159.4,137.39,212.78,137.39,266.16,137.39,319.54,137.39,372.92,137.39]},{"k":"line","c":"#fffff8","s":"x","d":[4,1.5,8,1.25,16,1.125,32,1.0625,64,1.03125,128,1.015625,256,1.0078125],"p":[52.64,287.37,106.02,312.37,159.4,324.87,212.78,331.12,266.16,334.24,319.54,335.8,372.92,336.59]},{"k":"line","c":"#c4501b","s":"x","d":[4,1.5,8,1.25,16,1.125,32,1.0625,64,1.03125,128,1.015625,256,1.0078125],"p":[52.64,287.37,106.02,312.37,159.4,324.87,212.78,331.12,266.16,334.24,319.54,335.8,372.92,336.59]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#c4501b"}],"lg":[]} 4 8 16 32 64 128 256 Control-flow blocks (log scale) 0 1 2 3 two passes to converge, a third to confirm 258 evaluations for 256 blocks 18 for 16: the body and header run twice Evaluations per block Full sweep Predecessor worklist Full sweeps evaluate each block 3 times; the worklist 1.01 times
  • At 256 blocks, full sweeps evaluate 768 blocks and the predecessor worklist evaluates 258, saving 510 evaluations.
  • Both schedules agree on every entry and exit set. Variables 0, 1, 2 are live on entry to the loop header.
  • Both start with empty live sets and visit higher block IDs first. Only the worklist limits later visits to affected predecessors.

Hover or focus a chart for exact values. Drawn by demo.ts at build time, from the fixtures the benchmark uses; it reproduces the README figure.

Terminal window
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:live-variables

problems/live-variables/README.md states the fixture, the seed, and the result table.