Skip to content

perf(drivers): decode ClickHouse and SQL Server cells without per-field copies #4550

Done

Description

@datlechin

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], and String(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 dbconvert to text and a Foundation string decode, and date and time values are then parsed again to reformat them.

Cause

ClickHouse:

  • ClickHouseTabSeparatedRowDecoder.ingest calls ClickHouseTabSeparatedBytes.fields(line) and maps it with enumerated().map, which cannot reserve the row's count (apple/Plugins/TableProPluginKit/ClickHouseTabSeparatedRowDecoder.swift:84-89). cellValue decodes with String(bytes:encoding:) (:92-97).
  • fields and lines build [ArraySlice<UInt8>] (apple/Plugins/TableProPluginKit/ClickHouseTabSeparatedBytes.swift:13-19, :97-111). value returns Array(field) for literal syntax and unescape returns Array(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:

  • columnValueAsString allocates [BYTE](repeating: 0, count: 256) per cell, calls dbconvert(..., SYBCHAR, ...) and decodes with String(bytes:encoding:); date and time cells then go through MSSQLDatetimeFormatter.reformat (apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift:771-805). reformat trims, builds a Character array and may run a Scanner (apple/Packages/TableProCore/Sources/TableProMSSQLCore/MSSQLDatetimeFormatter.swift:6-52).
  • readRow calls dbcoltype again 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 memchr over 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 String from the slice in one copy; keep .bytes for 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, where X-ClickHouse-Summary is needed.

SQL Server:

  • Read the column types once per result and reuse them in readRow.
  • Read native values from dbdata by type: integers with loadUnaligned and String(value), bit as 0 or 1. For date and time types, call dbanydatecrack into DBDATEREC2 and write the ISO digits directly, so there is no text to parse. Keep dbconvert for 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 PluginCellValue rows in the reader, or convert per streamed batch, so a buffered result is not held twice.
  • apple/scripts/probes/check-column-type-corpus.sh must 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 1000000 from a table of int, bigint, float, datetime2 and nvarchar columns: self time in readRow and total fetch time, before and after.
  • The column type corpus check for clickhouse and mssql.

Part of the performance plan.

Activity

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

    @datlechin
    MemberAuthor

    What the first PR for this issue ships, and what is left:

    • Shipped, ClickHouse: lines and fields are walked in place with memchr, a field without an escape becomes its String in one copy, rows reserve their width, and classify uses the same walk without copying the body into [UInt8].
    • Shipped, SQL Server: column types read once per result, one dbconvert buffer per result instead of one per cell, integers and bit read from their bytes, and a buffered result converted row by row so it is not held twice.
    • Left, SQL Server: date and time values through dbanydatecrack with the ISO digits written directly. The spelling has to be proven equal with check-column-type-corpus.sh mssql on 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.
    • Left, ClickHouse: moving uncapped row-returning reads onto the streaming shape. That changes which transport path a statement takes in ClickHousePluginDriver+Http, which needs its own change and its own check against a server.
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