Skip to content

Stop copying comment text through reflection in JavacTokens - #287

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/stop-copying-comment-text-through-reflection-in
Nov 28, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/stop-copying-comment-text-through-reflection-in

Conversation

@abashev

@abashev abashev commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #286, a port of google-java-format#1546. Based on 21.X.X; the one other open pull request, #271, touches neither file.

CommentSavingTokenizer.processComment looked up JavaTokenizer's reader field for every comment. javac has had no such field since JDK 15, where JavaTokenizer became a UnicodeReader, so on every JDK we run on the lookup threw a NoSuchFieldException before the fallback called getRawCharacters(int, int) on the tokenizer itself, reflectively. -Xlog:exceptions=info on the command line counts 119 of them for java/util/ArrayList.java, one per comment and one for the //EOF the lexer is fed. The copy then went into an AccessibleReader and a String, and getTokens read nothing of it but its length.

  • JavacTokens. A comment keeps its start, end and style, and getTokens takes the range as it is. getText, which only toString calls, reads the lexer's buffer. AccessibleReader has no use left and goes. Upstream still calls super.processComment, which copies the characters into a BasicComment; our tokenizer never did, so it copies nothing now. In the javac of JDK 21 and of JDK 27 every comment the tokenizer makes comes from processComment, /// comments included. A comment of another class would mean a bug in JavacTokens, and getTokens throws an IllegalStateException that says so. Skipping such a comment instead would lose it: its text would become part of the whitespace before the next token, which the formatter drops.
  • The native image's reachability-metadata.json loses the three entries that were there for the reflection: JavacTokens$CommentSavingTokenizer, the field JavaTokenizer.reader and UnicodeReader.getRawCharacters(int, int).

Allocation and time

JavacTokens.getTokens on java/util/ArrayList.java, 200 calls a round in one thread, on Temurin 21.0.12:

Before After
Allocated per call 1,576 KB 1,373 KB (−13%)
Time per call, rounds 2 and 5 0.71 ms, 0.62 ms 0.62 ms, 0.65 ms

Once the JIT has warmed up the time is the same; only the first round is faster. Formatting the whole file takes about 17 ms and allocates about 32 MB either way.

Output

The old and the new allJar formatted java.base/java and jdk.compiler from the src.zip of each JDK in place: 1,795 files on JDK 21 and 1,969 on JDK 27, 17 of which have /// comments. diff -r finds no difference, and the command line printed nothing to stderr either time. The native binary, built with GraalVM 25, formats the JDK 21 files as the old jar does.

Tests

./gradlew :open-java-format:check passes on JDK 21, 1,439 tests; :open-java-format:test -PjavaRuntime=27 passes, 2,357 tests. No formatting changes, so no new examples.

@abashev
abashev force-pushed the abashev/stop-copying-comment-text-through-reflection-in branch from 380774a to 8f48925 Compare November 28, 2026 09:45
CommentSavingTokenizer looked up JavaTokenizer's reader field for
every comment, a field javac has not had since JDK 15, where
JavaTokenizer became a UnicodeReader. Each lookup threw a
NoSuchFieldException, and the fallback then called getRawCharacters
on the tokenizer itself, reflectively. The copy it made became an
AccessibleReader and then a String, read only for its length.

A comment now keeps its start and end, and getTokens takes them as
they are; getText, which only toString calls, reads the lexer's
buffer. A comment of another class would mean a bug in JavacTokens,
and getTokens throws an IllegalStateException that says so rather
than lose the comment. The native image's metadata loses the three
entries that were there for the reflection. Ports
google-java-format#1546.

Fixes #286.
@abashev
abashev force-pushed the abashev/stop-copying-comment-text-through-reflection-in branch from 8f48925 to 5b0188a Compare November 28, 2026 10:12
@abashev
abashev merged commit c71319f into 21.X.X Nov 28, 2026
26 of 27 checks passed
@abashev
abashev deleted the abashev/stop-copying-comment-text-through-reflection-in branch November 28, 2026 16:28
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.

Stop copying comment text through reflection in JavacTokens

1 participant