Repository navigation
Allocate a sixth less in the layout: List.of() for OpsBuilder's empty lists, plain fields for what Doc memoizes - #269
Merged
abashev merged 3 commits intoNov 24, 2026
Conversation
OpsBuilder.build asks tokOps and reservations for the ops to insert at every op, and most ops have none. ArrayListMultimap.get makes a new empty list and a view of it for such a key, and the loop over it two iterators more. The two lines allocated 2.9 GB while formatting the 1,599 files of java.base in JDK 21, more than any other line of the formatter. Both are a ListMultimap now that returns List.of() for a key without values and leaves the rest to an ArrayListMultimap. build only reads what get returns, so that this list cannot be added to does not matter. Formatting those files allocates 20.9 GB instead of 23.1 GB, the output unchanged. Part of #229.
Each Doc held three Suppliers.memoize(...)::get, and each Level two more. For each that is a method reference, Guava's memoizing supplier, an object it locks on and the ::get of it: twelve objects for every Doc, which live as long as the Doc, and a Float for its width. They are plain fields now, computed when first asked for, as Level's hasCommentByFirstWord already was. The width is NaN until then, which computeWidth never returns, the depth of a Level -1, the others null. A Doc tree is built and laid out within one call of Formatter.format, in one thread, so the fields need no synchronization. ImmutableSupplier has no use left and goes. Formatting the 1,599 files of java.base in JDK 21 allocates 19.1 GB instead of 20.9 GB, the output unchanged. Part of #229.
The width, flat text and range a Doc memoizes are plain fields now, and Error Prone proves a class @immutable only when every field of its superclasses is final. Break and NonBreakingSpace, the two docs annotated so, needed @SuppressWarnings("Immutable") on those fields of Doc for it. Nothing else needed the annotation: without it, and without the suppressions, the module builds and checks as before. Both classes stay values that do not change once made: NonBreakingSpace has no fields, and Immutables generates the fields of Break final. Part of #229.
abashev
deleted the
abashev/the-formatter-allocates-over-1-000-times-its-inp
branch
November 24, 2026 09:31
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.
Part of #229. Based on
21.X.X; there are no other open pull requests.Two of the largest allocation sites of the layout, found by recording every TLAB allocation with Flight Recorder while the formatter went through the 1,599 files of
java.basein JDK 21:OpsBuilder.buildaskstokOpsandreservationsfor the ops to insert at every op, and most ops have none.ArrayListMultimap.getmakes a new empty list and a view of it for such a key, and the loop over it two iterators more: 2.9 GB on two lines, more than any other line of the formatter. Both are aListMultimapnow that returnsList.of()for a key without values and leaves the rest to anArrayListMultimap.buildonly reads whatgetreturns, so that this list cannot be added to does not matter.DocandLevelmemoized the width, flat text and range of a doc, and the split breaks and depth of a level, withSuppliers.memoize(...)::get: a method reference, Guava's memoizing supplier, an object it locks on and the::getof it. That is twelve objects for everyDoc, which live as long as theDoc, and aFloatfor its width. They are plain fields now, computed when first asked for, asLevel.hasCommentByFirstWordalready was. ADoctree is built and laid out within one call ofFormatter.format, in one thread, so the fields need no synchronization.ImmutableSupplierhas no use left and goes. The third commit drops Error Prone's@ImmutablefromBreakandNonBreakingSpace, which proved them immutable only with@SuppressWarnings("Immutable")on the fields ofDoc, now not final; nothing else needed it. Both stay values that do not change once made:NonBreakingSpacehas no fields, and Immutables generates those ofBreakfinal.Allocation
FormatFileCallablewith the command line's defaults, in one thread, on Temurin 21.0.12, after a round to warm up:OpsBuilderjava.baseMemory
The
Docs are what the layout keeps alive, and their suppliers lived as long, so less is live at once too. The command line on the same files with-Xmx320m, 10 cores, two runs each:Output
The old and the new
allJarformatted 5,996 files in place twice on JDK 21:java.baseandjdk.compilerfrom JDK 21'ssrc.zipand the sources of eleven libraries from the Gradle cache.diff -rfinds no difference.Tests
./gradlew :open-java-format:checkpasses on JDK 21, 1,438 tests;:open-java-format:test -PjavaRuntime=25passes, 2,356 tests.Not in this PR
Log.sourceMapholds theJavaFileObjectof each), and the live set after a full collection of the native binary grew by about 15 %, its peak by a quarter. They are left out.-Xmaxerrs, 100 by default. That was so before too, and is for another pull request.