Skip to content

Reuse Parser.parse results - #622

Closed
qwwdfsad wants to merge 3 commits into
Kotlin:mainfrom
qwwdfsad:optimize-ast-parsing
Closed

qwwdfsad wants to merge 3 commits into
Kotlin:mainfrom
qwwdfsad:optimize-ast-parsing

Conversation

@qwwdfsad

@qwwdfsad qwwdfsad commented Jun 3, 2026 •

Copy link
Copy Markdown
Member

Cuts down formatting time on coroutines from 4.7 to ~2.9-3 seconds.
All explanations are in commit messages (note: they should be reviewed separately, the first one addresses a non-functional but necessary change)

Partially addresses #619
Fixes #552

qwwdfsad added 2 commits June 3, 2026 14:52
Previously, sortedAndDistinctImports unconditionally appended a new line to the imports, which was redundant most of the time and later was fixed by prettyPrint.

Now we explicitly check for that and avoid redundant transformation when it can be avoided. It has a negligible performance impact but is important for a later change with the reuse of Parser.parse results
Introduce FormattingContext that encapsulates partially formatted code state and PSI, and avoids invocations of Parser.parse when it is clearly redundant.

On a properly formatted code, it reduces the number of parses from 6 to 1, leading to a ~40% throughput improvement.

All public-API formatting steps now have an internal method that accepts KtFile directly
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 3, 2026
Comment thread core/src/main/java/com/facebook/ktfmt/format/FormatterContext.kt
Comment thread core/src/main/java/com/facebook/ktfmt/format/RedundantElementManager.kt Outdated
@meta-codesync

meta-codesync Bot commented Jun 15, 2026

Copy link
Copy Markdown

@hick209 has imported this pull request. If you are a Meta employee, you can view this in D108656440.

@cortinico cortinico left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in 78ac5d7 Jun 17, 2026
@meta-codesync meta-codesync Bot added the Merged label Jun 17, 2026
@meta-codesync

meta-codesync Bot commented Jun 17, 2026

Copy link
Copy Markdown

@hick209 merged this pull request in 78ac5d7.

@hick209 hick209 linked an issue Jun 17, 2026 that may be closed by this pull request
3 of 5 tasks
@qwwdfsad qwwdfsad mentioned this pull request Jul 1, 2026
3 of 5 tasks
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
ktfmt 0.64

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>### Changed

* Reduced overall number of allocations to improve formatting performance (~6-7%) (Kotlin/ktfmt#620) – @qwwdfsad
* Reuse results of `Parser.parse` (Kotlin/ktfmt#622) – @qwwdfsad
* Fix trailing lambda with chained call indentation (Kotlin/ktfmt#626) – @ptitjes
* Refactor build scripts (Kotlin/ktfmt#623) – @AbdullinAM
* Switch in-repo formatting to simple local gradle task (#628) – @ZacSweers

### Fixed

* Support name-based destructuring declarations. (Kotlin/ktfmt#629) – @goutamadwant

Huge thanks to all the contributors! 🎉

**Full Changelog**: https://github.com/facebook/ktfmt/compare/v0.63...v0.64</pre>
  <p>View the full release notes at <a href="https://github.com/facebook/ktfmt/releases/tag/v0.64">https://github.com/facebook/ktfmt/releases/tag/v0.64</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!12237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Low-hanging fruits in ktfmt performance Four calls to Parser.parse cause performance issues

3 participants