Skip to content

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

Open
datlechin wants to merge 2 commits into
mainfrom
perf/clickhouse-mssql-decode
Open

datlechin wants to merge 2 commits into
mainfrom
perf/clickhouse-mssql-decode

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

ClickHouse (TableProPluginKit, no public API change):

  • A line's fields are walked in place with memchr; no [ArraySlice] per line or per body. A row reserves its width.
  • A field with no backslash becomes its String in one copy, validated in place; only a field holding an escape is unescaped into a buffer first. A value that is not UTF-8 stays .bytes, as before.
  • classify (uncapped and parameterized reads) uses the same walk over the body's own bytes instead of copying the body into [UInt8] and building a line array first.
  • A value that opens with a byte order mark keeps it. String(bytes:encoding:) dropped a leading BOM, so such a String value read back three bytes short.

SQL Server (FreeTDS reader, iOS shares it):

  • Column types are read once per result set and reused for every cell, instead of dbcoltype per cell.
  • One conversion buffer serves a whole result set instead of a fresh 256-byte array per cell.
  • tinyint, smallint, int, bigint and bit are read from their own bytes and spelled as dbconvert spells them (MSSQLNativeCellText, TableProMSSQLCore). Every other type, and a length that is not the type's, still goes through dbconvert.
  • A buffered result is converted to grid cells row by row, letting go of each read row, so the hand-off no longer holds the result twice.

Left for later, in a comment on #4550: dates and times through dbanydatecrack (needs the column type corpus on a live server to prove the spelling for every scale), and moving uncapped ClickHouse reads onto the streaming shape.

Measure

ClickHouse decoder, local benchmark against the public decoder API (release build, best of 3, 64 KB chunks for the stream):

Body Stream before Stream after Buffered before Buffered after
1,000,000 rows x 1 UInt64 (7.5 MB) 1,194 ms 59 ms 1,212 ms 49 ms
20,000 rows x 200 mixed columns (48.6 MB) 3,132 ms 190 ms 3,703 ms 164 ms

SQL Server was not measured: it needs a live server, and check-column-type-corpus.sh mssql was not run in this pass. Integer and bit spelling is covered by unit tests against FreeTDS's own conversion rules.

Test plan

  • ClickHouseTabSeparatedRowDecoderTests: a leading BOM is kept (fails on main), streaming, byte-by-byte and buffered reads of one body give the same rows; the existing decoder, chunking and classifier suites pass (82 tests).
  • MSSQLNativeCellTextTests: every integer type across its range, bit, an unaligned read, and the types and lengths that fall back to dbconvert; the TableProMSSQLCore suites pass (409 tests).
  • verify.sh plugins and lint. The ClickHouse driver suites and the iOS build run on CI.

Closes #4550

…ld copies

ClickHouse: walk lines and fields in place with memchr, make each unescaped
field's String in one copy, reserve rows, and use the same walk in classify
without copying the body. A leading byte order mark now stays in the value.

SQL Server: read the column types once per result, share one dbconvert buffer,
read integers and bit from their bytes, and convert a buffered result row by row
so it is not held twice.

This branch has not been deployed

No deployments
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.

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

1 participant