perf(bytecode): VM list-iterate with the counted for pass (#482) #520
Loading…
Reference in a new issue
No description provided.
Delete branch "loop/482-vm-list-iterate"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #482
The counted
forloop over a list ran 3x slower in the VM than before #475 (issue #482). Commit 1 widens the guard oftools/collection-cost.shtocompiler/src/backend/bytecodeandcompiler/src/vm, so this PR's tier B measures the VM, and prints the bytecode ofbenchmarks/list-iterate.trbfor base and head. Commit 2 is the fix.Root cause. The bytecode of the new loop is minimal (
less,branch, oneloadwith the list-item step, three adds), so the emitter is not at fault.kernelCall(compiler/src/vm/interpret.trb) turned theKernelOperationinto its number withkernelNumberOf(compiler/src/backend/bytecode/format.trb), a linear scan overallKernelOperations.ElementAddress(whatlist-indexuses) sits near the front of the table,ListItemAddress(operation 68, added late) near the back, so every item read scanned ~68 entries before the kernel ran: ~500 ns per read, 80M reads, 62 s - 20 s.Fix.
kernelNumberOfis amatch, generated from the table in the table's order, so the CTORB_OPERATION_*numbering and the table are unchanged.compiler/tests/bytecode.test.trbalready pinned "every operation maps to its index"; it now also pins that the table's last operation stays belowkernelNativeBase.Test. Tier B (
tools/collection-cost.sh), VM fastest of 3, criteria:list-iterate<= ~22 400 ms (pre-#475 baseline in the issue: 20 339-20 828 ms),list-index<= 1.10x, native <= 1.7x, allocations 22/22.8cd52b0f, merge base already carries #475):list-index22 672 -> 23 177 ms,list-iterate62 358 -> 61 511 ms (noise floor; native 1.48x / 1.58x; allocations 22/22).00a63378, the same two commits before the rebase onto the currentmain; forge CI green, tier A/B and gates):list-index23 217 -> 23 476 ms (1.011x),list-iterate62 321 -> 12 143 ms (5.1x faster; the pre-#475 baseline was 20 339-20 828 ms, so the fix also beats it: the item read no longer scans the table at all, #475's smaller loop now pays off); native ratios unchanged (list-iterate1.60x,list-index6.19x -> 6.41x with the usual noise); allocations 22/22; hello-world native size 0 bytes; bytes-pass 0.860; round trip 0.971 of the one before.tools/collection-cost.shcompares to the merge base, which already carries #475, so its "base" row is the slow one; the pre-#475 numbers come from the issue.tools/check-commits.sh origin/main HEAD: clean.9030a853d8f9acf31b5ff9acf31b5f8cd52b0f728cd52b0f7200a63378d500a63378d524803ba850WIP: perf(bytecode): VM list-iterate with the counted for pass (#482)to perf(bytecode): VM list-iterate with the counted for pass (#482)Approved.
bytecode.test.trbalready pinned "every operation maps to its index"; the new test pins the last operation andlast < kernelNativeBase. CI green on this head (tier A/B, gates, pdisc, commit messages).matcharms againstallKernelOperations: same order, same numbers, and the set equals theKernelOperationcases, so the CTORB_OPERATION_*numbering is unchanged and the match is exhaustive.tools/collection-cost.sh(measurement only, exits 0).list-iterate62 s to 12 s,list-indexwithin noise, allocations 22/22).Non-blocking: the match must be extended by hand when an operation is added; the index-equals-table test catches a missing or wrong arm.