Skip to content

Pool commentState allocations #1286

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 30, 2025
Merged

Pool commentState allocations #1286

merged 1 commit into from
Jun 30, 2025

Conversation

jakebailey
Copy link
Member

These comment states appear to be one of the main allocators when emitting the VS Code codebase. Pool them within a print run.

Reduces the VS Code alloc count from 36015276 to 28140625 (20% reduction).

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 a commentStatePool to reuse commentState instances and significantly reduce allocations when printing. It replaces direct allocations of commentState with pooled instances in various emitComments methods.

  • Added a core.Pool[commentState] field to Printer
  • Updated emitCommentsBeforeNode to retrieve and initialize a pooled commentState
  • Switched emitCommentsBeforeToken and emitCommentsAfterToken to use pooled instances instead of fresh allocations

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

This seems fine, but Q: Does this need to be a heap allocated pointer at all? printerState, which always hosts it, is by-value already. I'm pretty sure the comment state is created by emitCommentsBeforeNode and then only consumed by emitCommentsAfterNode (or similar token/list functions) at the same print depth, and the fields fit in a few registers easy.

@jakebailey
Copy link
Member Author

emitCommentsAfterNode returns early on nil; I tried using a zero value but it breaks things because a zero value is actually seemingly valid. ☹️

@jakebailey jakebailey added this pull request to the merge queue Jun 30, 2025
Merged via the queue into main with commit 37a196a Jun 30, 2025
22 checks passed
@jakebailey jakebailey deleted the jabaile/comment-state-pool branch June 30, 2025 16:10
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