Skip to content

Pool EmitContext #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025
Merged

Pool EmitContext #1287

merged 1 commit into from
Jun 25, 2025

Conversation

jakebailey
Copy link
Member

These objects can be reused, which is good because we create one of them per file to emit.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces pooling for EmitContext instances to reduce allocations by reusing contexts across files. Key changes include:

  • Added sync.Pool and GetEmitContext/release functions in internal/printer/emitcontext.go.
  • Updated consumers (emitter.go and test baselines) to use GetEmitContext with deferred release instead of NewEmitContext.
  • Ensured EmitContext.Reset() clears prior state before returning to the pool.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
internal/testutil/tsbaseline/type_symbol_baseline.go Switched from NewEmitContext() to pooled GetEmitContext()/putCtx().
internal/printer/emitcontext.go Introduced sync.Pool, GetEmitContext + release func, and Reset().
internal/compiler/emitter.go Replaced NewEmitContext() with GetEmitContext() and deferred release.
Comments suppressed due to low confidence (2)

internal/compiler/emitter.go:136

  • [nitpick] Consider standardizing the name of the cleanup function returned by GetEmitContext() (e.g. putCtx or releaseCtx) across the codebase to avoid confusion when reading pooled-context usage.
	emitContext, putEmitContext := printer.GetEmitContext()

internal/printer/emitcontext.go:47

  • Add a unit test to verify that an EmitContext obtained via GetEmitContext() is reset to a clean state after calling the release function, ensuring no leftover state persists between uses.
func GetEmitContext() (*EmitContext, func()) {

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea - any before/after numbers?

@jakebailey
Copy link
Member Author

For the test runner, it accounts for 5% of the allocations (6179460), and with this PR just 0.1% (99838).

@jakebailey jakebailey added this pull request to the merge queue Jun 25, 2025
Merged via the queue into main with commit 12cbd18 Jun 25, 2025
22 checks passed
@jakebailey jakebailey deleted the jabaile/random-perf-opts branch June 25, 2025 03:22
zshannon added a commit to zshannon/typescript-go that referenced this pull request Jun 25, 2025
* Update submodule, fix JSON syntax problems (microsoft#1285)

* Pool EmitContext (microsoft#1287)

---------

Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
zshannon added a commit to zshannon/typescript-go that referenced this pull request Jul 2, 2025
* Update submodule, fix JSON syntax problems (microsoft#1285)

* Pool EmitContext (microsoft#1287)

---------

Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants