Performance Optimization: Formatting Modes & String Generation - #277
Merged
Conversation
… (no, jdt, LIGHTWEIGHT) and updating documentation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
…eFormatter for external configuration
…other configurations
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
This PR introduces configurable formatting modes for generated builder source code and investigates/optimizes a major performance bottleneck in Roaster's string generation.
1. Configurable Formatting Modes (
FormattingMode)Adds a new compiler option
-Asimplebuilder.formattingModewith three modes:JDT(default)Roaster.format()LIGHTWEIGHTNONEThis gives users control over the performance/quality trade-off, especially useful in large codebases where the JDT formatter is a bottleneck.
2. String Generation Investigation
During investigation, we identified that
Roaster.toUnformattedString()internally callsCompilationUnit.rewrite(), an expensive Eclipse JDT operation that accounts for ~18s of processing time on the performance test project (1077 classes).3. Performance Breakdown
Performance measured on the performance-test project (1077 builder classes):
Changes
Committed (this PR)
FormattingModeenum (core): New enum withJDT,LIGHTWEIGHT,NONEmodesSourceFormatterclass (processor): Handles formatting dispatch based on mode, including lightweight formatter implementationCompilerArgumentsEnum: AddedFORMATTING_MODEargumentRoasterCodeGenerator: UsesSourceFormatterfor formatting, removed inline formatting logicBuilderProcessor: ReadsformattingModecompiler argument and passes it throughFormattingModeTest: Comprehensive tests for bothLIGHTWEIGHTandJDTmodesCONFIGURATION.mdwith formatting mode documentationFuture Optimization Opportunities
The investigation revealed that both major bottlenecks — String Generation (~18s) and JDT Formatting (~24s) — are fundamentally caused by Roaster's internal use of Eclipse JDT. The stashed reflection-based workaround only addresses the symptom, not the root cause.
The most promising path forward is to replace Roaster with a direct
StringBuilder-based source generator. This would eliminate both bottlenecks at once:rewrite()call — A StringBuilder approach generates the string directly, bypassing the expensive JDTCompilationUnit.rewrite()that accounts for ~18s.roaster-jdtdependency (and its shaded Eclipse JDT), reducing jar size and classpath complexity.The existing
SourceFormatter.lightweightFormat()already demonstrates that minimal post-processing (tab→space, blank line collapsing, javadoc prefixes) is sufficient for readable output. A StringBuilder generator would produce properly formatted code from the start, making evenLIGHTWEIGHTpost-processing optional.Test Results
FormattingModeTestverifies bothLIGHTWEIGHTandJDToutput qualityCloses #274