|
| 1 | +// Micro-benchmark for RemoteNodeList access patterns. |
| 2 | +// |
| 3 | +// Focuses on the cost of the element accessors (`list[i]`), the `.at()` |
| 4 | +// method, and the inherited Array iteration methods (map/forEach/reduce), |
| 5 | +// all of which walk the encoded linked list of children. |
| 6 | +// |
| 7 | +// Run with: |
| 8 | +// npm run node -- test/nodelist.bench.ts |
| 9 | +// npm run node -- test/nodelist.bench.ts --filter map |
| 10 | + |
| 11 | +import { |
| 12 | + type Expression, |
| 13 | + type Path, |
| 14 | + type SourceFile, |
| 15 | + type Statement, |
| 16 | + SyntaxKind, |
| 17 | + TokenFlags, |
| 18 | +} from "@typescript/native-preview/unstable/ast"; |
| 19 | +import { |
| 20 | + createArrayLiteralExpression, |
| 21 | + createExpressionStatement, |
| 22 | + createNumericLiteral, |
| 23 | + createSourceFile, |
| 24 | + createToken, |
| 25 | +} from "@typescript/native-preview/unstable/ast/factory"; |
| 26 | +import { fileURLToPath } from "node:url"; |
| 27 | +import { parseArgs } from "node:util"; |
| 28 | +import { Bench } from "tinybench"; |
| 29 | +import { encodeSourceFile } from "../src/api/node/encoder.ts"; |
| 30 | +import type { RemoteNodeList } from "../src/api/node/node.generated.ts"; |
| 31 | +import { RemoteSourceFile } from "../src/api/node/node.ts"; |
| 32 | + |
| 33 | +const isMain = process.argv[1] === fileURLToPath(import.meta.url); |
| 34 | +if (isMain) { |
| 35 | + const { values } = parseArgs({ |
| 36 | + options: { |
| 37 | + filter: { type: "string" }, |
| 38 | + size: { type: "string", default: "500" }, |
| 39 | + }, |
| 40 | + }); |
| 41 | + runBenchmarks(values); |
| 42 | +} |
| 43 | + |
| 44 | +function makeSourceFileWithLargeList(elementCount: number): Uint8Array { |
| 45 | + const elements: Expression[] = []; |
| 46 | + let text = "["; |
| 47 | + for (let i = 0; i < elementCount; i++) { |
| 48 | + elements.push(createNumericLiteral(String(i), TokenFlags.None)); |
| 49 | + text += (i === 0 ? "" : ",") + i; |
| 50 | + } |
| 51 | + text += "]"; |
| 52 | + const array = createArrayLiteralExpression(elements); |
| 53 | + const stmt: Statement = createExpressionStatement(array); |
| 54 | + const eof = createToken(SyntaxKind.EndOfFile); |
| 55 | + const sf: SourceFile = createSourceFile([stmt], eof, text, "/bench.ts", "/bench.ts" as Path); |
| 56 | + return encodeSourceFile(sf); |
| 57 | +} |
| 58 | + |
| 59 | +function getElementsList(data: Uint8Array): RemoteNodeList { |
| 60 | + const sf = new RemoteSourceFile(data, new TextDecoder()); |
| 61 | + const stmt = (sf.statements as any).at(0); |
| 62 | + return stmt.expression.elements as RemoteNodeList; |
| 63 | +} |
| 64 | + |
| 65 | +export function runBenchmarks(options?: { filter?: string; size?: string; }): void { |
| 66 | + const { filter, size } = options ?? {}; |
| 67 | + const elementCount = size ? parseInt(size, 10) : 500; |
| 68 | + |
| 69 | + const encoded = makeSourceFileWithLargeList(elementCount); |
| 70 | + |
| 71 | + // Sanity check that access works and produces the expected length. |
| 72 | + { |
| 73 | + const list = getElementsList(encoded); |
| 74 | + if (list.length !== elementCount) { |
| 75 | + throw new Error(`Expected ${elementCount} elements, got ${list.length}`); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const bench = new Bench({ |
| 80 | + name: `RemoteNodeList (${elementCount} elements)`, |
| 81 | + iterations: 30, |
| 82 | + warmupIterations: 10, |
| 83 | + }); |
| 84 | + |
| 85 | + // Each task re-decodes the source file so node materialization caching |
| 86 | + // inside `sourceFile.nodes` does not carry over between iterations, which |
| 87 | + // would otherwise mask the cost of walking the list. |
| 88 | + bench |
| 89 | + .add("index access (list[i])", () => { |
| 90 | + const list = getElementsList(encoded); |
| 91 | + let sum = 0; |
| 92 | + for (let i = 0; i < list.length; i++) { |
| 93 | + sum += list[i].pos; |
| 94 | + } |
| 95 | + if (sum < 0) throw new Error("unreachable"); |
| 96 | + }) |
| 97 | + .add("at(i)", () => { |
| 98 | + const list = getElementsList(encoded); |
| 99 | + let sum = 0; |
| 100 | + for (let i = 0; i < list.length; i++) { |
| 101 | + sum += list.at(i)!.pos; |
| 102 | + } |
| 103 | + if (sum < 0) throw new Error("unreachable"); |
| 104 | + }) |
| 105 | + .add("for..of (iterator)", () => { |
| 106 | + const list = getElementsList(encoded); |
| 107 | + let sum = 0; |
| 108 | + for (const node of list) { |
| 109 | + sum += node.pos; |
| 110 | + } |
| 111 | + if (sum < 0) throw new Error("unreachable"); |
| 112 | + }) |
| 113 | + .add("map", () => { |
| 114 | + const list = getElementsList(encoded); |
| 115 | + const positions = list.map(node => node.pos); |
| 116 | + if (positions.length < 0) throw new Error("unreachable"); |
| 117 | + }) |
| 118 | + .add("filter", () => { |
| 119 | + const list = getElementsList(encoded); |
| 120 | + const filtered = list.filter(node => node.pos >= 0); |
| 121 | + if (filtered.length < 0) throw new Error("unreachable"); |
| 122 | + }) |
| 123 | + .add("forEach", () => { |
| 124 | + const list = getElementsList(encoded); |
| 125 | + let sum = 0; |
| 126 | + list.forEach(node => { |
| 127 | + sum += node.pos; |
| 128 | + }); |
| 129 | + if (sum < 0) throw new Error("unreachable"); |
| 130 | + }) |
| 131 | + .add("reduce", () => { |
| 132 | + const list = getElementsList(encoded); |
| 133 | + const sum = list.reduce((acc, node) => acc + node.pos, 0); |
| 134 | + if (sum < 0) throw new Error("unreachable"); |
| 135 | + }) |
| 136 | + .add("forEachNode", () => { |
| 137 | + const list = getElementsList(encoded); |
| 138 | + let sum = 0; |
| 139 | + list.forEachNode(node => { |
| 140 | + sum += node.pos; |
| 141 | + return undefined; |
| 142 | + }); |
| 143 | + if (sum < 0) throw new Error("unreachable"); |
| 144 | + }); |
| 145 | + |
| 146 | + if (filter) { |
| 147 | + const pattern = filter.toLowerCase(); |
| 148 | + for (const task of [...bench.tasks]) { |
| 149 | + if (!task.name.toLowerCase().includes(pattern)) { |
| 150 | + bench.remove(task.name); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + bench.runSync(); |
| 156 | + console.table(bench.table()); |
| 157 | +} |
0 commit comments