Repository navigation
Done
Labels
Description
Problem
Every table open, page turn, query run, phase-2 count and metadata read goes from the main actor to the plugin, and nothing in the chain leaves the main actor. Drivers that decode inside their own queue or actor (PostgreSQL, MySQL, SQLite, Redis) only pay a few hops on main. Drivers that do CPU work after an await do it on the main thread:
- MongoDB flattens the BSON documents and makes several passes over every document after
runtime.evaluate. - ClickHouse parses the whole HTTP body after the request returns. The other URLSession engines follow the same shape.
- SQLite and any streaming driver resume on main once per batch in
PluginBoundedStream.collect.
The cost grows with page size and column count, and a Fetch All on MongoDB or an HTTP engine becomes a main-thread hang. Not yet measured per engine.
Cause
- The app and every plugin bundle compile with
SWIFT_APPROACHABLE_CONCURRENCY: YES(apple/project.yml:131for plugins,:499for the app), and the shared packages enableNonisolatedNonsendingByDefault(apple/Packages/TableProCore/Package.swift:7,apple/Packages/TableProMacCore/Package.swift:7). The plugins were given the app's settings on purpose so one function has one meaning in the process (apple/project.yml:128-130, build(plugins): give the 41 plugin targets the app's concurrency settings #2958). The macOS app sets noSWIFT_DEFAULT_ACTOR_ISOLATION. So every nonisolatedasyncfunction runs on its caller's actor (SE-0461), and only@concurrentones leave it. - The chain starts on the main actor:
MainContentCoordinator.withExecutionDriver(apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift:34-54),DatabaseManager, which is@MainActor(apple/TablePro/Core/Database/DatabaseManager.swift:15-16) and runs the body atapple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift:271-278, thenQueryExecutor, also@MainActor(apple/TablePro/Core/Services/Query/QueryExecutor.swift:90). QueryExecutor.fetchBoundedQueryData,fetchExplainPlan,fetchQueryDataandfetchQueryDataParameterizedarenonisolated staticwithout@concurrent(QueryExecutor.swift:157,:195,:215,:251), so they run on main. The comment above them says "nonisolated, runs on background" (QueryExecutor.swift:151), which is wrong under these settings.PluginDriverAdapteris the app's one boundary to every plugin (apple/TablePro/Core/Plugins/PluginDriverAdapter.swift:15). Its async entry points (executeUserQueryat:472and:1405-1420,executeBoundedQueryat:494-500, thefetch*catalog reads from:581) are plainasync. Its only@concurrentfunction ismapPluginTables(:631-639), whose comment already notes "a caller on the main actor would map it there".- So the plugin's own nonisolated functions inherit main:
MongoDBPluginDriveris anonisolated final class(apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift:11), andrunScriptcallsbuildPluginResultafter the await (MongoDBPluginDriver.swift:237-260), which runsunionColumns,columnKinds, theremember*passes andflatten(:1056-1096). ClickHouse classifies the response after the await (apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift:113-139). SQLite's bounded read goes throughPluginBoundedStream.collect(apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift:365-368,apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift:1679), whosefor try awaitloop appends on main (apple/Plugins/TableProPluginKit/PluginBoundedStream.swift:21-59).
Change
Put the hop at the host boundary, the way SE-0461 documents it.
- Mark the
PluginDriverAdapterasync entry points@concurrent.DatabaseDriveris alreadySendable(apple/TablePro/Core/Database/DatabaseDriver.swift:15) and the adapter keeps its mutable state behind a lock (PluginDriverAdapter.swift:49), so only the hop moves. A plugin's nonisolated function called from a@concurrentcaller runs on the global executor, so the plugin protocol in TableProPluginKit does not change andTableProPluginKitVersiondoes not move. - Mark
QueryExecutor.fetchBoundedQueryData,fetchExplainPlan,fetchQueryDataandfetchQueryDataParameterized@concurrent nonisolated static, and remove the wrong comment atQueryExecutor.swift:151. - Keep the main-actor parts on main: the lease, admission and result install stay where they are; only the driver call and the mapping of its result leave.
- Add a test that pins the off-main behaviour: a stub
PluginDatabaseDriver, compiled with the plugin settings, records whetherexecuteUserQuery,executeBoundedQueryand a streamedcollectran on the main thread, called throughPluginDriverAdapterfrom a@MainActorcaller. It fails on main today.
Measure
- Debug builds:
dispatchPrecondition(condition: .notOnQueue(.main))atBsonDocumentFlattener.flatten(apple/Plugins/MongoDBDriverPlugin/BsonDocumentFlattener.swift:83),ClickHouseResponseClassifier.classify,PluginBoundedStream.collectandPluginDriverAdapter.mapQueryResult(PluginDriverAdapter.swift:1278). Each trips today and must not after. - Time Profiler with the main thread track and the Swift Concurrency instrument: open a MongoDB collection, Fetch All of 1M MongoDB documents, a 100,000-row ClickHouse
SELECT, and a SQLite table open. Compare main-thread busy ms, the Hangs instrument's count of hangs of 250 ms or more, and theTableLoadTracerstagesdriverFetchBegintodriverFetchEndandgridReloadEndtomainRunLoopIdle(apple/TablePro/Core/Diagnostics/TableLoadTrace.swift:28-36), before and after. - Target: no driver decode or result mapping samples on the main thread.
Part of the performance plan.
Activity
datlechin commentedon Nov 26, 2026
Measured while fixing this in #4595: the driver call itself already runs off the main thread.
TableProPluginKitis the one target built withoutSWIFT_APPROACHABLE_CONCURRENCY, so everyPluginDatabaseDriverrequirement keeps the oldnonisolated asyncmeaning and hops to the global executor; the plugin's own work after anawait(MongoDB's flatten, ClickHouse's classify,PluginBoundedStream.collect) runs there. A stub driver called throughPluginDriverAdapterfrom a@MainActortest recorded its calls off the main thread on main. What ran on the main thread was the adapter's side of each call:admit(the NUL scan and the database-move classifier), the statement's lexical description,readSchema, and the mapping of the answer, includingfetchAllColumnsandfetchAllForeignKeysover a whole schema. #4595 moves that. Not done here: the Time Profiler and Hangs passes against live MongoDB and ClickHouse servers.