Skip to content

perf(mongodb): read each document's BSON once with libbson's iterator - #4580

Merged
datlechin merged 12 commits into
mainfrom
perf/mongodb-bson-decode
Nov 27, 2026
Merged

datlechin merged 12 commits into
mainfrom
perf/mongodb-bson-decode

Conversation

@datlechin

@datlechin datlechin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

  • MongoDB documents are read once, with libbson's iterator (MongoBsonDocumentReader). One pass per document gives the Swift values the grid and the kind caches take, the canonical Extended JSON tree in stored field order, the _id locator and the stored size. Before, each document went BSON, canonical Extended JSON text, JSONSerialization, an unwrap pass, and then the text was parsed again for the locator, the nested cells and the export copy, plus bson_new_from_json only to read its size.
  • A cursor batch keeps the stored BSON bytes. JavaScript still gets canonical Extended JSON, made from the bytes only when a script reads the documents. Documents a script builds stay text and read as before.
  • The export copy is built from the tree the read already holds, with its size from the BSON length prefix. The streaming export and every dictionary read (bsonToDict, the sampling find) use the same reader.
  • The rare BSON types (regular expressions, code, symbols, timestamps, DBPointers, MinKey, MaxKey, undefined) are spelled by libbson for that one value, so their text cannot drift. Doubles use libbson's %.20g rule. A repeated field name, compared as bytes, leaves the document without a canonical tree, as the strict text read refused it.
  • From review: a value whose own text the strict reader refuses (a code scope that repeats a name) keeps its row and leaves its document, and every container above it, without a canonical tree, so a partial _id never becomes a locator. A field name that is not UTF-8 refuses the document, as libbson's Extended JSON did, so a repaired name never spells another document's _id. A repeated name is found through a set of its bytes, not a scan of the members before it.
  • apple/scripts/probes/check-mongodb-export-values.sh calls the tree-based MongoExportDocument.read and flatten(stored:) and compiles the files they now need.
  • Signposts (com.TablePro, MongoDBRead) around the shell evaluation, the cursor read, the decode and the grid build, so the shell's share of a browse page can be measured in Instruments.

Left for later, in a comment on #4548: building the export copy only when an export or copy asks (the host keeps it per row, so it needs a PluginKit requirement and the tab's row store), reusing the declared schema on a first page (the driver gets no signal for an explicit Refresh), the capped filtered count (needs the threshold passed through PluginKit), and browsing without the shell.

Measure

Local harness on this Mac (the plugin sources and the shipped libbson in a scratch SwiftPM package, release build, machine shared with other builds), the grid path for one page: read, locators, column and kind passes, flatten, absent cells, export copy. Persistent bytes are malloc in-use after the result lands.

Documents x fields Before After Held before Held after
1,000 x 10 302 ms 103 ms 3.2 MB 2.3 MB
1,000 x 50 2,556 ms 1,079 ms 13.7 MB 10.3 MB
100,000 x 10 61.9 s 17.3 s 309 MB 220 MB
100,000 x 50 262 s 72 s 1,415 MB 1,078 MB

Round trips and the filtered count are unchanged by this PR.

Test plan

  • MongoBsonDocumentReaderTests (TableProMobileTests): every BSON type, the doubles whose spelling is easy to get wrong, nested documents and arrays, a top-level $ name, a NUL inside a string, a repeated name and two spellings of one character. Each document read from its BSON gives the same tree, values, locator and size as reading its canonical text did. It and MongoDBReadMemoryTests pass locally in the harness (9 tests); on CI they run in the iOS job (no simulator runtime on this Mac).
  • MongoDBReadMemoryTests updated for the batch holding documents instead of text; same assertions.
  • New in MongoBsonDocumentReaderTests: a code scope that repeats a name keeps its row; an _id holding one gives no locator; a field name that is not UTF-8 refuses the document (raw BSON written by hand).
  • After the merge with main, where perf(memory): build each result row at its exact size in every driver #4563 rewrote the loop in BsonDocumentFlattener.flatten and the merge kept its indices loop with this PR's stored trees: MongoBsonDocumentReaderTests and MongoDBReadMemoryTests in a macOS SwiftPM harness over the plugin's sources, its shared sources and the shipped libbson: 12 executed, 12 passed, the three new cases among them.
  • verify.sh build: PASS. verify.sh plugins: PASS. iOS build-for-testing for the generic Simulator: TEST BUILD SUCCEEDED. The export probe: PASS. verify.sh lint, shellcheck on the probe: PASS.
  • The iOS suites have not run in a Simulator here: this Mac has none. CI's iOS job, which installs the runtime since ci(ios): install the simulator runtime when the runner image lacks it #4607, is queued on this push.
  • Codex: two review rounds and two security passes. Every finding is fixed above; the last security pass found the repeated-name scan, fixed with the set.

Closes #4548

A page of documents went BSON, canonical Extended JSON, JSONSerialization and an
unwrap pass, and the text was then parsed again for the row locator, the nested
cells and the export copy, plus bson_new_from_json only for the size. One
bson_iter pass now gives the values, the canonical tree, the locator and the
size; a cursor batch keeps the BSON bytes and makes text only for JavaScript.
Signposts mark the shell, the cursor read, the decode and the grid build.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:50
@datlechin
datlechin merged commit 5b45237 into main Nov 27, 2026
10 of 11 checks passed
@datlechin
datlechin deleted the perf/mongodb-bson-decode branch November 27, 2026 16:36
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(mongodb): decode BSON once and stop the extra reads on every page

1 participant