Skip to content

Cache JavaInput in Cleanups and fuse AST cleanup passes - #1532

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

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

Conversation

@copybara-service

Copy link
Copy Markdown

Cache JavaInput in Cleanups and fuse AST cleanup passes

Eliminates redundant full-file lexing and parsing across Cleanups,
CleanupPass, and Formatter.getFormatReplacements:

  • Retain ImmutableList<Tok> on JavaInput (getToks()), cache JavaInput in
    Cleanups (invalidated alongside parsed in applyReplacements), and return
    it in Cleanups.Result so CleanupScanner, ImportOrderer (when javaInput
    is already cached), ModifierOrderer, and Formatter.getFormatReplacements
    share a single full-file lex instead of 3 full-file lexes + 1 prefix lex on
    clean files.
  • Fuse REMOVE_UNUSED_IMPORTS and AST_CLEANUPS onto a single shared
    JCCompilationUnit parse (applyFusedAstPasses), passing
    preExistingDeletions into CleanupScanner so adjacent deletions (such as
    import java.util.List;;) remain identical.
  • Order SORT_IMPORTS before REORDER_MODIFIERS in CleanupPass so when
    imports are sorted (invalidating javaInput), SORT_IMPORTS only does a
    prefix lex (CLASS_START) and REORDER_MODIFIERS then builds JavaInput
    once for Formatter to reuse.
  • Update CleanupsTest to cover fused AST pass behavior, javaInput reuse, and
    adjacent unused-import + redundant-semicolon deletion.

Benchmarks (warm in-JVM ThreadMXBean CPU time):

  • JavaInputAstVisitor.java (140 KB, clean): med 178.1 -> 146.1 ms (1.22x),
    min 147.4 -> 105.2 ms (1.40x).

Eliminates redundant full-file lexing and parsing across `Cleanups`,
`CleanupPass`, and `Formatter.getFormatReplacements`:
- Retain `ImmutableList<Tok>` on `JavaInput` (`getToks()`), cache `JavaInput` in
  `Cleanups` (invalidated alongside `parsed` in `applyReplacements`), and return
  it in `Cleanups.Result` so `CleanupScanner`, `ImportOrderer` (when `javaInput`
  is already cached), `ModifierOrderer`, and `Formatter.getFormatReplacements`
  share a single full-file lex instead of 3 full-file lexes + 1 prefix lex on
  clean files.
- Fuse `REMOVE_UNUSED_IMPORTS` and `AST_CLEANUPS` onto a single shared
  `JCCompilationUnit` parse (`applyFusedAstPasses`), passing
  `preExistingDeletions` into `CleanupScanner` so adjacent deletions (such as
  `import java.util.List;;`) remain identical.
- Order `SORT_IMPORTS` before `REORDER_MODIFIERS` in `CleanupPass` so when
  imports are sorted (invalidating `javaInput`), `SORT_IMPORTS` only does a
  prefix lex (`CLASS_START`) and `REORDER_MODIFIERS` then builds `JavaInput`
  once for `Formatter` to reuse.
- Update `CleanupsTest` to cover fused AST pass behavior, `javaInput` reuse, and
  adjacent unused-import + redundant-semicolon deletion.

Benchmarks (warm in-JVM `ThreadMXBean` CPU time):
- `JavaInputAstVisitor.java` (140 KB, clean): med 178.1 -> 146.1 ms (1.22x),
  min 147.4 -> 105.2 ms (1.40x).

PiperOrigin-RevId: 1025905856
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