Skip to content

Commit 12cbd18

Browse files
authored
Pool EmitContext (#1287)
1 parent dea45fd commit 12cbd18

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

internal/compiler/emitter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ func (e *emitter) emitJSFile(sourceFile *ast.SourceFile, jsFilePath string, sour
133133
return
134134
}
135135

136-
emitContext := printer.NewEmitContext()
136+
emitContext, putEmitContext := printer.GetEmitContext()
137+
defer putEmitContext()
138+
137139
for _, transformer := range getScriptTransformers(emitContext, e.host, sourceFile) {
138140
sourceFile = transformer.TransformSourceFile(sourceFile)
139141
}
@@ -175,7 +177,8 @@ func (e *emitter) emitDeclarationFile(sourceFile *ast.SourceFile, declarationFil
175177
}
176178

177179
var diags []*ast.Diagnostic
178-
emitContext := printer.NewEmitContext()
180+
emitContext, putEmitContext := printer.GetEmitContext()
181+
defer putEmitContext()
179182
for _, transformer := range e.getDeclarationTransformers(emitContext, sourceFile, declarationFilePath, declarationMapPath) {
180183
sourceFile = transformer.TransformSourceFile(sourceFile)
181184
diags = append(diags, transformer.GetDiagnostics()...)

internal/printer/emitcontext.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package printer
33
import (
44
"maps"
55
"slices"
6+
"sync"
67
"sync/atomic"
78

89
"github.com/microsoft/typescript-go/internal/ast"
@@ -37,6 +38,20 @@ func NewEmitContext() *EmitContext {
3738
return c
3839
}
3940

41+
var emitContextPool = sync.Pool{
42+
New: func() any {
43+
return NewEmitContext()
44+
},
45+
}
46+
47+
func GetEmitContext() (*EmitContext, func()) {
48+
c := emitContextPool.Get().(*EmitContext)
49+
return c, func() {
50+
c.Reset()
51+
emitContextPool.Put(c)
52+
}
53+
}
54+
4055
func (c *EmitContext) Reset() {
4156
*c = EmitContext{
4257
Factory: c.Factory,

internal/testutil/tsbaseline/type_symbol_baseline.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ func (walker *typeWriterWalker) writeTypeOrSymbol(node *ast.Node, isSymbolWalk b
347347
fileChecker, done := walker.getTypeCheckerForCurrentFile()
348348
defer done()
349349

350-
ctx := printer.NewEmitContext()
350+
ctx, putCtx := printer.GetEmitContext()
351+
defer putCtx()
351352

352353
if !isSymbolWalk {
353354
// Don't try to get the type of something that's already a type.

0 commit comments

Comments
 (0)