Repository navigation
Done
Labels
Description
Problem
- Import. After the SQL file parser has split a statement, the import sink lexes it again to decide batching and track transactions. A SQL Server dump with no
GOline is lexed up to four times in one import: the count's batch-separator scan, the count, the import's scan, and the import. The parser itself walks the file one UTF-16 unit at a time throughNSString. All of this runs off the main thread, so it costs throughput, not hitches. Estimates, not measured: about 2 extra scans and 2 copies per batched statement in the sink, and 5-15 s of parser CPU per GB. - Export. A SQL dump reads each table's
CREATE TABLEwith its own catalog queries, one table after another on one connection. On PostgreSQL that is at least 3 round trips per table, so a 10k-table schema at +50 ms RTT spends about 25 minutes in the structure phase (arithmetic, not measured).
Cause
Import:
ImportDataSinkAdapter.batchesscans each statement again withSQLStatementScanner.executableStatements, searches the whole text forset_configcase-insensitively, and classifies it withCatalogChangeClassifier, which copies the text (apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift:264-275). After the statement runs,noteNameHazards(:241-251) andnoteTransactionEnd(:477-500) classify it again. The parser saw every one of those tokens a moment earlier and kept only the text.- With SQL Server grammar,
settleReadingstarts a secondParseSessionthat lexes until the firstGOline (apple/Packages/TableProCore/Sources/TableProSQLFile/SQLFileParser.swift:830-859). With noGO, it lexes the whole file. The count and the import each run it. readChunkdecodes each chunk to aStringand appends it to anNSMutableString(SQLFileParser.swift:884-918).processBufferreadsnsBuffer.character(at:)per UTF-16 unit (:921-930), andappendCharappends one unit at a time withCFStringAppendCharacters(:111-115). A finished statement is trimmed throughNSString(:267), so the driver transcodes it back to UTF-8. Quoted literals are copied by range, which keeps the cost below one call per byte.- The core already lexes bytes and returns classified units:
ScriptUnitcarries text, line, repeat count and kind (core/crates/core/src/sql_syntax/script/script_reader/mod.rs:21-28).
Export:
writeCreatePhaseawaitsdataSource.fetchTableDDLper table (apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift:638-670).- PostgreSQL's
fetchTableDDLruns its columns, constraints and shape queries throughasync let(apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLCatalogExport.swift:643-655), but they share one libpq connection, so they still take 3 round trips. A partitioned table adds a partition tree read (:682). - The dump already takes schema-wide reads for columns, foreign keys, indexes, comments, sequences and types, but not for table DDL (
apple/Plugins/TableProPluginKit/PluginExportBulkReads.swift:5-19).
Change
- One lex per file. The parser emits a classified unit per statement: text, line, leading keyword, whether it ends a transaction, whether it changes names, whether it holds a backslash, and its statement count. The sink decides batching and transaction tracking from that unit, with no rescan, no
set_configtext search and no classifier copy. - One batch-separator decision per file. Make it once and hand it to the import. With perf(export): stream gzip while writing a dump and while reading one back #4553's count pass gone, a SQL Server file with no
GOis lexed twice, not four times. - Lex bytes. Replace the
NSMutableStringbuffer with a UTF-8 byte scanner over a reusable buffer. Build each statement'sStringonce withString(decoding:as:), and passCOPYpieces as slices of the input. Statement text stays UTF-8 from file to driver. Moving file import onto the core'sScriptReaderinstead is outside ADR 0010's slice list (slice 4 covers the editor's runs), so that path needs the ADR to name it first. - Table DDL per schema. Add
.tableDDLtoPluginExportBulkReads. PostgreSQL readspg_class,pg_attribute,pg_constraintandpg_partitioned_tablefor the whole schema in a few statements and renders everyCREATE TABLEfrom that snapshot. The bulk read must give the same text as the per-table read, as the type's own contract says. Drivers without it keep the per-table read.
Measure
- Microbenchmark: parse a 1 GB generated mysqldump and a 1 GB pg_dump with
COPYblocks through the parser. Report MB/s and peak RSS before and after. - Time Profiler of a 1 GB mysqldump import into local MySQL: self time under
ImportDataSinkAdapter.batches,SQLStatementScannerandCatalogChangeClassifiergoes to near zero. - A 500 MB T-SQL file with no
GO: count full lexes with a signpost perParseSession. Before: 4. After (with perf(export): stream gzip while writing a dump and while reading one back #4553): 2. - Structure-only dump of a 10k-table PostgreSQL schema, locally and at +50 ms RTT: catalog statements from
pg_stat_statementsdrop from about 3 per table to a few per schema, and the structure phase time drops with them. A parity test compares the bulk and per-table DDL for every table in the fixture.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
The PR for this issue ships the export half: each plain table's
CREATE TABLEread once per schema (PluginExportBulkReads.tableDDL), 2 statements per PostgreSQL schema in place of 3 per table, with the same text as the per-table read.What is left:
PluginImportElement, so it is an ABI addition of its own.GOis now lexed twice (the pre-scan and the import), the target this item set.NSMutableStringbuffer inSQLFileParser. A rewrite of the lexer's core; it should land with a benchmark of MB/s and peak RSS against the current parser on a generated 1 GB dump.