Repository navigation
Description
While chasing a slow SELECT … LIMIT 5 over an R2RML graph source, for an embedder reading Iceberg tables in direct mode on S3, I had Claude build a synthetic repro and bisect the read path: LocalStack as S3, tables written by pyiceberg, 20 ms of latency injected on the S3 side, the server pinned to 16 vCPUs on a Linux box. The slowdown we were actually after turned out to live outside db, but the repro turned up three inefficiencies in db's own Iceberg read path that seem worth fixing regardless, all on current main (467f0da4d). The findings are Claude's; I've traced each one against the code, and every number here is from the synthetic tables (shapes at the bottom), measured on main and on the fix branches on the same box.
1. Each Parquet page header re-fetches an 8 MiB window
RangeBackedChunkReader (fluree-db-iceberg/src/io/chunk_reader.rs) reads every data file over 64 MiB. Its get_read(start) fetches min(8 MiB, file_size - start) on every call (chunk_reader.rs:287-298), and the range cache only serves a request that fits inside one cached window (CachedRange::contains). Parquet's page reader (SerializedPageReader::get_next_page, parquet 58) calls get_read once per page, only to decode the page header; the body then comes through get_bytes. So once a page starts more than about 256 KiB past the start of the last window, its header no longer fits, and it fetches a fresh 8 MiB. A column chunk of 1 MiB pages moves about 8 MiB per page from storage: roughly 8x the column's bytes, one GET per page.
On an 8-file table of about 46M rows (about 136 MB per file), SELECT ?x ?y WHERE { ?r ex:a ?x ; ex:b ?y } LIMIT 5 (two int64 columns projected), cold / warm:
| wall | S3 GETs | MB from S3 | server CPU-s | |
|---|---|---|---|---|
main |
21.2 / 20.8 s | 1,310 / 1,295 | 10,437 / 10,478 | 16.9 / 15.0 |
get_read served from a cached window |
1.7 / 1.5 s | 118 / 106 | 597 / 648 | 2.7 / 2.3 |
A full-table SUM over one int32 column of the same table moves 11 GB in 1,359 GETs on main, and 861 MB in 130 with the fix.
There's a memory side to it as well, fwiw: the reader keeps up to 64 cached windows, so at 8 MiB each one open file can hold up to 512 MiB of mostly overlapping bytes until its decode ends.
2. A LIMIT doesn't bound how much of each file the scan reads
scan_table_inner (fluree-db-api/src/graph_source/r2rml.rs) reads iceberg_scan_concurrency() files at once (min(cpus, files, 32)), and each file's read_task decodes the whole file into a Vec<ColumnBatch> before the stream yields a row. The operator's LIMIT budget (R2rmlScanOperator::row_budget) caps its materialize window but never reaches the scan, so a LIMIT 5 over a 576-file table starts 16 whole-file reads on a 16-vCPU host and waits for the first one to finish.
The same whole-file wait also slows down cancellation. A query notices that it was cancelled (timed out, or its client went away) at its next checkpoint, which sits between batches, so nothing drops the scan's stream until a whole file has been read. With the client hanging up 3 s into that full-table SUM on main, the server went on reading for the next 30 s (1,185 GETs, 9.8 GB), and abort-on-drop (item 3) doesn't change that here, because the stream isn't dropped until the first file is done.
I think the shape we want is to pass the remaining budget down to the scan as a hint, and have the scan read lazily under it: fewer files in flight, and a per-file read that yields batches as it decodes and stops when its consumer does. The Delta reader already does the latter through a bounded channel (fluree-db-delta/src/table.rs). It can only ever be a hint, though, since the scan can't know how many rows survive the null-subject check, a consumed FILTER or the view policy, so it has to keep reading past the budget when the operator keeps asking. #2315 is also reworking this same fan-out (read_files_in_order, plus an additive scan_table_ordered on R2rmlTableProvider), so we may want the hint to ride on that request rather than add a second additive method beside it. That trait shape is the part I'd want to settle before writing it.
3. A dropped scan's file reads keep running
Each file read is a tokio::spawn the stream awaits through buffer_unordered. Dropping the stream (the LIMIT has its rows, or a cancelled query finally unwinds) drops the JoinHandles, which detaches the tasks rather than stopping them, so every read in flight runs to the end of its file for no one. A file over 64 MiB decodes on spawn_blocking threads, which not even an aborted task stops, so those keep fetching and decoding too. In the 10 s after each LIMIT 5 response, cold / warm:
after the response, main |
with abort-on-drop | |
|---|---|---|
| 8 files of ~136 MB (the query above) | 21 / 18 GETs, 204 / 164 MB | 0 / 0 GETs, 29 / 23 MB (bodies already in flight) |
576 files of ~12 MB, SELECT ?s WHERE { ?s a ex:C } LIMIT 5, cold only |
0 and 108 MB in two runs (it depends on whether the reads beat the response) | 0.1 and 0.2 MB in two runs |
Why this matters more under some allocators
String columns decode into one owned String per cell. Under an allocator with a global lock (e.g. a static musl build that doesn't declare a replacement global allocator), a big scan becomes allocation-bound: on the same string-key scans the repro saw roughly 6-60x the CPU of mimalloc. db's own binaries use mimalloc and docs/operations/configuration.md already tells embedders to declare it, but the less of 1-3 a query does, the less this bites anyone who hasn't. Worth knowing, too: under musl's allocator, fixing 1 alone made the string-key join slower (34 s to 45 s), because the I/O was no longer hiding the allocator contention.
Repro
LocalStack 4.9 as S3, tables written by pyiceberg 0.10 with version-hint.text, one direct-mode graph source over the warehouse root, tc netem adding 20 ms on the S3 container's egress, the server (dev-fast, default features, so mimalloc) under taskset to 16 vCPUs on a c7i.8xlarge. Tables: one file of 10k rows (control); 576 files of about 12 MB (45k rows, a dozen string columns); 8 files of about 136 MB (5.8M rows, int64 keys); 8 files of about 166 MB (5.8M rows, string keys plus decimal and timestamp columns, 45 columns wide). Unordered LIMIT 5 answers vary from run to run on main too (which file finishes first), so for those I checked that every row returned, in every run on every build, is a row of its table; the counts and the SUM are identical across builds. Happy to share the generator and probe scripts.
Activity