Skip to content

Hold tokens by reference in nodes - #4

Merged
TomasVotruba merged 3 commits into
mainfrom
token-refs
Dec 3, 2026
Merged

TomasVotruba merged 3 commits into
mainfrom
token-refs

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Nodes stored every token inline, 56 bytes each, also when the token was absent - a method call node took 408 bytes. Nodes now hold a reference to the token instead.

What changes

  • node token fields are Option<&'a Token<'a>>, token lists are Vec<'a, &'a Token<'a>>
  • the lexer allocates each token once in the arena and the parsers pass the reference around instead of copying the token
  • tokens in a tree are shared, so a visitor replaces a token instead of editing it in place (the formatter does that now)

Speed

Laravel corpus (18851 files), single thread: 8.58 -> 7.97 billion instructions, about 3.0 -> 2.2 billion cycles. Peak memory of the 24-core run drops from about 340 MB to 239 MB.

Same output

The dump stays byte-identical to the Go parser on Laravel (8.4, 7.4), Symfony (8.4, 7.4) and a PHP 7.4-era Laravel (7.4, 5.6), and all tests pass.

@TomasVotruba
TomasVotruba merged commit 14b2eeb into main Dec 3, 2026
3 checks passed
@TomasVotruba
TomasVotruba deleted the token-refs branch December 3, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant