[Perf][SwiftSyntax] Tail allocate a parsed token's text, and optimize short tokens #3453
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.60) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.60 · 238 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.328
Shallow Review Depth
shallow_review_depth
0.289
Sensitive Paths
sensitive_paths
0.060
Real PR Conversation Timeline
1 comments
"Review active on #3453"
general
weight: 0.35
nkcsgexi
Each parsed token now keeps its text in a buffer tail-allocated past its fields, instead of a
SyntaxText(an address and a count) into the arena's copy of the whole source, and stores three lengths in its place. The parser no longer copies the source into the arena.The text is copied with
copyText(_:to:sourceBufferEnd:)rather thanmemcpy, which spends longer deciding how to copy a few bytes than copying them.copyTextmoves whole words, so a short token is one load and one store, saving about 1% of a parse's instructions and 1.5–2.5% of its time. Because it can read past the token's end, it does so only wheresourceBufferEndsays the lexer's buffer extends that far.A token that is present, has no diagnostic and has at most 255 bytes of text, which is almost every token, gets its own four-byte shape,
SmolParsedToken.ParsedTokenneeds twenty.Every node is allocated through one function, generic over its fields, which checks that the header names that shape and that the fields' alignment fits. The four accessors that read a node's fields share one generic read through the same check.
Collecting a tree's bytes no longer coalesces adjacent texts, which only worked while every token pointed into one copy of the source. Left as it was would cost 1.82× the instructions on the path
SourceLocationConvertertakes for every file. It now allocates once and copies each text withcopyText.Against main (911061a6775a6b1216504de882dc205c7b6de3f1), over 749 files, the arena allocates 21.9% less, and a tree goes from 24.25× the size of its source to 18.94×. A parse executes 0.8% to 1.4% fewer instructions depending on the input.
@_spi(RawSyntax)changes:RawSyntax.withEachSyntaxText(body:)andParsingRawSyntaxArena.internSourceBuffer(_:)are removed, andRawTokenSyntaxgains an initializer taking the lexer's buffer and the token's byte lengths.