Skip to content

Stop comments that share a line from doubling each other's indent - #42

Merged
abashev merged 1 commit into
mainfrom
multiline-comment-column
Sep 23, 2026
Merged

abashev merged 1 commit into
mainfrom
multiline-comment-column

Conversation

@abashev

@abashev abashev commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Closes #36. Upstream report: google/google-java-format#413.

What changes

Comment.computeBreaks set the column after a comment to the column it started at plus the length of its last line. For a comment that spans lines, JavaCommentsHelper.rewrite has already indented that last line to the start column, so the column was counted twice, and every further comment on the same line started twice as far right as the one before. Commented-out code with javadoc in it, where each *//** javadoc *//* puts several multi-line comments on one line, grew exponentially:

comments on the line before after
16 4.3 MB 5.2 kB
26 OutOfMemoryError with a 2 GB heap 12.5 kB in 0.33 s

After a comment that spans lines the column is now the length of its last line; after a one-line comment it is still the start column plus the comment.

Checked

  • The new golden ojf-issue-36-multiline-comments-on-one-line (the file from OutOfMemoryError on formating file with no actual code google/google-java-format#413) and FormatterTest.indentsCommentsThatShareALineLinearly fail without the change: the golden's format and its three idempotence checks, and the size test. All pass with it.
  • ./gradlew :open-java-format:test on JDK 21: 1422 tests, all green.
  • The 15,747 files of the JDK 21 sources format byte for byte as before.

Comment.computeBreaks moved the column past a comment by adding the
length of the comment's last line to the column the comment started at.
For a comment that spans lines, JavaCommentsHelper.rewrite has already
indented that last line to the start column, so the column was counted
twice, and each further comment on the line was re-indented from the
doubled column. Commented-out code with javadoc in it, where every
"*//** javadoc *//*" puts several multi-line comments on one line, grew
from 817 bytes to 4.3 MB with 16 of them and ran out of a 2 GB heap
with 26 (#36, from google/google-java-format#413).

After a comment that spans lines the column is now the length of its
last line; after a one-line comment it is still the start column plus
the comment. The 26 comments now format into 12.5 kB in 0.33 s, each
indented to the column where it starts.

A golden of the reported file and a test with 16 such comments fail
without the change. The 15,747 files of the JDK 21 sources format
exactly as before.
@abashev
abashev enabled auto-merge September 23, 2026 07:28
@abashev
abashev disabled auto-merge September 23, 2026 08:20
@abashev
abashev merged commit eda37c1 into main Sep 23, 2026
10 checks passed
@abashev
abashev deleted the multiline-comment-column branch September 23, 2026 08:20
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.

Multi-line comments on one line double the indent each time, up to an OutOfMemoryError

1 participant