Skip to content

perf(memory): build each result row at its exact size in every driver #4520

Done

Description

@datlechin

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 with oids.enumerated().map (apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection+ResultReading.swift:91-95). It serves the single-row read loops at apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift:2172, :2314 and :2398 (singleRowReadSync, which table pages and Fetch All use).
  • With the app's macOS 13 deployment target, EnumeratedSequence is only a Sequence; its Collection conformance needs a newer OS. Sequence.map cannot reserve the final count, so the buffer grows by doubling. A probe built with -target arm64-apple-macosx13.0 gives a 20-element row a capacity of 36 and a malloc size of 896 B; the same code built for macOS 27, or indices.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().map for every document (apple/Plugins/MongoDBDriverPlugin/BsonDocumentFlattener.swift:92).
    • ClickHouse TabSeparated streams: fields(line).enumerated().map for every row (apple/Plugins/TableProPluginKit/ClickHouseTabSeparatedRowDecoder.swift:84).
    • Less hot: demoteBinaryColumns rebuilds rows the same way when a column is binary (apple/Plugins/TableProPluginKit/ClickHouseResponseClassifier.swift:149).
  • 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 indices or a 0..<count range, 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 capacity equals its count. 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_1m run 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

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
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