Skip to content

perf(format): stop re-parsing and re-tokenizing work the formatter already has - #8

Merged
kvlonge merged 1 commit into
mainfrom
perf/formatter-redundant-work
Oct 31, 2026
Merged

kvlonge merged 1 commit into
mainfrom
perf/formatter-redundant-work

Conversation

@kvlonge

@kvlonge kvlonge commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Why

Profiling sqlclean format showed two pieces of repeated work. Statement splitting collected every statement's facts, such as tables, joins and columns, only to read the statement ends. Every print also re-tokenized the text the printer had just built from known tokens.

Changes

  • A new statement_ends function gives format_source the statement boundaries. It still parses the whole file and checks the statement count, so files with opaque nodes or blocks that span semicolons are still refused, but it skips fact collection. file_facts shares the same helper.
  • print_authored_tokens returns the byte range and type of each token it printed, and PrintedSql::new builds from them instead of re-tokenizing.
  • Unchanged: the final token-preservation check, the comment-attachment and string-break checks, and the idempotence re-print. These still re-tokenize the rendered output and compare it.

Verification

  • On 10,282 Snowflake files, sqlclean format output is byte-identical to main.
  • format --check, 4 CPUs, median of 5 runs: wall 2.97 s → 2.76 s (−7%), CPU 11.35 s → 10.32 s (−9%), peak RSS 50 MiB → 50 MiB.
  • New regression tests cover statement ends (a semicolon in a literal, comments after terminators, empty statements, comment-only files, a parse error), plus formatting with multibyte comments and literals. The multibyte case passes on both the old and the new printer.
  • make verify and fensu check pass.

…ready has

Statement splitting in sqlclean format now parses the file and checks the
statement count without collecting every statement's facts. The layout
passes now take the printer's own token ranges instead of re-tokenizing the
text it just printed. The final token check and the idempotence re-print
still re-tokenize and compare, so every safety check stays.

Amp-Thread-ID: https://ampcode.com/threads/T-01a18b8a-340f-717d-9ca7-6dcf564a065a
Co-authored-by: Kevin Longe <kvlonge@gmail.com>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying sqlclean with  Cloudflare Pages  Cloudflare Pages

Latest commit: b4ce12f
Status: ✅  Deploy successful!
Preview URL: https://8a1bf672.sqlclean.pages.dev
Branch Preview URL: https://perf-formatter-redundant-wor.sqlclean.pages.dev

View logs

@kvlonge
kvlonge merged commit ae48505 into main Oct 31, 2026
15 checks passed
This was referenced Oct 31, 2026
kvlonge added a commit that referenced this pull request Oct 31, 2026
…ready has (#8)

Statement splitting in sqlclean format now parses the file and checks the
statement count without collecting every statement's facts. The layout
passes now take the printer's own token ranges instead of re-tokenizing the
text it just printed. The final token check and the idempotence re-print
still re-tokenize and compare, so every safety check stays.
@kvlonge
kvlonge deleted the perf/formatter-redundant-work branch October 31, 2026 08:34
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.

2 participants