Skip to content

Improve memory usage of formatter #1107

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 12 commits into from
Jun 10, 2025
Merged

Conversation

weswigham
Copy link
Member

@weswigham weswigham commented Jun 9, 2025

And throw in a bugfix for the road, plus a (disabled) entrypoint that can make testing formatter changes on the cli easier. Combined, these lower the total count of allocations from around 2 million to 750k per checker.ts format. Byte-wise, it goes from around 113MB allocated to 83MB (36% savings). These, in turn, help performance on cache-limited systems substantially.

@weswigham weswigham requested review from jakebailey and Copilot June 9, 2025 19:51
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

Adds a dedicated fmtMain entrypoint with a benchmark test and refactors the formatter to use value‐typed spans, reuse rule slices, and fix indentation logic—reducing memory allocations substantially.

  • Switch *TextRangeWithKind and *tokenInfo to value types and use zero values as sentinels
  • Introduce currentRules slice reuse to cut down allocations
  • Add fmtMain and applyBulkEdits for standalone formatting plus benchmark in fmt_test.go

Reviewed Changes

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

Show a summary per file
File Description
internal/format/span.go Switch to value‐typed spans; reuse currentRules
internal/format/scanner.go Switch to value‐typed tokenInfo; track last info via bool
internal/format/rulesmap.go Update getRules to accept and return reusable slice
internal/format/indent.go Guard against -1 indent and return correct delta
internal/format/context.go Drop pointer fields in formattingContext
internal/execute/tsc.go Add fmtMain, applyBulkEdits
internal/execute/fmt_test.go Add benchmark to profile formatting allocations
Comments suppressed due to low confidence (3)

internal/format/span.go:274

  • Comparing value-typed spans against NewTextRangeWithKind(0,0,0) uses a freshly allocated loc and will never equal the zero value sentinel; you need a reliable sentinel (e.g. a boolean flag or check previousRange.Loc == nil).
if w.previousRange != NewTextRangeWithKind(0, 0, 0) && w.formattingScanner.getTokenFullStart() >= w.originalRange.End() {

internal/format/span.go:292

  • Unconditionally using tokenInfo.Loc on a zero‐value TextRangeWithKind will panic when Loc is nil; guard with an explicit flag or ensure tokenInfo was actually initialized before accessing its Loc.
if tokenInfo.Loc.Pos() == w.previousRangeTriviaEnd {

internal/execute/tsc.go:26

  • strings.Builder is used but "strings" is not imported; add import "strings" to avoid a compile error.
func applyBulkEdits(text string, edits []core.TextChange) string {

@weswigham weswigham requested a review from gabritto June 9, 2025 19:53
@weswigham weswigham requested a review from jakebailey June 9, 2025 23:46
Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

The actual perf changes seem good, though I think the PR title should be updated to talk just about the perf given the code for the formatting test entrypoint is commented out. (Do we need that code? Can we just keep it locally or just use regular benchmarks?)

@weswigham weswigham changed the title Add extra test entrypoint for formatter for profiling Improve memory usage of formatter Jun 10, 2025
@weswigham
Copy link
Member Author

weswigham commented Jun 10, 2025

Do we need that code?

I'd like to make it a public entrypoint to the compiler at some point in the future (alongside other LS features), but for now it's useful to keep around as a quick edit for testing and making changes to the formatter without waiting for a full vscode extension reload.

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

Seems fine for now for testing purposes; adding in the formatter as part of the CLI I think will take further discussion. I think a gopls esque model might be the way to go there, so long as we can make the Project system not load any programs in the course of doing non-semantic functionality like formatting.

(That and, configuring it sounds like a nightmare. I'm not sure how much we really want people depending on this anyway.)

@weswigham weswigham added this pull request to the merge queue Jun 10, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jun 10, 2025
@jakebailey
Copy link
Member

Ah, I broke this via #979

@weswigham weswigham requested a review from jakebailey June 10, 2025 18:51
@weswigham weswigham added this pull request to the merge queue Jun 10, 2025
Merged via the queue into microsoft:main with commit 393f055 Jun 10, 2025
23 checks passed
@weswigham weswigham deleted the fmt-test branch June 10, 2025 20:48
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