Skip to content

perf(core): decode results straight into typed columns the store keeps #4522

Backlog

Description

@datlechin

Problem

#4521 moves every Mac result into the core's ResultStore. As the store and drivers stand, that would not deliver the memory or CPU it should:

  • Each value is copied three to four times on the way in.
  • Each cell carries 17 B of overhead, and integers and timestamps are kept twice.
  • Every PostgreSQL editor result is untyped text, and it sorts wrong.
  • DuckDB's columns are turned into rows and back.
  • SQLite copies each value through sqlite3 before the driver copies it again.
  • The byte limit measures a shape the store never keeps.
  • Table pages are held whole, with no byte bound.

None of this is measured yet: the core has no benches today (no benches/ directory and no [[bench]] target in any crate).

Cause

  • Row-major events. The driver contract is ResultEvent::Rows(Vec<Vec<Value>>) (core/crates/core/src/result_event.rs:51): a Vec<Value> per row and, for text, JSON and bytes, one more heap buffer per cell. RowBatch and ByteLimitSink each walk every row to sum its bytes (core/crates/core/src/row_batch.rs:20, core/crates/core/src/byte_limited_sink.rs:38).
  • Copies on append. ResultStore::append (core/crates/ffi/src/results/store.rs:86-125):
    • stages a fresh ColumnCells per column for every batch, with no reserve;
    • formats each cell into it: write! for integers, and a value_text String for floats, UUIDs, dates, timestamps and decimals (core/crates/ffi/src/results/column_cells.rs:40-77);
    • then copies bytes, kinds, raw values and ends again into the chunk under the write lock (column_cells.rs:103-118).
    • ADR 0020 records it: "the FFI's store holds a batch about three times while it appends it" (core/docs/decisions/0020-a-run-keeps-a-byte-limit-of-rows.md:8).
  • Per-cell overhead. ColumnCells keeps kinds: Vec<u8>, raw: Vec<i64> and ends: Vec<usize> for every cell (column_cells.rs:22-28). An integer keeps its decimal text and raw. A timestamp keeps its ISO text and raw. For text, raw repeats the length that ends already gives. The kind byte repeats even when the column's type is fixed.
  • PostgreSQL as text. The editor reads through simple_query_raw (core/crates/drivers/postgres/src/editor/run.rs:412):
    • Every column is text_column, ColumnType::untyped_text (run.rs:461, core/crates/drivers/postgres/src/catalog/mod.rs:46-48), and every cell is Value::Text(text.to_owned()) (run.rs:470).
    • Such a column sorts_as_text (core/crates/core/src/value/order.rs:39-43). natural_text_order compares digit runs as unsigned (order.rs:55-79), so -5 sorts before -10 and 1.5 before 1.25.
  • DuckDB. The driver reads Arrow chunks, then builds a Vec<Value> per row (core/crates/drivers/duckdb/src/result_stream.rs:76-90). Text is copied per cell, and JSON twice (core/crates/drivers/duckdb/src/decode/mod.rs:74-77). The store then turns the rows back into columns.
  • SQLite.
    • sqlx-sqlite 0.9.1's SqliteRow::current calls SqliteValue::dup for every column and sends each row over a channel on its own.
    • decode_cell then allocates the type name for every non-null cell (core/crates/drivers/sqlite/src/decode.rs:27) and copies text again (decode.rs:41).
  • The byte limit. Value::retained_bytes is size_of::<Value>() plus heap bytes (core/crates/core/src/byte_limit.rs:29-31), so the 256 MB default (byte_limit.rs:11) counts 32-byte slots the store never keeps. A 1M x 10 run of short values measures about 1.5 times what the store holds.
  • Table pages.
    • read_into awaits the whole page, then delivers it in one event into a RunSink with no ByteLimitSink (core/crates/ffi/src/session.rs:866-900, :895).
    • The PostgreSQL page collects every row first (core/crates/drivers/postgres/src/workspace_call.rs:195).
    • ADR 0020 exempts pages (0020-a-run-keeps-a-byte-limit-of-rows.md:27), so a page of large cells has no memory bound and is held about three times during the append.
  • Sort. ResultStore::row_order builds a Value for every row while it holds the read lock (store.rs:209). A sort therefore stalls a still-streaming append, and it cannot be cancelled.

Change

Each item lands with a bench that shows its effect.

  • Columnar batches. Add a batch builder to tablepro-core with typed appends (null, i64, f64, bool, temporal micros, text and bytes from a borrowed slice, and Value as the fallback for rare kinds). Drivers decode from the wire buffer straight into it. ResultEvent::Rows carries the finished batch, whose byte count the builder knows. The store keeps the batch as it is instead of copying it again. Value stays for keys, edits and parameters.
  • Typed storage.
    • Fixed-width kinds (int, uint, float, bool, date, time, timestamp, timestamptz with its offset) go in a typed lane plus a validity bitmap.
    • Variable-width kinds keep u32 offsets per chunk, with a chunk also cut by bytes.
    • A kind byte per cell exists only for a column with dynamic_storage.
    • Decimal, wide int and interval stay exact text.
    • Fixed-width cells are formatted when a window is packed (Cursor::pack_cell, core/crates/ffi/src/results/cursor.rs:185), byte for byte as value_text writes them today, so export and copy do not change. The window ABI stays the same, so the Windows app needs no change.
  • PostgreSQL typed. Keep DECLARE on the simple protocol, and send the FETCH through the extended protocol as an unnamed statement, which PgBouncer allows. That way each column has its type OID and binary values decode through the existing codec (core/crates/drivers/postgres/src/codec/). Exact text stays only for types without a native decoder. Numbers then sort as numbers.
  • DuckDB. Append the Arrow arrays of each chunk as columns. Convert only decimal, interval and nested types through value_text.
  • SQLite. Step the statement on one blocking thread per connection with the sqlite3 handle sqlx already owns, read sqlite3_column_* straight into the builder, and send whole batches over a bounded channel. Keep one SQLite library in the process (ADR 0010, "One SQLite in the app"). Drop the per-cell type-name to_owned in the same change.
  • Byte limit. Charge the bytes the store keeps, read from the batch. Amend ADR 0020 to define the limit that way. Under D3, revisit in the same amendment the 10,000 default and the Windows clamp of the user's row limit to 1,000,000 (windows/src/TablePro.App.Model/AppSettings.cs:207).
  • Table pages. Stream a page into its sink batch by batch, under the same byte bound. Keep the limit + 1 truncation and the keyset edge keys, which need only the first and last rows.
  • Sort. Order over the store's own cells: typed keys from the lanes, byte-slice heads for text. Do not hold the store's lock for the whole sort. Make ResultSet::sorted async with a cancel token.
  • Benches. Add criterion benches under core/crates/ffi/benches for RunSink and ResultStore at 1M x 20 (mixed int, text, timestamp, numeric) and 1M x 200.

Measure

  • Bench, before and after each item: ns per cell, allocations per batch (dhat), and the store's retained bytes per cell. Today an int cell is about 23 B and a timestamp about 43 B; typed storage should keep 8 B plus a validity bit.
  • 1M rows x 10 columns through QueryEditor::run with the row limit at 1,000,000: the arithmetic says today's run ends with ByteLimitReached near 640k rows (confirm it first); after, all rows are kept and the store's counter matches the phys_footprint delta within a few percent.
  • PostgreSQL 18, 1M rows of int and timestamp columns: ResultColumnInfo.value_kind is Integer and Timestamp, and a column of negatives and decimals sorts numerically.
  • DuckDB SELECT * FROM range(10_000_000) joined with a text column, and a 1M x 10 SQLite file: wall time and allocations.
  • A table page of 1,000 rows of 1 MB text: peak memory during the append.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    rust
    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-macosrust

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions