Repository navigation
Done
Labels
Description
Problem
Opening a MongoDB collection converts each document many times, keeps a second copy of every document for an export that may never happen, and adds a sequential round trip on every first page:
- Each document goes BSON, canonical Extended JSON text,
JSONSerialization, an unwrap pass into a second[String: Any]tree, then several walks for columns and kinds, the flatten, and another parse for the export copy. Estimated 100-200 µs per 10-field document against 5-20 µs for onebson_iterpass, not measured. - Every first page and refresh sends
listCollectionsafter the find to re-read the validator: one more RTT, sequential with the find (about 50 ms at +50 ms, more through a tunnel). - Every browse page, including app-built ones, runs through the JavaScriptCore shell. Its share of the open is not measured.
- A filtered tab counts with an uncapped
countDocuments(5 s budget) on the same serial libmongoc queue that serves the next page.
Cause
iterateCursorJsonturns each document into canonical Extended JSON and keeps[String](apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+ScriptHelpers.swift:329-354,bsonToJsonatapple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift:1003-1009).readDocumentsparses each text withJSONSerializationand builds a second tree withunwrapExtendedJson(MongoDBConnection+ScriptHelpers.swift:23-34,MongoDBConnection.swift:1013).buildPluginResultwalks the documents again for columns, kinds, filter kinds, identity kinds, binary subtypes, field kinds and number kinds, then flattens (apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift:1056-1107).- It always sets
exportDocuments(MongoDBPluginDriver.swift:1105).MongoExportDocument.readparses each text again and runsbson_new_from_jsonon the whole text only to read its size (apple/Plugins/MongoDBDriverPlugin/MongoExportDocument.swift:11,:29). The documents travel with the rows (apple/TablePro/Core/Plugins/PluginDriverAdapter.swift:1302) and stay in the tab (apple/TablePro/Models/Query/TableRows.swift:208,:394-408). runScriptevaluates every statement inMongoScriptRuntimeand then awaitsdeclaredColumns(MongoDBPluginDriver.swift:227-271).declaredColumnsreuses the cached schema only whenskip > 0; a first page always callsdeclaredSchema, which sendslistCollections(:275-291,:421-432).- An unfiltered open already uses
estimatedDocumentCount(MongoDBPluginDriver.swift:503-512, chosen byrowCountPlaninapple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift:915-916). A filtered count callsmongoc_collection_count_documentswith onlymaxTimeMSand an_id_hint, nolimit(apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift:241-262). Because Mongo setsfiltersTypedByReadRows, the count runs on the page's own driver (apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift:249-258).
Overlaps #4444: M2 (build export documents only when exporting) and M3 (read BSON once instead of five parses a document) already cover the decode and the export copy. This issue keeps them as the first two steps and adds the validator read, the shell on browse pages and the filtered count.
Change
- M3: read the cursor's
bson_twithbson_iter, the documented way to read BSON, in one pass per document that yields the typed cell, the kind per field (feeding every kind cache from one place), the absent-cell bits and the_idlocator. Produce Extended JSON only for nested documents and arrays, from the sub-document. Keep the raw BSON bytes as the stored form for editing and export. KeepJSONSerializationout of every hot loop. - M2: derive
PluginExportDocumentfrom the stored bytes when an export or copy asks, through a PluginKit requirement with a default, so export and copy still match the rows the grid showed. Take the size from thebson_tlength prefix. - Use the cached declared schema for tab open, filter and sort reloads; re-read it on an explicit Refresh and drop it in
tableDefinitionDidChangeand after acollModorcreatethe shell runs. - Measure the shell's share of a browse page with a signpost. If it is a real share, have the table browse path, which builds its own find (filter, sort, skip, limit) and never needs JavaScript, call
mongoc_collection_find_with_optsdirectly, and keep the shell for statements the user writes. - Give the filtered count the documented
limitoption (threshold + 1), the same capped count SQL engines use, so the server stops at the threshold.
Measure
- Signposts around the find,
iterateCursorJson,readDocuments,buildPluginResultand the shell evaluation. A 1,000-document and a 100,000-document find on 10-field and 50-field collections: time and persistent bytes after the result lands (Instruments Allocations), before and after. - Round trips per collection open with the mongod profiler: one find, no
listCollections. - A filtered tab on a 1M-document collection: count time and next-page latency while the count runs.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
What the first PR for this issue ships, and what is left:
bson_iterpass per document into the values, the canonical tree, the locator and the size, BSON bytes kept as the batch's stored form, Extended JSON only when JavaScript reads it. The export copy is built from that tree with the size from the length prefix (no second parse, nobson_new_from_json). Signposts around the shell, the cursor read, the decode and the grid build.PluginExportDocumentper row (TableRows.exportSnapshots, masking included), so deriving it on demand needs a PluginKit requirement that turns stored bytes into export documents and a place in the tab's row store for the bytes. That is the row store perf(results): hold every Mac result in the core's columnar store #4521 is replacing; it belongs with that work.collModorcreate.fetchFilteredRowCounttakes no limit, so the threshold + 1 cap needs a new PluginKit requirement with a default andRowCountPlan.filteredNonSQLcarrying the limit.mongoc_collection_find_with_optsitself.