Skip to content

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 into
21.X.Xfrom
abashev/the-formatter-allocates-over-1-000-times-its-inp
Nov 24, 2026
Merged

abashev merged 3 commits into
21.X.Xfrom
abashev/the-formatter-allocates-over-1-000-times-its-inp

Conversation

@abashev

@abashev abashev commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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.base in JDK 21:

  • 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: 2.9 GB on two lines, 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.
  • Doc and Level memoized the width, flat text and range of a doc, and the split breaks and depth of a level, with Suppliers.memoize(...)::get: a method reference, Guava's memoizing supplier, an object it locks on and the ::get of it. That is 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.hasCommentByFirstWord already was. 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. The third commit drops Error Prone's @Immutable from Break and NonBreakingSpace, which proved them immutable only with @SuppressWarnings("Immutable") on the fields of Doc, now not final; nothing else needed it. Both stay values that do not change once made: NonBreakingSpace has no fields, and Immutables generates those of Break final.

Allocation

FormatFileCallable with the command line's defaults, in one thread, on Temurin 21.0.12, after a round to warm up:

Before OpsBuilder Both commits
1,599 files of java.base 23.1 GB, 9.7 s 20.9 GB, 9.3 s 19.1 GB (−17%), 8.7 s (−11%)
The same files, formatted already 18.5 GB, 7.9 s 16.7 GB, 7.7 s 15.2 GB (−18%), 6.9 s (−12%)

Memory

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:

Before After
Wall 9.7 s, 10.1 s 8.5 s, 8.8 s
Full collections 33, 31 13, 14
GC pauses 3.3 s, 3.2 s 1.7 s, 1.9 s

Output

The old and the new allJar formatted 5,996 files in place twice on JDK 21: java.base and jdk.compiler from JDK 21's src.zip and the sources of eleven libraries from the Gradle cache. diff -r finds no difference.

Tests

./gradlew :open-java-format:check passes on JDK 21, 1,438 tests; :open-java-format:test -PjavaRuntime=25 passes, 2,356 tests.

Not in this PR

  • Items 2 and 3 of The formatter allocates over 1,000 times its input, and the native binary spends most of its run collecting it #229, javac set up once per file and one parse for the steps that read the same text, were tried. They cut the allocation by a further 12–18 %, but the javac of a file then kept every text it had read alive until the file was done (Log.sourceMap holds the JavaFileObject of 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.
  • A file with more than a hundred methods without a return type, which the formatter accepts though javac reports each as an error, hides a syntax error after them: javac stops reporting errors past -Xmaxerrs, 100 by default. That was so before too, and is for another pull request.

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
abashev merged commit ee0f61a into 21.X.X Nov 24, 2026
16 checks passed
@abashev
abashev deleted the abashev/the-formatter-allocates-over-1-000-times-its-inp branch November 24, 2026 09:31
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.

1 participant