Skip to content

Cache tok stream and JavaInput in Cleanups - #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

@copybara-service copybara-service Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Cache tok stream and JavaInput in Cleanups

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

  • Add a package-private JavaInput(String, ImmutableList<Tok>) constructor so
    Cleanups can cache @Nullable ImmutableList<Tok> toks and @Nullable JavaInput javaInput as separate lazy fields (invalidated alongside parsed
    in applyReplacements) without retaining toks on JavaInput for all
    callers.
  • Return toks and javaInput in Cleanups.Result so AST_CLEANUPS shares
    its full-file lex with REORDER_MODIFIERS and SORT_IMPORTS, and
    Formatter reuses javaInput directly or builds new JavaInput(contents, toks) when --skip-reordering-modifiers is set.
  • Update CleanupsTest to cover toks/javaInput reuse, ImportOrderer
    cached-toks reuse, adjacent unused-import + redundant-semicolon deletion,
    and --fix-imports-only not eagerly building JavaInput.

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 across `Cleanups`, `CleanupPass`,
`ImportOrderer`, and `Formatter.getFormatReplacements`:
- Add a package-private `JavaInput(String, ImmutableList<Tok>)` constructor so
  `Cleanups` can cache `@Nullable ImmutableList<Tok> toks` and `@Nullable
  JavaInput javaInput` as separate lazy fields (invalidated alongside `parsed`
  in `applyReplacements`) without retaining `toks` on `JavaInput` for all
  callers.
- Return `toks` and `javaInput` in `Cleanups.Result` so `AST_CLEANUPS` shares
  its full-file lex with `REORDER_MODIFIERS` and `SORT_IMPORTS`, and
  `Formatter` reuses `javaInput` directly or builds `new JavaInput(contents,
  toks)` when `--skip-reordering-modifiers` is set.
- Update `CleanupsTest` to cover `toks`/`javaInput` reuse, `ImportOrderer`
  cached-`toks` reuse, adjacent unused-import + redundant-semicolon deletion,
  and `--fix-imports-only` not eagerly building `JavaInput`.

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
@copybara-service copybara-service Bot changed the title Cache JavaInput in Cleanups and fuse AST cleanup passes Cache tok stream and JavaInput in Cleanups Nov 25, 2026
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