Skip to content

perf(results): driver calls and the work after each await run on the main actor #4530

Done

Description

@datlechin

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:131 for plugins, :499 for the app), and the shared packages enable NonisolatedNonsendingByDefault (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 no SWIFT_DEFAULT_ACTOR_ISOLATION. So every nonisolated async function runs on its caller's actor (SE-0461), and only @concurrent ones 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 at apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift:271-278, then QueryExecutor, also @MainActor (apple/TablePro/Core/Services/Query/QueryExecutor.swift:90).
  • QueryExecutor.fetchBoundedQueryData, fetchExplainPlan, fetchQueryData and fetchQueryDataParameterized are nonisolated static without @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.
  • PluginDriverAdapter is the app's one boundary to every plugin (apple/TablePro/Core/Plugins/PluginDriverAdapter.swift:15). Its async entry points (executeUserQuery at :472 and :1405-1420, executeBoundedQuery at :494-500, the fetch* catalog reads from :581) are plain async. Its only @concurrent function is mapPluginTables (: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: MongoDBPluginDriver is a nonisolated final class (apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift:11), and runScript calls buildPluginResult after the await (MongoDBPluginDriver.swift:237-260), which runs unionColumns, columnKinds, the remember* passes and flatten (:1056-1096). ClickHouse classifies the response after the await (apple/Plugins/ClickHouseDriverPlugin/ClickHousePluginDriver+Http.swift:113-139). SQLite's bounded read goes through PluginBoundedStream.collect (apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift:365-368, apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift:1679), whose for try await loop 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 PluginDriverAdapter async entry points @concurrent. DatabaseDriver is already Sendable (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 @concurrent caller runs on the global executor, so the plugin protocol in TableProPluginKit does not change and TableProPluginKitVersion does not move.
  • Mark QueryExecutor.fetchBoundedQueryData, fetchExplainPlan, fetchQueryData and fetchQueryDataParameterized @concurrent nonisolated static, and remove the wrong comment at QueryExecutor.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 whether executeUserQuery, executeBoundedQuery and a streamed collect ran on the main thread, called through PluginDriverAdapter from a @MainActor caller. It fails on main today.

Measure

  • Debug builds: dispatchPrecondition(condition: .notOnQueue(.main)) at BsonDocumentFlattener.flatten (apple/Plugins/MongoDBDriverPlugin/BsonDocumentFlattener.swift:83), ClickHouseResponseClassifier.classify, PluginBoundedStream.collect and PluginDriverAdapter.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 the TableLoadTracer stages driverFetchBegin to driverFetchEnd and gridReloadEnd to mainRunLoopIdle (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

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 26, 2026

    @datlechin
    MemberAuthor

    Measured while fixing this in #4595: the driver call itself already runs off the main thread. TableProPluginKit is the one target built without SWIFT_APPROACHABLE_CONCURRENCY, so every PluginDatabaseDriver requirement keeps the old nonisolated async meaning and hops to the global executor; the plugin's own work after an await (MongoDB's flatten, ClickHouse's classify, PluginBoundedStream.collect) runs there. A stub driver called through PluginDriverAdapter from a @MainActor test 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, including fetchAllColumns and fetchAllForeignKeys over a whole schema. #4595 moves that. Not done here: the Time Profiler and Hangs passes against live MongoDB and ClickHouse servers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions