Repository navigation
Done
Labels
Description
Problem
Both drivers allocate and copy for every cell before the value reaches the grid. The cost is linear in cells, so it shows on numeric- and date-heavy reads of 100k rows and up. Neither has been profiled on a real server yet; the per-cell overhead is read from the code.
- ClickHouse: every streamed line becomes an array of slices, every field without an escape is copied into a new
[UInt8], andString(bytes:encoding:)copies it again. This decoder serves every capped grid read and every export. - SQL Server: every non-string cell (int, bigint, float, decimal, money, bit, date and time) allocates a fresh 256-byte buffer, goes through
dbconvertto text and a Foundation string decode, and date and time values are then parsed again to reformat them.
Cause
ClickHouse:
ClickHouseTabSeparatedRowDecoder.ingestcallsClickHouseTabSeparatedBytes.fields(line)and maps it withenumerated().map, which cannot reserve the row's count (apple/Plugins/TableProPluginKit/ClickHouseTabSeparatedRowDecoder.swift:84-89).cellValuedecodes withString(bytes:encoding:)(:92-97).fieldsandlinesbuild[ArraySlice<UInt8>](apple/Plugins/TableProPluginKit/ClickHouseTabSeparatedBytes.swift:13-19,:97-111).valuereturnsArray(field)for literal syntax andunescapereturnsArray(field)when there is no backslash (:31-33,:45-46).- The buffered path (
classify) also copies the whole body into[UInt8]and builds the line array first (apple/Plugins/TableProPluginKit/ClickHouseResponseClassifier.swift:29-48). It runs for uncapped and parameterized reads. - Closed perf(drivers): widen the SSH window and cut SQL driver round trips #4331 (P33) already classifies the column type once per column; the copies above remain.
SQL Server:
columnValueAsStringallocates[BYTE](repeating: 0, count: 256)per cell, callsdbconvert(..., SYBCHAR, ...)and decodes withString(bytes:encoding:); date and time cells then go throughMSSQLDatetimeFormatter.reformat(apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift:771-805).reformattrims, builds aCharacterarray and may run aScanner(apple/Packages/TableProCore/Sources/TableProMSSQLCore/MSSQLDatetimeFormatter.swift:6-52).readRowcallsdbcoltypeagain for every cell (FreeTDSConnection.swift:1105-1131).- An uncapped buffered result is then mapped row by row into a second
[[PluginCellValue]]while the first is alive (apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift:28-38), about 24 bytes per cell plus 32 per row extra at the hand-off.
Change
ClickHouse:
- Walk a line's fields with
memchrover the buffer and decode each field in place, without building[ArraySlice]per line. Build the row with reserved capacity. - Allocate only for a field that holds a backslash. Otherwise validate the UTF-8 in place and make the
Stringfrom the slice in one copy; keep.bytesfor a value that is not valid UTF-8, as today. - Apply the same decoder to
classify, and move uncapped row-returning reads onto the streaming shape so the body is not held twice. Keep the buffered shape only for statements without a result set, whereX-ClickHouse-Summaryis needed.
SQL Server:
- Read the column types once per result and reuse them in
readRow. - Read native values from
dbdataby type: integers withloadUnalignedandString(value), bit as 0 or 1. For date and time types, calldbanydatecrackintoDBDATEREC2and write the ISO digits directly, so there is no text to parse. Keepdbconvertfor decimal, numeric and money, and for float and real until their Swift spelling is proven equal, with one buffer per reader instead of one per cell. - Build
PluginCellValuerows in the reader, or convert per streamed batch, so a buffered result is not held twice. apple/scripts/probes/check-column-type-corpus.shmust show no spelling change for either engine.
Measure
- ClickHouse:
SELECT * FROM numbers(1e7)and a 200-column table through the grid and an export: Time Profiler self time in the decoder, wall time and peak memory, before and after. - SQL Server:
SELECT TOP 1000000from a table of int, bigint, float, datetime2 and nvarchar columns: self time inreadRowand total fetch time, before and after. - The column type corpus check for
clickhouseandmssql.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
What the first PR for this issue ships, and what is left:
classifyuses the same walk without copying the body into [UInt8].dbanydatecrackwith the ISO digits written directly. The spelling has to be proven equal withcheck-column-type-corpus.sh mssqlon a live server for every scale of time, datetime2 and datetimeoffset, which this pass did not run. Float and real stay on dbconvert for the same reason.ClickHousePluginDriver+Http, which needs its own change and its own check against a server.