Repository navigation
Reuse template literal scratch buffer on Checker - #65804
Open
Jake Bailey (jakebailey) wants to merge 1 commit into
Open
Jake Bailey (jakebailey) wants to merge 1 commit into
Jake Bailey (jakebailey) wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Reentrant template-literal resolution can reuse the scratch buffer and overwrite accumulated text, causing incorrect types to be cached.
1 open issue
What changed in this PR
This PR reduces per-call allocations when the TypeScript checker constructs template literal types.
Changes:
- Reuses a checker-owned byte buffer instead of a local
strings.Builder. - Extracts span processing from a closure into
addTemplateLiteralSpans.
| File | Description |
|---|---|
tsc/internal/checker/checker.go |
Adds reusable scratch storage and extracts template literal span processing. |
Review effort: Balanced
| var newTexts []string | ||
| var sb strings.Builder | ||
| sb.WriteString(texts[0]) | ||
| out := append(c.templateBuf[:0], texts[0]...) |
getTemplateLiteralType previously allocated a fresh strings.Builder
and closure on every call. Reuse a byte scratch buffer on Checker and
extract addTemplateLiteralSpans as a method to avoid per-call builder
and closure allocations.
benchstat (-pgo=off, parent vs commit, n=6):
goos: linux
goarch: amd64
│ old │ new │
│ sec/op │ sec/op vs base │
Tsc/vscode 6.192 ± 2% 6.165 ± 2% ~ (p=0.589 n=6)
Tsc/mui-docs 3.742 ± 3% 3.661 ± 3% ~ (p=0.132 n=6)
geomean 4.814 4.751 -1.30%
│ old │ new │
│ B-rss │ B-rss vs base │
Tsc/vscode 4.546Gi ± 3% 4.575Gi ± 2% ~ (p=0.485 n=6)
Tsc/mui-docs 2.177Gi ± 4% 2.114Gi ± 6% ~ (p=0.065 n=6)
geomean 3.146Gi 3.110Gi -1.15%
Jake Bailey (jakebailey)
force-pushed
the
fast-pr/template-literal-buf
branch
from
December 4, 2026 23:13
6e3cc75 to
644ab22
Compare
Member
Author
|
TypeScript Bot (@typescript-bot) perf test this faster |
Contributor
Contributor
|
Jake Bailey (@jakebailey) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.

getTemplateLiteralTypepreviously allocated a freshstrings.Builderand closure on every call.Reuse a byte scratch buffer on the checker and extract
addTemplateLiteralSpansas a method to avoid per-call builder and closure allocations.