Skip to content

perf(e6): pause GC per /convert-query, port upstream parser fast paths, bound log_records - #341

Merged
tkaunlaky-e6 merged 1 commit into
mainfrom
perf/gc-parser-fastpaths
Nov 23, 2026
Merged

tkaunlaky-e6 merged 1 commit into
mainfrom
perf/gc-parser-fastpaths

Conversation

@tkaunlaky-e6

Copy link
Copy Markdown

Why

Under the samsung-dashboard stress test, one 3.8 MB Tableau query (about 20k OR branches, 24k IN lists) took 27 s in the transpiler on the old path, and 57 s with SUBQUERY_TREE while other users ran queries in parallel. Profiling showed three problems:

  1. The parse itself. It's about 70% of the transpile time, spent in sqlglot's Python parser.
  2. The cyclic GC.
    • A sqlglot AST has parent pointers, so a finished tree can only be freed by the cyclic GC. Reference counting can't free it.
    • During a big parse, the GC repeatedly does full scans of the growing tree and of the previous request's dead tree, which is still in memory. That costs about 2 s per request and doubles each worker's memory.
    • Under parallel load it got worse: work per request doubled, and at 8 concurrent requests workers were killed for running out of memory.
  3. log_records grows forever. Each worker keeps every log line, including the full converted query: about 8 KB per normal query, 67 KB per Samsung query, and 3.8 MB for this one.

What

File Change
sqlglot/parser.py, sqlglot/expressions.py Ports five parser speedups from upstream tobymao/sqlglot. Details below.
converter_api.py Pauses the GC for the length of /convert-query and collects once at the end, in finally. Calls gc.freeze() once after startup so that collect stays cheap. The handler has no await, so requests never interleave while the GC is paused.
log_collector.py log_records is now a deque(maxlen=1000). /statistics and /guardstats are deprecated.
tests/test_subquery_tree.py New test: the GC is re-enabled after a request, including one that raises, and the log buffer is bounded.

The parser ports:

  • 18f15ca: inline _parse_tokens into the disjunction, conjunction, comparison and exponent parsers.
  • 489ba1d: column fast path, plus is_primitive on Literal, Identifier, Var and Boolean, which skips validation and parent wiring for them.
  • c95ae50: literal fast path in _parse_type.
  • ec7b3d8: _parse_select_or_expression tries the expression first.

The fast paths fall back to the normal path wherever a dialect override applies (Postgres _parse_field, the Databricks string merge and quoted qualifier, DuckDB #n, Teradata (FORMAT ...)).

Results

Docker, 7 CPUs, the 3.8 MB query, samsung-dashboard config (SUBQUERY_TREE, Rust tokenizer, STRIP flags). main and this branch ran back to back. Client times:

main this PR
Sequential 10.9 / 9.0 / 9.1 s 5.7 / 5.4 / 6.2 s
4 concurrent, worst 22.8 s 8.4 s
8 concurrent, worst / typical 30.2 s / 16.5–20.0 s 16.3 s / 8.6–9.5 s
Server work per request, 8 concurrent 15.9–19.2 s 7.2–8.2 s
Peak memory 3.8 GB 3.1 GB

Postgres parse alone on this query: 4.9 s → 3.4 s.

Verified

  • Outputs are byte-identical to main with the samsung-dashboard config: nov_4th workload 14,840 / 14,840, Samsung corpus 704 / 704.
  • make test and make test-rs: 1065 tests OK. make test-multidialect (Samsung / Condé Nast / OneTrust): OK.
  • The new test fails without the change.
  • ruff and ruff-format pass. The only mypy error is the missing generated sqlglot/_version.py, which is unrelated.

…s, bound log_records

- converter_api: disable the cyclic GC during /convert-query and collect once at the
  end (finally); gc.freeze() after startup. sqlglot ASTs are cyclic, so the GC kept
  rescanning the growing tree and the previous request's dead one.
- parser/expressions: port upstream fast paths (18f15ca inlined _parse_tokens,
  489ba1d column fast path + is_primitive, c95ae50 literal fast path,
  ec7b3d8 expression-first _parse_select_or_expression).
- log_collector: log_records is a deque(maxlen=1000).

3.8 MB Tableau query, Docker 7 CPU: 9-11 s -> 5.4-6.2 s sequential, 4 concurrent worst
22.8 s -> 8.4 s. Outputs identical on nov_4th (14,840) and Samsung (704).
@tkaunlaky-e6
tkaunlaky-e6 merged commit 0dd4c5f into main Nov 23, 2026
6 checks 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.

2 participants