Skip to content

Optimize StringWrapper fast-path, parse reuse, and AST verification - #1533

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1025905861
Open

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1025905861

Conversation

@copybara-service

Copy link
Copy Markdown

Optimize StringWrapper fast-path, parse reuse, and AST verification

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).

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
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