Repository navigation
Done
Labels
Description
Problem
A 1M-row, 20-column SELECT * FROM trips_1m (Execute Without Limit) takes phys_footprint from 573 MB to 1,262 MB on PostgreSQL and from 257 MB to 856 MB on MySQL. The heap shows the row arrays (_ContiguousArrayStorage<PluginCellValue>) at 1,000,000 x 896 B = 896 MB on PostgreSQL against 1,000,000 x 512 B = 512 MB on MySQL, for the same columns. That is 384 MB per million rows spent on empty slots.
Cause
singleRow(of:oids:)builds each row withoids.enumerated().map(apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift:91-95). It serves the single-row read loops atapple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift:2172,:2314and:2398(singleRowReadSync, which table pages and Fetch All use).- With the app's macOS 13 deployment target,
EnumeratedSequenceis only aSequence; itsCollectionconformance needs a newer OS.Sequence.mapcannot reserve the final count, so the buffer grows by doubling. A probe built with-target arm64-apple-macosx13.0gives a 20-element row a capacity of 36 and a malloc size of 896 B; the same code built for macOS 27, orindices.map, gives capacity 20 and 512 B. That is why a scratch test on a current target sees no waste and the app does. - MySQL builds rows with
decodings.indices.map(apple/Plugins/MySQLDriverPlugin/MySQLColumnDecoding.swift:95-104) and SQLite with(0..<columnCount).map(apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift:703-705). Both are exact, which matches MySQL's 512 B. - The same pattern is in two other per-row builders:
- MongoDB:
columns.enumerated().mapfor every document (apple/Plugins/MongoDBDriverPlugin/BsonDocumentFlattener.swift:92). - ClickHouse TabSeparated streams:
fields(line).enumerated().mapfor every row (apple/Plugins/TableProPluginKit/ClickHouseTabSeparatedRowDecoder.swift:84). - Less hot:
demoteBinaryColumnsrebuilds rows the same way when a column is binary (apple/Plugins/TableProPluginKit/ClickHouseResponseClassifier.swift:149).
- MongoDB:
- The other PostgreSQL loops already reserve
numFields(LibPQPluginConnection.swift:1357,:1548,:1827;LibPQPluginConnection+ResultReading.swift:119-120).
Change
- Build every row of a result with its exact count: map over
indicesor a0..<countrange, or reserve the count and append. Fix the three builders above in the same change. - Add a test per engine plugin that decodes a 20-column row through the driver's real row builder and checks the row buffer's
capacityequals itscount. That test fails on main for PostgreSQL, MongoDB and ClickHouse, and keeps the next builder from regressing on the macOS 13 target. - This is the cheap, local part. The row-of-enums shape itself goes with perf(results): hold every Mac result in the core's columnar store #4521.
Measure
- Repeat the 1M x 20
trips_1mrun on PostgreSQL with Execute Without Limit. Heap: the row arrays drop from 896 B to 512 B each, 896 MB to 512 MB in total; phys_footprint with the result open drops by about 384 MB from 1,262 MB. - Run the same on a 1M-document MongoDB collection and a 1M-row ClickHouse query and record bytes per row array before and after.
Part of the performance plan.
Activity