Repository navigation
Description
This is the second of two regressions I found in the same program. The first is #2081, about the take on the host, and it accounts for the instructions. This one accounts for most of the cycles. The program is a BPE tokenizer ([email protected], 256 merges learned from a 4,096-word text, then a 32,768-word text encoded). The repro and a counting script are in https://github.com/nuxyel/bend-ml/blob/devel/docs/upstream/bpe_encode.bend and bpe_encode.sh; run it with EVENTS=instructions:u,cycles:u,mem_load_retired.l1_miss:u. The setup was one thread pinned to a P-core of a Core Ultra 7 155H, clang 23.1.3 with -std=c11 -O3, and perf stat in user mode. The output is the same on every build.
What I measured. I bisected between the tags on L1 load misses and it stopped at #2036 (3c1f0d3). Against its parent (4024f04):
- Instructions go down 1.1% (196.3 G to 194.2 G), so perf(comp): a wide def's boxed record stays boxed until a use needs it #2036 does what it says.
- Cycles go up 11% (39.1 G to 43.4 G).
- L1 load misses go from 0.22 G to 1.24 G, and almost all of them hit in L2 (
mem_load_retired.l2_missstays at 0.005 G).
What I could pin down. #2036 changes three functions in this program's C. merge.go and count.go now keep a list element's token (B{n}/M{k}) boxed and share it with term_keep, where before they took it apart and built a fresh node. bump.go reuses the taken Cnt node in place. I put one function at a time back to its pre-#2036 C, inside 2.0.40's own C:
merge.goand thepeekit calls, as before: the misses go back to 0.22 G and cycles to 39.2 G. Instructions go up to 198.9 G, since the oldmerge.godoes the extra unboxing.count.goas before: no change (1.24 G).bump.go's reuse or free order as before, each one alone and both: no change (1.24 G).- With this one and 2.0.40: on the CPU, every take of a shared node is a call (#1978), +10% instructions on a list-heavy program #2081's take both put back, 2.0.40 is at 2.0.39 again: 177.1 G instructions, 33.9 G cycles, 0.23 G misses.
The misses are not in merge.go itself, though. The sampled profile (mem_load_retired.l1_miss:pp) puts them in bump.go, the walk over the pair counters during training. There are two reads:
heap_allocreading a free block's next pointer (ALC_AT(e, cls) = e.mem[h]): 118 M to 630 M.term_peekreading a redirect cell: 7 M to 465 M.
The allocation counts barely move (class 0: 1.262 G to 1.153 G). How long a one-word block waits on its free chain before it is handed out again has almost the same histogram before and after.
What I haven't proven: why. My guess is the heap layout that each merge pass leaves for the counting pass after it. Before #2036, merge.go rebuilt every token node in list order. Now the old nodes stay where they were, and the counter list's nodes come from free chains in another order, spread over more cache lines. I haven't measured lines touched or data addresses, so take it as a lead. It may also just be what this one program does; I haven't looked for it in others.
Activity