Skip to content

perf: cut parser, cleanup and e6 generator overhead in subquery-tree conversion - #329

Merged
tkaunlaky-e6 merged 1 commit into
codex/experiment-dialect-regionsfrom
perf/dialect-regions-hot-paths
Oct 28, 2026
Merged

tkaunlaky-e6 merged 1 commit into
codex/experiment-dialect-regionsfrom
perf/dialect-regions-hot-paths

Conversation

@tkaunlaky-e6

Copy link
Copy Markdown

Speed-only changes on top of codex/experiment-dialect-regions. The flow is unchanged: same region discovery, same Postgres-first / Databricks-fallback, same output.

Changes

Parser (sqlglot/parser.py, sqlglot/expressions.py)

  • Inline hot helpers: _advance, Expression.__init__ parent setting, and Parser.expression comments and validation.
  • Inline _match_set in the 8 operator-precedence loops; faster _match_text_seq and update_positions.
  • Fast exits when no matching token can follow: _parse_types, _parse_select_query (guarded against dialect overrides), and _parse_range / _parse_bitwise.

Cleanup + transforms (apis/utils/helpers.py)

  • normalize_unicode_spaces uses precompiled regexes, returning early when there is nothing to replace. It was a character-by-character loop costing about 4% of all CPU.
  • apply_e6_ast_transforms walks the tree with an explicit stack instead of tree.transform.

e6 generator (sqlglot/dialects/e6.py)

  • quote_identifier used to rebuild a 690-word keyword set on every identifier; it is now a module-level frozenset. This is most of the e6.py diff: the set moves from inside the method to module level.
  • indent fast path when no line toggles the quote state, which is the common case.

Verification

  • Output: identical (sha256 of every converted query or error) on all 1,628 Samsung + Conde Nast queries. Checked with both tokenizers, pretty-print on and off.
  • Test suite: unchanged from the base branch. Python tokenizer: 937 passed / 4 skipped. Rust tokenizer (sqlglotrs 0.6.3): 940 passed / 1 skipped.

Benchmark

  • Setup: two images built from the production Dockerfile, base vs. this branch. Both run side by side, each container pinned to its own CPU with 2 GB.
  • Harness: benchmarks/four_way.py calling the real convert_query, with all 1,628 queries, 3 passes, and each query's median.
Tokenizer Pretty Output differs Mean CPU / query Median p95 Queries >= 100k chars (107)
Rust on 0 / 1628 77.1 → 54.8 ms (-28.9%) -33.0% -29.0% -30.2%
Rust off 0 / 1628 71.4 → 51.4 ms (-28.1%) -32.9% -27.7% -29.1%
Python on 0 / 1628 94.5 → 73.7 ms (-22.0%) -21.8% -23.0% -22.9%
Python off 0 / 1628 91.4 → 70.0 ms (-23.4%) -26.9% -22.3% -23.4%

Peak memory is unchanged (295 → 291 MiB Rust, 274 → 273 MiB Python). The 18 errors are the same queries on both sides.

Notes

  • Maintenance: the fast exits in _parse_select_query, _parse_range and _parse_bitwise rely on lists of the tokens each function reacts to. If one of those functions learns a new token, it must be added to the matching list.
  • Not included: pausing GC per request (a further 7-10%, left out on purpose) and the token-reuse gate fix for 0x / 0b literals and digit-leading names (separate change).

…conversion

- parser: inline hot helpers (_advance, Expression.__init__, Parser.expression),
  fast exits in _parse_types, _parse_select_query, _parse_range and _parse_bitwise
  when no matching token follows, inlined _match_set in operator-precedence loops,
  faster _match_text_seq and update_positions
- helpers: normalize_unicode_spaces as precompiled regexes (fast path when nothing
  to replace); apply_e6_ast_transforms as an explicit stack walk
- e6: build the quote_identifier keyword set once at module level; indent fast path
  when no line toggles quote state

Output is identical on all 1,628 Samsung + Conde Nast queries (both tokenizers,
pretty on and off). Test suite unchanged: 937 passed / 4 skipped (Python
tokenizer), 940 passed / 1 skipped (Rust tokenizer).
@tkaunlaky-e6
tkaunlaky-e6 merged commit 6431a11 into codex/experiment-dialect-regions Oct 28, 2026
1 check passed
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