Skip to content

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

Open
datlechin wants to merge 2 commits into
mainfrom
perf/mongodb-bson-decode
Open

datlechin wants to merge 2 commits into
mainfrom
perf/mongodb-bson-decode

Conversation

@datlechin

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.
  • 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.
  • verify.sh plugins, the iOS test build, lint.

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.

This branch has not been deployed

No deployments
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