Skip to content

perf(format): remove repeated work from the native SQL formatter - #1188

Merged
kvlonge merged 11 commits into
mainfrom
perf/formatter-repeated-work
Oct 21, 2026
Merged

kvlonge merged 11 commits into
mainfrom
perf/formatter-repeated-work

Conversation

@kvlonge

@kvlonge kvlonge commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Why

A cold sqb format --check --no-cache on a synthetic 10,000-model project spends about 78% of its instructions in the native SQL formatter, much of it repeating work on identical inputs. Separately, a body ending in a line comment gained a newline on every sqb format run, so --check never passed on such files.

Changes

  • Skip the idempotency re-print, and the token check with its extra tokenize, when the printed body equals the input; both can only reproduce it. Changed bodies still get both.
  • Syntax protection builds sentinel names only when needed, stops upper-casing every token, and looks up cast ranges by start token.
  • Restore protected fragments by indexing only sentinel-prefixed tokens.
  • Compare alignment tokens by a small key kind and raw text instead of formatted key strings.
  • The token check reuses the printer's authored token texts and reads printed texts only to confirm a keyword recase.
  • Skip the CTE macro pass when the layout has no lint sentinels.
  • Reuse header spans when trimming and comment relocation change nothing.
  • Fix: comments after the last token no longer end the printed body with a newline, so formatting settles in one run.

Verification

  • --check, --diff, --check --json and written trees are byte-identical to the previous build on 1k/10k projects (formatted, unformatted, fault-heavy) and all examples, except the one file the newline fix settles. Formatting every file twice changes nothing.
  • New tests: a re-print counter for unchanged and changed bodies; trailing-comment unit and format-twice integration cases.
  • cargo test -p sqlbuild-rules, format unit/integration/E2E tests and performance guards, make check-ci.
  • Cold --no-cache instructions: 10k formatted 237.0G → 148.2G (−37%), 10k unformatted 240.2G → 199.1G (−17%). Warm runs unchanged.

…ds one

Protecting SQL for the layout parse no longer formats a sentinel name
and upper-cases every token, and looks cast ranges up by start token
instead of scanning them for each token.
A body the formatter leaves unchanged would be re-printed from the same
text, tokens and layout oracle, so the re-print can only reproduce it.
Changed bodies are still re-printed and refused when not idempotent.
Restoring fragments after the layout parse indexed every token of the
generated SQL by its lower-case text. Only tokens that start with the
sentinel prefix can match a fragment, so only those are indexed.
Aligning authored tokens with the layout guide built a formatted key
string, with an upper-case copy for words, for every token. Lexemes now
carry a small key kind and compare their raw text directly, ignoring
ASCII case for words, which matches exactly what the strings compared.
The token check read the authored and printed token texts twice each,
once for the case-folded comparison and once for keyword recases. The
printer already holds the authored texts and fold flags, so the check
reuses them, compares them with the printed tokens without upper-case
copies of the authored side, and reads the printed texts only when a
keyword recase must be confirmed.
Laying out CTE macros tokenized the restored layout guide again for
every body. A CTE macro token starts with the lint sentinel prefix, so
the pass now returns at once when the text does not contain it.
Formatting headers scanned the file for header spans again after moving
leading comments. When trimming and relocation changed nothing, the
spans scanned from the original text are reused.
Printed text identical to the authored body tokenizes to the authored
tokens, so the token check compares the body with itself and can only
pass. The check and the printed-text tokenize now run only for bodies
the printer changed.
A line comment after the last token was printed with a newline after
it. Format runs trim a body's trailing whitespace and keep it apart, so
each run added that newline again and the file never settled. The
printer now ends the output at the comment, as the authored body did.
@kvlonge
kvlonge enabled auto-merge (squash) October 21, 2026 02:56
@kvlonge
kvlonge merged commit 8928936 into main Oct 21, 2026
44 checks passed
@kvlonge
kvlonge deleted the perf/formatter-repeated-work branch October 21, 2026 03:25
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