Skip to content

perf(sqlite): run SQLite statements on their own thread, not Swift's shared pool #4551

Done

Description

@datlechin

Problem

A long SQLite statement holds one of Swift's cooperative threads for its whole run: a COUNT(*) over 10M rows, a 1M-row read, VACUUM, an export whose writer is slower than SQLite, or a wait on a database another process has locked. The cooperative pool has about one thread per core (about 10 on an M4). With no long statement the cost is nil. Once as many long SQLite operations run as there are cores, async work across the whole app stops until one finishes: other connections' results, schema loads and autocomplete. How often that happens is not measured.

With the busy timeout at 0, a locked database keeps its thread until the user presses Stop.

Cause

  • SQLiteLocalBackend is a plain actor with no custom executor (apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift:287), so its methods run on the global cooperative executor. macOS uses it for every local file connection (apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift:185-193).
  • runStatement steps the statement to its end synchronously on the actor (SQLiteExecutionBackend.swift:491). stream calls spill.deliver synchronously, and its comment says why: an await would let another call reach the handle mid-statement (:602-650, :639-640).
  • PluginRowSpill.deliver blocks its thread past the spool budget and is documented as "for a producer on its own thread" (apple/Plugins/SharedSpill/PluginRowSpill.swift:91-96). Past the budget it polls with usleep(2_000) (:281-292), then PluginRowStream.deliverBlocking polls again (apple/Plugins/TableProPluginKit/PluginStreamAbort.swift:99-125, :174).
  • sqliteBusyHandler calls usleep on the same thread (SQLiteExecutionBackend.swift:196-201), and shouldRetry returns true forever when timeoutMilliseconds is 0 (:187-193).
  • libSQL's local backend has the same shape (apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift:34).
  • PostgreSQL and MySQL avoid this by running their C libraries on their own serial queue through pluginDispatchAsync (apple/Plugins/TableProPluginKit/PluginConcurrencySupport.swift:3-31).

Overlaps #4444: its note that SQLiteAgentBackend calls the blocking PluginRowSpill.deliver from the cooperative pool (apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift:174) covers the remote agent path. Overlaps #4462: WI14 covers waking a blocked producer instead of polling every 2 ms. This issue adds the local backends and the busy handler.

Change

  • Confine SQLiteLocalBackend's state to a private serial DispatchQueue and enter it through pluginDispatchAsync, the pattern the PostgreSQL and MySQL plugins already use and test. The "no await mid-statement" guarantee stays, because a statement runs start to end on that queue. (A custom SerialExecutor also works on the macOS 13 floor, but its enqueue(UnownedJob) form is deprecated from macOS 14; the queue pattern avoids that.)
  • Apply the same change to libSQL's SQLiteLocalBackend.
  • The busy handler's waits then run on the backend's queue, not the shared pool. Stop keeps working through the existing cancel flag in SQLiteBusyState.
  • PluginRowSpill.deliver is then called from a thread that owns its wait, as its documentation requires. Replacing the 2 ms polling with a signal stays with WI14 in perf(storage): Server Dashboard reads and gated idle items left from #4435 #4462.

Measure

  • Start 12 concurrent SELECT COUNT(*) FROM trips_10m on separate SQLite connections and time a trivial round trip on another connection (a PostgreSQL SELECT 1 tab), before and after. Target: unchanged from the idle case.
  • Instruments Swift Concurrency template during the same run: no cooperative thread blocked in sqlite3_step or usleep.
  • Lock a database from another process with the busy timeout at 0: the app's other connections stay responsive, and Stop ends the wait.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
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