Conversation
Every output line's indentation was a fresh `" ".repeat(indent)`. JavaOutput.spaces(int) now hands out one String per width up to 100 spaces, built on first use; wider widths are built as before, and zero or less gives the empty string. Taken from google/google-java-format#1466 (Liam Miller-Cushon), minus its Style.indentString half, which has no counterpart here: the fork's Style knows no tabs, and JavaOutput.indent is the only caller. JavaOutputTest asks for the same instance twice, for the empty string at zero and below, and for a width beyond the cache. Measured on the 135 files of java.util from JDK 21, six passes on one thread, bytes from ThreadMXBean.getThreadAllocatedBytes: 1,980 MB per pass before, 1,977 MB after, about 30 bytes per output line out of 17,770. The time per pass does not move, and the JDK 21 sources format exactly as before.
Collaborator
Author
|
Closed unmerged: the measured gain is 0.2 % of what formatting allocates, which does not pay for the extra code. The regex port, #104, stays. |
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.
Port of google/google-java-format#1466 (Liam Miller-Cushon, author kept). Every output line's indentation was a fresh
" ".repeat(indent);JavaOutput.spaces(int)now hands out oneStringper width up to 100 spaces, built on first use. Wider widths are built as before, zero or less gives the empty string. Upstream's second half,Style.indentStringdelegating to the cache, has no counterpart here: the fork'sStyleknows no tabs, andJavaOutput.indentis the only caller.TDD
JavaOutputTestwas written first and did not compile: the same instance for the same width, the empty string at zero and below, and a width beyond the cache.What it buys
Measured with a small harness (
ThreadMXBean.getThreadAllocatedByteson one thread, the 135java.utilfiles of JDK 21, six warm-up and six measured passes, three JVMs each):About 30 bytes per output line, 0.2 % of what formatting allocates, and the time does not move. It is upstream alignment more than a measurable win; merging decides whether that is worth the twenty lines.
Checks
:open-java-format:teston JDK 21: 1600 tests, 0 failures.