Skip to content

Latest commit

 

History

History
178 lines (157 loc) · 15.3 KB

ALLOCATIONS.md

File metadata and controls

178 lines (157 loc) · 15.3 KB

ALLOCATIONS: what one parse allocates, per target (auto-generated)

A REPORT, not a gate: node test/alloc-work.ts --write writes it and the README auto-regen workflow (.github/workflows/readme-bench.yml) rewrites it on master. The gate alloc-work checks only the invariants (per-token counts do not grow with the input; a reused Go builder allocates nothing). The counts are exact: TS target and emitted JS parser by instrumentation of the emitted module (test/alloc-instrument.ts), Go by runtime.MemStats with the collector off. Bytes: Go exact (TotalAlloc); TS / JS modeled from V8's layout (pointer compression; the model is in test/alloc-instrument.ts; the gate prints a measured heapUsed delta beside it). One warm parse runs before the measured one; what a module builds once is not counted.

Go rows measured with go1.27.4. Which function an allocation is attributed to depends on the toolchain (one commit: a 16 B slice growth in (*engine).matchLit under go1.27.4, in (*engine).finish under go1.26.11), so the rows hold for this toolchain only.

Inputs (locked npm packages): acorn@8.16.0 dist/acorn.js, typescript@5.9.3 lib/lib.es5.d.ts and lib/_tsserver.js (also repeated 4×: per-token counts must not grow with size).

Modes. TS target: parse(src) and parseWith(src, cstBuilder): one engine, run with the CST builder; parse adds no step of its own. JS target: fresh = a new createParser() per parse, reuse = one parser, its document reused. Go: fresh = a new CstBuilder per ParseWith, reuse = one kept. RESULT = what the returned tree holds (TS: objects reachable from it; Go fresh: the final capacity of Nodes / Kids / Toks); OVERHEAD = everything else, garbage once the parse returns. Tokens: each target's own tokenize count. Nodes: rule nodes of the result (JS, Go: the arena's node count).

Per parse

target grammar input mode tokens nodes allocations per token per node bytes B per token B per node result allocs result bytes overhead bytes
ts typescript acorn.js parse 42,251 28,638 159,870 3.78 5.58 5,263,936 124.6 183.8 99,327 2,948,317 2,315,619
ts typescript acorn.js parseWith 42,251 28,638 159,870 3.78 5.58 5,263,936 124.6 183.8 99,327 2,948,317 2,315,619
ts typescript lib.es5.d.ts parse 13,659 7,182 52,393 3.84 7.30 1,712,924 125.4 238.5 28,023 836,284 876,640
ts typescript lib.es5.d.ts parseWith 13,659 7,182 52,393 3.84 7.30 1,712,924 125.4 238.5 28,023 836,284 876,640
ts typescript _tsserver.js parse 4,932 3,139 18,563 3.76 5.91 621,168 125.9 197.9 11,206 333,261 287,907
ts typescript _tsserver.js parseWith 4,932 3,139 18,563 3.76 5.91 621,168 125.9 197.9 11,206 333,261 287,907
ts typescript _tsserver.js ×4 parse 19,728 12,553 74,173 3.76 5.91 2,439,120 123.6 194.3 44,818 1,332,916 1,106,204
ts typescript _tsserver.js ×4 parseWith 19,728 12,553 74,173 3.76 5.91 2,439,120 123.6 194.3 44,818 1,332,916 1,106,204
ts javascript acorn.js parse 42,051 32,239 166,694 3.96 5.17 5,457,020 129.8 169.3 106,529 3,149,490 2,307,530
ts javascript acorn.js parseWith 42,051 32,239 166,694 3.96 5.17 5,457,020 129.8 169.3 106,529 3,149,490 2,307,530
ts javascript _tsserver.js parse 4,928 3,411 19,197 3.90 5.63 638,752 129.6 187.3 11,750 348,191 290,561
ts javascript _tsserver.js parseWith 4,928 3,411 19,197 3.90 5.63 638,752 129.6 187.3 11,750 348,191 290,561
ts javascript _tsserver.js ×4 parse 19,712 13,641 76,709 3.89 5.62 2,509,456 127.3 184.0 46,994 1,392,635 1,116,821
ts javascript _tsserver.js ×4 parseWith 19,712 13,641 76,709 3.89 5.62 2,509,456 127.3 184.0 46,994 1,392,635 1,116,821
js typescript acorn.js fresh 42,251 28,813 5,161 0.12 0.18 14,643,756 346.6 508.2 - - -
js typescript acorn.js reuse 42,251 28,813 4,992 0.12 0.17 234,424 5.5 8.1 - - -
js typescript lib.es5.d.ts fresh 13,659 7,692 7,008 0.51 0.91 2,790,368 204.3 362.8 - - -
js typescript lib.es5.d.ts reuse 13,659 7,692 6,907 0.51 0.90 162,260 11.9 21.1 - - -
js typescript _tsserver.js fresh 4,932 3,217 429 0.09 0.13 1,340,580 271.8 416.7 - - -
js typescript _tsserver.js reuse 4,932 3,217 330 0.07 0.10 15,368 3.1 4.8 - - -
js typescript _tsserver.js ×4 fresh 19,728 12,865 1,423 0.07 0.11 5,001,916 253.5 388.8 - - -
js typescript _tsserver.js ×4 reuse 19,728 12,865 1,293 0.07 0.10 60,512 3.1 4.7 - - -
js javascript acorn.js fresh 42,051 32,559 4,796 0.11 0.15 14,602,512 347.3 448.5 - - -
js javascript acorn.js reuse 42,051 32,559 4,627 0.11 0.14 228,444 5.4 7.0 - - -
js javascript _tsserver.js fresh 4,928 3,535 425 0.09 0.12 1,497,812 303.9 423.7 - - -
js javascript _tsserver.js reuse 4,928 3,535 318 0.06 0.09 15,176 3.1 4.3 - - -
js javascript _tsserver.js ×4 fresh 19,712 14,137 1,386 0.07 0.10 6,416,748 325.5 453.9 - - -
js javascript _tsserver.js ×4 reuse 19,712 14,137 1,245 0.06 0.09 59,744 3.0 4.2 - - -
go typescript acorn.js fresh 42,251 28,638 85 0.00 0.00 9,671,168 228.9 337.7 3 2,154,496 7,516,672
go typescript acorn.js reuse 42,251 28,638 0 0.00 0.00 0 0.0 0.0 0 0 0
go typescript acorn.js slim-reuse 42,251 11,491 0 0.00 0.00 0 0.0 0.0 0 0 0
go typescript lib.es5.d.ts fresh 13,659 7,233 75 0.01 0.01 2,289,312 167.6 316.5 3 589,824 1,699,488
go typescript lib.es5.d.ts reuse 13,659 7,233 0 0.00 0.00 0 0.0 0.0 0 0 0
go typescript lib.es5.d.ts slim-reuse 13,659 3,112 0 0.00 0.00 0 0.0 0.0 0 0 0
go typescript _tsserver.js fresh 4,932 3,185 61 0.01 0.02 759,456 154.0 238.4 3 237,568 521,888
go typescript _tsserver.js reuse 4,932 3,185 0 0.00 0.00 0 0.0 0.0 0 0 0
go typescript _tsserver.js slim-reuse 4,932 1,372 0 0.00 0.00 0 0.0 0.0 0 0 0
go typescript _tsserver.js ×4 fresh 19,728 12,737 79 0.00 0.01 3,841,696 194.7 301.6 3 933,888 2,907,808
go typescript _tsserver.js ×4 reuse 19,728 12,737 0 0.00 0.00 0 0.0 0.0 0 0 0
go typescript _tsserver.js ×4 slim-reuse 19,728 5,485 0 0.00 0.00 0 0.0 0.0 0 0 0
go javascript acorn.js fresh 42,051 32,239 85 0.00 0.00 9,671,168 230.0 300.0 3 2,154,496 7,516,672
go javascript acorn.js reuse 42,051 32,239 0 0.00 0.00 0 0.0 0.0 0 0 0
go javascript acorn.js slim-reuse 42,051 11,491 0 0.00 0.00 0 0.0 0.0 0 0 0
go javascript _tsserver.js fresh 4,928 3,411 61 0.01 0.02 759,456 154.1 222.6 3 237,568 521,888
go javascript _tsserver.js reuse 4,928 3,411 0 0.00 0.00 0 0.0 0.0 0 0 0
go javascript _tsserver.js slim-reuse 4,928 1,367 0 0.00 0.00 0 0.0 0.0 0 0 0
go javascript _tsserver.js ×4 fresh 19,712 13,641 81 0.00 0.01 4,554,400 231.0 333.9 3 1,089,536 3,464,864
go javascript _tsserver.js ×4 reuse 19,712 13,641 0 0.00 0.00 0 0.0 0.0 0 0 0
go javascript _tsserver.js ×4 slim-reuse 19,712 5,465 0 0.00 0.00 0 0.0 0.0 0 0 0

Where: ts target, parse, typescript grammar, acorn.js (top 15 of 16 sites by bytes)

kind site (literals folded) in functions allocations per token modeled bytes share in result
object { off, end, nl, kid, lid } 1 42,251 1.00 1,352,032 25.7% 0
object { tokenType, offset: off, end } 1 43,236 1.02 1,210,608 23.0% 42,051
.slice() → array _hs.slice(b, _n) 1 28,916 0.68 978,432 18.6% 28,638
object { rule, children, offset: off, end } 1 28,915 0.68 809,620 15.4% 28,638
array growth toks.push(mk_tok(off, end, pendingNl, kid, lid)) 1 18 0.00 607,924 11.5% 0
.slice() → string src.slice(pos, e) 1 16,248 0.38 300,424 5.7% 0
.slice() → string _src.slice(t.off, u.end) 1 203 0.00 3,248 0.1% 0
.slice() → string _src.slice(_sp[b * _SW + N], _sp[b * _SW + N]) 1 72 0.00 1,240 0.0% 0
array growth localE.push({ tokStart: _sp[s + N]!, tokEnd: pos, ext: Math.max(pos... 1 1 0.00 76 0.0% 0
array growth parenHead.push(!isMember && prevKid === KID_IDENT && !!_phK[prevLid]) 1 1 0.00 76 0.0% 0
array [] 10 4 0.00 64 0.0% 0
object { pos, pendingNl, prevLid, prevKid, hasPrev, bpLid, hasPrev2, paren... 1 1 0.00 60 0.0% 0
object { tokStart: _sp[s + N]!, tokEnd: pos, ext: Math.max(pos, lookEnd())... 1 1 0.00 36 0.0% 0
array [toks, lexFrom(src, N, false, N, N, N, false, false, [], false, fal... 1 1 0.00 32 0.0% 0
closure emit 1 1 0.00 32 0.0% 0

Where: ts target, parseWith, typescript grammar, acorn.js (top 15 of 16 sites by bytes)

kind site (literals folded) in functions allocations per token modeled bytes share in result
object { off, end, nl, kid, lid } 1 42,251 1.00 1,352,032 25.7% 0
object { tokenType, offset: off, end } 1 43,236 1.02 1,210,608 23.0% 42,051
.slice() → array _hs.slice(b, _n) 1 28,916 0.68 978,432 18.6% 28,638
object { rule, children, offset: off, end } 1 28,915 0.68 809,620 15.4% 28,638
array growth toks.push(mk_tok(off, end, pendingNl, kid, lid)) 1 18 0.00 607,924 11.5% 0
.slice() → string src.slice(pos, e) 1 16,248 0.38 300,424 5.7% 0
.slice() → string _src.slice(t.off, u.end) 1 203 0.00 3,248 0.1% 0
.slice() → string _src.slice(_sp[b * _SW + N], _sp[b * _SW + N]) 1 72 0.00 1,240 0.0% 0
array growth localE.push({ tokStart: _sp[s + N]!, tokEnd: pos, ext: Math.max(pos... 1 1 0.00 76 0.0% 0
array growth parenHead.push(!isMember && prevKid === KID_IDENT && !!_phK[prevLid]) 1 1 0.00 76 0.0% 0
array [] 10 4 0.00 64 0.0% 0
object { pos, pendingNl, prevLid, prevKid, hasPrev, bpLid, hasPrev2, paren... 1 1 0.00 60 0.0% 0
object { tokStart: _sp[s + N]!, tokEnd: pos, ext: Math.max(pos, lookEnd())... 1 1 0.00 36 0.0% 0
array [toks, lexFrom(src, N, false, N, N, N, false, false, [], false, fal... 1 1 0.00 32 0.0% 0
closure emit 1 1 0.00 32 0.0% 0

Where: js target, fresh, typescript grammar, acorn.js (top 15 of 55 sites by bytes)

kind site (literals folded) in functions allocations per token modeled bytes share in result
new Array → array new Array(tokN + N) 1 33 0.00 5,578,056 38.1% -
new Int32Array → typed array new Int32Array(kidCap) 1 9 0.00 2,753,088 18.8% -
new Int32Array → typed array new Int32Array(tokN + N) 1 11 0.00 1,859,792 12.7% -
new Int32Array → typed array new Int32Array(rowCap) 1 16 0.00 1,573,888 10.7% -
new Int32Array → typed array new Int32Array(tkCap) 2 8 0.00 983,552 6.7% -
new Uint8Array → typed array new Uint8Array(tkCap) 2 16 0.00 492,544 3.4% -
new Int32Array → typed array new Int32Array(rows) 1 8 0.00 262,656 1.8% -
new Uint8Array → typed array new Uint8Array(rowCap) 1 10 0.00 246,400 1.7% -
new Uint16Array → typed array new Uint16Array(tkCap) 2 4 0.00 246,016 1.7% -
new Int32Array → typed array new Int32Array(rows * N) 1 3 0.00 196,800 1.3% -
new Uint16Array → typed array new Uint16Array(rowCap) 1 2 0.00 98,432 0.7% -
.exec() → array LXRE_10_Number.exec(source) 1 1,719 0.04 96,280 0.7% -
.exec() → array LXRE_3_LineComment.exec(source) 1 784 0.02 46,972 0.3% -
.exec() → array LXRE_11_String.exec(source) 1 738 0.02 44,016 0.3% -
new Uint8Array → typed array new Uint8Array(rows) 1 5 0.00 41,280 0.3% -

Where: js target, reuse, typescript grammar, acorn.js (top 15 of 15 sites by bytes)

kind site (literals folded) in functions allocations per token modeled bytes share in result
.exec() → array LXRE_10_Number.exec(source) 1 1,719 0.04 96,280 41.1% -
.exec() → array LXRE_3_LineComment.exec(source) 1 784 0.02 46,972 20.0% -
.exec() → array LXRE_11_String.exec(source) 1 738 0.02 44,016 18.8% -
.slice() → string docFlat.slice(a, b) 1 1,297 0.03 20,892 8.9% -
.exec() → array LXRE_6_HexNumber.exec(source) 1 234 0.01 13,272 5.7% -
.exec() → array LXRE_4_BlockComment.exec(source) 1 195 0.00 11,740 5.0% -
.exec() → array LXRE_12_Regex.exec(source) 1 15 0.00 888 0.4% -
.exec() → array LXRE_1_JSDoc.exec(source) 1 2 0.00 120 0.1% -
array growth parenHeadStack.push(_ph) 1 1 0.00 76 0.0% -
array [] 9 2 0.00 32 0.0% -
closure prevIsValue 1 1 0.00 32 0.0% -
closure tkPush 1 1 0.00 32 0.0% -
array [N] 2 1 0.00 28 0.0% -
array [source] 1 1 0.00 28 0.0% -
.slice() → array lexEmptyPops.slice() 1 1 0.00 16 0.0% -

Where: go target, fresh, typescript grammar, acorn.js (top 6 of 6 sites by bytes)

site (innermost function in the parser package) allocations bytes share
(*engine).finish 48 6,244,960 64.6%
lexFrom.func2 26 3,413,248 35.3%
(*engine).matchLit 7 7,344 0.1%
(*engine).pushLeaf 2 5,440 0.1%
(*engine).matchTok 1 128 0.0%
(*engine).parseProgram 1 48 0.0%

Where: go target, reuse, typescript grammar, acorn.js (top 0 of 0 sites by bytes)

site (innermost function in the parser package) allocations bytes share

Where: go target, slim-reuse, typescript grammar, acorn.js (top 0 of 0 sites by bytes)

site (innermost function in the parser package) allocations bytes share