Skip to content

perf(export): faster SQL and XLSX exports, row batches bounded by bytes - #4434

Merged
datlechin merged 5 commits into
mainfrom
perf/export-data
Nov 23, 2026
Merged

datlechin merged 5 commits into
mainfrom
perf/export-data

Conversation

@datlechin

@datlechin datlechin commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

  • SQL export escapes string literals with one byte scan (export-data E1, finding 17a). A value with nothing to escape comes back without a copy. Every byte an escape acts on is ASCII, so the bytes found are the scalars the old loop changed. PluginKit's copy gets the same scan, and the SQL row is built in one string.
  • XLSX sheets are deflated (ZIP method 8, zlib level 1) as their rows arrive, and the small parts too (E2, 17b). Level 1 keeps any sheet under TablePro's own import limit of 250 to 1; level 6 can pass it.
  • XLSX row XML is written as byte runs into one buffer, with a byte path for plain decimals (E3).
  • Driver streams cut a batch at 5,000 rows or 1 MiB, whichever comes first (E4, 16b): PostgreSQL, MySQL and MariaDB, SQLite, libSQL, ClickHouse. The XLSX export writes each batch as it arrives. The Parquet export stages at most 2,000 rows or 8 MiB at a time.
  • ClickHouse exports stream through a bounded buffer and the spill SQLite uses, so a reader slower than the server costs temporary disk instead of memory (E5). The spill gets deliverSuspending for a producer that runs as a task, so a spent budget no longer blocks a thread of the cooperative pool.
  • XLSX text cells over 32,767 characters are cut on a scalar boundary, and the export warns with the count (E7, decision 20). Excel 16 for Mac repairs a workbook with a longer cell and cuts it there anyway; the same deflated workbook with cells up to 32,767 opens with no repair.
  • MongoDB (M1, 15): a drained script cursor drops its documents' text, number kinds of one shape share one dictionary, row locators are read once per document, and a result with no masking rule keeps each export document as read.

Measurements

Before is origin/main ab6f526, after is this branch, interleaved on the same Mac (M4, load 7 to 14). Harness rows: 1,000,000 rows shaped like bench_typical, from memory through the export plugin built with -O.

What Before After
SQL export, instructions 190.0 G 15.4 G
SQL export, CPU 7.9 to 11.6 s 0.71 s
SQL export, output 214,554,085 bytes same bytes (only the Generated: time differs)
XLSX export, instructions 141.2 G 46.0 G
XLSX export, CPU 6.6 s 2.3 s
XLSX export, file 575.5 MB 61.4 MB, every entry inflates to the bytes it held before
Escape, 30-byte clean value, PostgreSQL 28,379 instructions 893
Escape, 30-byte clean value, MySQL 11,241 instructions 582
PostgreSQL stream, 300 rows of 1 MiB, slow reader, peak over baseline (n=2) 310.8, 309.7 MB 37.8, 37.7 MB
SQLite stream, same table, peak over baseline (n=2) 304.6, 303.6 MB 34.0, 36.9 MB (282 MB went to the spool)

The stream rows come from scripts/probes/check-stream-backpressure.sh with a 1 MiB payload and a 200 ms wait per batch. With ORDER BY id the SQLite numbers are 379 and 335 MB, because SQLite's sorter holds the table before the first row comes out, whatever the stream does.

Tests changed on purpose

  • XLSXWriterStreamingTests: workbookBytesAreUnchanged hashes the stored workbook rebuilt from the deflated entries, so the old digest still pins every byte of XML. sheetXMLReachesItsFileAsRowsArrive checks the deflated part. oversizedWorkbookFailsEarly checks the sheet's XML size.
  • ExportStopAndStatusTests.xlsxStoppedDuringArchive writes 320 cells of random base64, which does not deflate and fits Excel's cell limit, so the stop still lands mid-copy.
  • XLSXWriterCellTypeTests, XLSXWriterSheetNameTests, ExportFormatRowScopeTests and FormatSettingsChangedMidRunTests read the workbook through ZipArchive instead of its raw bytes.
  • ClickHouseStreamFailureTests gains two cases with a held reader; the existing ones are unchanged.

Deliberately not fixed here

  • M2, export documents built only when exporting: it adds a PluginKit field and kit 47, and touches QueryExecutor.swift, which the table-open-save lane changes next. Its own PR, after this one.
  • M3, reading BSON once: gated on a profile with a plugin dSYM, which was not run.
  • E6, Parquet staging and COPY limits: gated on a staging sweep, which was not run. Only the 8 MiB staging batch from E4 lands here.
  • MongoDB and ClickHouse were not measured: no registry plugin harness or ClickHouse server on this Mac. Their behavior is pinned by tests.
  • The MySQL wide-row probe: the local MariaDB takes only its Unix socket.
  • SQLiteAgentBackend still calls the blocking spill deliver from the cooperative pool. Its callback is synchronous, so it needs the agent connection's callback made async first.
  • SQLite exports still spool the table, as perf(sqlite): end a copy's read at full speed and keep the rows it has not taken in a temporary file #4278 intends.

Test plan

  • verify.sh test on FormatPluginTests, SQLiteDriverTests, ClickHouseDriverTests, PostgreSQLDriverTests, MySQLDriverTests and the TableRows, masking and document export suites: 2,768 cases pass; FormatPluginTests again after the cell limit, 617 pass.
  • verify.sh package TableProCore for the quoting, PostgreSQL conformance, PluginKit parity, gzip and row stream suites: 67 and 19 pass.
  • New: the old scalar loop kept as an oracle for every escape mode, raw deflate round trips, every entry method 8 with its CRC, an 85 MB repetitive sheet read back through the import's 250 to 1 guard, a golden sheet XML, the plain decimal path against the general rule, row batcher cuts, 1 MiB SQLite rows one to a batch, task producers on a spent or failed spool, task producers leaving the pool free, a held ClickHouse export reader, the cell limit with an emoji on the boundary, and the masked and unmasked document export.
  • verify.sh plugins passes. The iOS app and its tests build for testing (arm64); the iOS tests run on CI only.
  • Opened the long-cell workbook and a control workbook in Excel 16 for Mac.

Docs: TableProApp/docs#273

@datlechin
datlechin enabled auto-merge (squash) November 23, 2026 08:14
@datlechin
datlechin merged commit 00f003e into main Nov 23, 2026
14 checks passed
@datlechin
datlechin deleted the perf/export-data branch November 23, 2026 12:17
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