Repository navigation
perf(js): parsec-js parsed at 0.57 times oxc's speed - #215
Open
jamesconwaydev wants to merge 12 commits into
Open
jamesconwaydev wants to merge 12 commits into
jamesconwaydev wants to merge 12 commits into
Conversation
Checking a declaration for `let` and duplicate names, a function's parameters, and an `else if` chain each built a fresh vector, and turning a cover into arrow parameters or a pattern copied its list first. They now reuse buffers the parser keeps, and walk lists by index. typescript.js parses about 8% faster.
The lexer read each identifier in the loop that also handles `\u` escapes and non-ASCII characters, then matched its text against the 54 keyword spellings. Plain ASCII identifiers now take a tight loop, and the keyword is found by a perfect hash, which the table's construction checks for collisions at compile time. typescript.js parses about 18% faster.
Each string key of an object literal was decoded to UTF-16 to see whether it says `__proto__`, allocating a buffer per key, which is most of the work in a page's inline JSON-like data. A key with no backslash is its own value, so it's compared as it is. The inline scripts of the benchmark's pages parse about 30% faster.
The string loop checked each byte against the quote, a backslash and the line terminators one call at a time. It now skips the bytes that can't end the string or start an escape in a single scan. The inline scripts parse about 11% faster, typescript.js about 3%.
The tree's vectors were reserved at one node per eight bytes, which fits a large bundle; short inline scripts run one per four or five, so their vectors reallocated as they filled. The first 64 KB are now sized at one per four, and the list scratch starts with room for 64. Scripts under 4 KB parse about 9% faster; a bundle's reservation grows by 8,192 nodes at most.
A script of a few dozen bytes got room for a handful of nodes and reallocated on the first statements. The tree now starts with room for 16 more. Scripts under 1 KB parse about 2% faster.
Every punctuator, `(` and `;` included, was found by matching the next four bytes at once, each read with a bounds check. The ten that are one character whatever follows are now matched where the lexer first looks at the byte. typescript.js and the inline scripts parse about 5% faster.
A string's run of ordinary characters was searched one byte at a time for the quote, a backslash or a line terminator. It's now searched eight bytes at a time, by the bit trick that finds a zero byte in a word, with no unsafe code. The inline scripts parse about 10% faster.
Asking whether an identifier is `let`, `eval`, `arguments` or a key like `__proto__` decoded its escapes, which scans it for a backslash, before comparing. Only a spelling longer than the word can decode to it, so the lengths settle most of these without decoding. The inline scripts parse about 2% faster.
A node and its span lived in two vectors, so making a node checked two capacities and wrote two places, and a short script allocated and grew both. They're now one vector of pairs, the same 36 bytes a node. Scripts under 4 KB parse about 3% faster.
The parser asks after each token whether the lexer reported an error, and that was a function call even when it hadn't. The length compare is now inlined, and reading what was reported is a cold function of its own. typescript.js and react-dom parse 1 to 3% faster.
Reading the next token ran inside the loop that keeps comments, so every token took that loop's setup. A token with no comment before it now returns straight away, and the loop is a function of its own. typescript.js and react-dom parse about 3% faster.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #190.
Problem. parsec-js parsed at 0.57 times
oxc_parser's speed over the bench's 11.6 MB, and at 0.30 times on the smallest inline scripts. Profiling typescript.js and the inline scripts with Instruments showed the time going to work no parse needs: a vector or two allocated for every declaration's early checks, every string key of an object decoded to UTF-16 to see whether it says__proto__, every identifier read through the loop that handles\uescapes, every punctuator read with four bytes of bounds-checked lookahead, strings scanned a byte at a time, a function call on every token to ask whether the lexer reported an error, and the comment loop's setup paid by every token. Short scripts also reallocated their tree as it filled, because it was sized for a large bundle.Change. One commit a cause, each measured on its own (release build, interleaved runs of typescript.js and the inline scripts):
perf(js): each declaration's early checks allocated a vector or two: the bound-name checks reuse buffers the parser keeps, and cover lists are walked by index instead of copied. typescript.js +8%.perf(js): every identifier was read a byte at a time through escapes: a tight loop for plain ASCII identifiers, and keywords found by a perfect hash whose table is checked for collisions at compile time. typescript.js +18%.perf(js): checking an object's string keys decoded every one: a key with no backslash is compared as it is. Inline scripts +30%.perf(js): strings were lexed a byte at a time: one scan to the next byte that can end the string. Inline +11%.perf(js): a short script's tree outgrew its vectors twice over: the first 64 KB are sized at a node per four bytes, not eight, and the list scratch starts with room for 64. Scripts under 4 KB +9%.perf(js): the tiniest scripts still grew their tree's vectors: 16 nodes more to start with. Scripts under 1 KB +2%.perf(js): one-character punctuators went through four bytes of lookahead: the ten that are one character whatever follows are matched where the lexer first looks at the byte. +5%.perf(js): strings were scanned for their end a byte at a time: eight bytes at a time, by the zero-byte bit trick on au64, in safe code. Inline +10%.perf(js): checking a name for let, eval or a key decoded it first:Parser::name_issettles most comparisons from the lengths. Inline +2%.perf(js): making a node pushed onto two vectors: a node and its span are oneVec<(Node, Span)>, the same 36 bytes. Small scripts +3%.perf(js): checking the lexer for errors was a call on every token: the length compare is inlined, the rest is cold. +1 to 3%.perf(js): every token paid for the loop that keeps comments: a token with no comment before it returns at once. +3%.Nothing else changed: no backtracking, the depth limit and step budget as they were, comments kept, no
unsafe, no new dependencies.Before is
js-records(d20638c), after is this branch, in the same session, each run twice, load average under 3. MB/s, median of the bench's runs:<script><script><script><script><script><script><script><script><script><script><script>Overall that's 1.41 times as fast as before, 0.80 times oxc and 1.95 times swc. Peak memory on typescript.js went from 44.1 MB to 44.4 (oxc holds 67.1, swc 90.5); the denser sizing for the first 64 KB is the 0.3 MB.
js-scopeis close to where it was: the same within run-to-run noise on most sources, and typescript.js about 3% slower (236 and 237 MB/s before, 226 and 231 after), likely from nodes and spans now sharing one vector. The scope, binding, reference and free-name counts are the same.What's left. The remaining 20% is spread across the per-token path: the lexer's dispatch and the branch at the end of each identifier and run of whitespace, and the parser's descent from
assign_covertoprimaryfor each operand. I tried and dropped, as not measurably faster: inliningLexer::next_token, a plain counter for the step budget, eight-space whitespace skipping, the word-at-a-time scan for comments and templates, and a single four-byte read for the multi-character punctuators. Getting further likely means a byte-class dispatch table in the lexer and fewer kind checks on the way down the expression grammar.Tests. No behaviour changed, so the existing tests are the check:
js-records.crates/parsec-js/scripts/compare-acorn.sh --bench: 107 the same, 0 different.tests/limits.rsstill passes, so the depth limit and step budget stop where they did.keywords_by_hashintoken.rschecks the perfect hash finds every keyword and agrees withKeyword::from_stron near misses;runs_withoutinlexer.rschecks the word-at-a-time scan against a byte-at-a-time one for every stop byte at every offset of inputs up to 20 bytes.Passed locally:
cargo test --workspace --release(523 tests),cargo clippy --workspace --all-targets -- -D warnings, clippy on the bench,cargo fmt --all --check,cargo docfor parsec-js with-D warnings,scripts/check-headers.sh, and theparsefuzz target for 3 minutes (1.8 million runs, no crash).