← Back to Pull requests

[Perf][SwiftSyntax] Tail allocate a parsed token's text, and optimize short tokens #3453

Closed
unknown closed this pull request without merging on Oct 03, 2026 03:31 UTC
View on GitHub
unknown unknown commented on Oct 01, 2026 16:43 UTC
Author

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 than memcpy, which spends longer deciding how to copy a few bytes than copying them. copyText moves 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 where sourceBufferEnd says 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. ParsedToken needs 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 SourceLocationConverter takes for every file. It now allocates once and copies each text with copyText.

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:) and ParsingRawSyntaxArena.internSourceBuffer(_:) are removed, and RawTokenSyntax gains an initializer taking the lexer's buffer and the token's byte lengths.

Diff: 238 lines (+178 / -60) across 2 files Reviewer: nkcsgexi Review duration: 2088m
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
0.60
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 60.4% (MEDIUM) · Higher = larger blast radius
60.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 23.9% · Safe to merge
23.9%