Skip to content

perf(import): lex imported SQL once and read a dump's table DDL per schema #4555

Done

Description

@datlechin

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 GO line 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 through NSString. 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 TABLE with 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.batches scans each statement again with SQLStatementScanner.executableStatements, searches the whole text for set_config case-insensitively, and classifies it with CatalogChangeClassifier, which copies the text (apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift:264-275). After the statement runs, noteNameHazards (:241-251) and noteTransactionEnd (: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, settleReading starts a second ParseSession that lexes until the first GO line (apple/Packages/TableProCore/Sources/TableProSQLFile/SQLFileParser.swift:830-859). With no GO, it lexes the whole file. The count and the import each run it.
  • readChunk decodes each chunk to a String and appends it to an NSMutableString (SQLFileParser.swift:884-918). processBuffer reads nsBuffer.character(at:) per UTF-16 unit (:921-930), and appendChar appends one unit at a time with CFStringAppendCharacters (:111-115). A finished statement is trimmed through NSString (: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: ScriptUnit carries text, line, repeat count and kind (core/crates/core/src/sql_syntax/script/script_reader/mod.rs:21-28).

Export:

  • writeCreatePhase awaits dataSource.fetchTableDDL per table (apple/Plugins/SQLExportPlugin/SQLExportPlugin.swift:638-670).
  • PostgreSQL's fetchTableDDL runs its columns, constraints and shape queries through async 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_config text 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 GO is lexed twice, not four times.
  • Lex bytes. Replace the NSMutableString buffer with a UTF-8 byte scanner over a reusable buffer. Build each statement's String once with String(decoding:as:), and pass COPY pieces as slices of the input. Statement text stays UTF-8 from file to driver. Moving file import onto the core's ScriptReader instead 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 .tableDDL to PluginExportBulkReads. PostgreSQL reads pg_class, pg_attribute, pg_constraint and pg_partitioned_table for the whole schema in a few statements and renders every CREATE TABLE from 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 COPY blocks 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, SQLStatementScanner and CatalogChangeClassifier goes to near zero.
  • A 500 MB T-SQL file with no GO: count full lexes with a signpost per ParseSession. 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_statements drop 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

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 25, 2026

    @datlechin
    MemberAuthor

    The PR for this issue ships the export half: each plain table's CREATE TABLE read 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:

    • One lex per file. The parser would emit a classified unit (leading keyword, ends a transaction, changes names, holds a backslash, statement count) and the sink would batch and track transactions from it. The unit has to cross PluginKit as part of PluginImportElement, so it is an ABI addition of its own.
    • One batch-separator decision per file. With perf(export): stream gzip while writing a dump and while reading one back #4553 the dialog's count pass is gone, so a SQL Server file with no GO is now lexed twice (the pre-scan and the import), the target this item set.
    • A UTF-8 byte scanner in place of the NSMutableString buffer in SQLFileParser. 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.
    • Partitioned tables and partitions in the schema-wide DDL read; they keep the per-table read for now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions