Repository navigation
Optimize StringWrapper fast-path, parse reuse, and AST verification - #1533
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
Reduces `StringWrapper.wrap` overhead on files with long lines or long string literals while preserving its AST-equivalence safety guarantees: - Replace allocating `Newlines.lineIterator` in `needWrapping` with a zero-allocation line scan that also requires `"` on the long line and intersection with `characterRanges` (skipping files whose only long lines are comments, imports, or signatures without quotes). - Reuse `Reflower`'s parsed `JCCompilationUnit` in `firstPass` (`formatter.getFormatReplacements`), and skip duplicate `reflow()` work when multiple long lines belong to the same concatenation chain. - When called from `Formatter.formatSource` on whole-file ranges, pass `alreadyFormattedWholeFile = true` (skipping the redundant `firstPass`) and reuse `cleaned.unit()` when no whitespace edits occurred. - Replace 2x full-compilation-unit `parse(..., allowStringFolding=true).toString()` verification with a tiny synthetic class containing only the replacement expressions parsed with `allowStringFolding=true` and checked against the concatenated original `LiteralTree.getValue()`s. For any enclosing expression whose parent could change operator precedence (e.g. `TypeCastTree`, `MULTIPLY`/`DIVIDE`/`REMAINDER`/`MINUS`/`PLUS`, `UnaryTree`), fall back to full-file AST comparison (`verifyFullAst`). - Add `StringWrapperTest.wrap_typeCastParentFallsBackToFullAstCheck` and `StringWrapperTest.wrap_newClassEnclosingExpressionFallsBackToFullAstCheck`. Benchmarks (warm in-JVM `ThreadMXBean` CPU time): - `JavaInputAstVisitor.java` (140 KB + long URL comment): med 145.6 -> 117.5 ms (1.24x). - `Formatter.java` (dirty imports + 10 long strings): med 36.1 -> 23.4 ms (1.54x), min 29.4 -> 15.2 ms (1.93x). - `JavaInputAstVisitor.java` (dirty imports + 10 long strings): med 310.2 -> 160.7 ms (1.93x), min 261.8 -> 128.1 ms (2.04x). - Direct `StringWrapper.wrap` (142 KB, 10 long strings): med 150.9 -> 99.7 ms (1.51x). PiperOrigin-RevId: 1025905861
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.
Optimize StringWrapper fast-path, parse reuse, and AST verification
Reduces
StringWrapper.wrapoverhead on files with long lines or long stringliterals while preserving its AST-equivalence safety guarantees:
Newlines.lineIteratorinneedWrappingwith azero-allocation line scan that also requires
"on the long line andintersection with
characterRanges(skipping files whose only long lines arecomments, imports, or signatures without quotes).
Reflower's parsedJCCompilationUnitinfirstPass(
formatter.getFormatReplacements), and skip duplicatereflow()work whenmultiple long lines belong to the same concatenation chain.
Formatter.formatSourceon whole-file ranges, passalreadyFormattedWholeFile = true(skipping the redundantfirstPass) andreuse
cleaned.unit()when no whitespace edits occurred.parse(..., allowStringFolding=true).toString()verification with a tiny synthetic class containing only the replacement
expressions parsed with
allowStringFolding=trueand checked against theconcatenated original
LiteralTree.getValue()s. For any enclosing expressionwhose parent could change operator precedence (e.g.
TypeCastTree,MULTIPLY/DIVIDE/REMAINDER/MINUS/PLUS,UnaryTree), fall back tofull-file AST comparison (
verifyFullAst).StringWrapperTest.wrap_typeCastParentFallsBackToFullAstCheckandStringWrapperTest.wrap_newClassEnclosingExpressionFallsBackToFullAstCheck.Benchmarks (warm in-JVM
ThreadMXBeanCPU time):JavaInputAstVisitor.java(140 KB + long URL comment): med 145.6 -> 117.5 ms (1.24x).Formatter.java(dirty imports + 10 long strings): med 36.1 -> 23.4 ms (1.54x),min 29.4 -> 15.2 ms (1.93x).
JavaInputAstVisitor.java(dirty imports + 10 long strings): med 310.2 -> 160.7 ms(1.93x), min 261.8 -> 128.1 ms (2.04x).
StringWrapper.wrap(142 KB, 10 long strings): med 150.9 -> 99.7 ms (1.51x).