Skip to content

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

Merged
datlechin merged 6 commits into
mainfrom
perf/sqlite-own-executor
Nov 27, 2026
Merged

datlechin merged 6 commits into
mainfrom
perf/sqlite-own-executor

Conversation

@datlechin

@datlechin datlechin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

  • SQLiteLocalBackend in the SQLite and libSQL plugins was a default actor, so sqlite3_step, the busy handler's usleep on a locked database, and PluginRowSpill.deliver past its budget all blocked a thread of Swift's cooperative pool (about one per core).
  • Each backend now runs on a custom serial executor over a dispatch queue of its own (SQLiteConnectionExecutor in TableProSQLiteCore, SE-0392 unownedExecutor). The actor keeps its guarantees: one job at a time, and a statement still runs start to end with no await, so nothing reaches the handle mid-statement. SQLiteExecutionBackend: Actor, its callers and the agent backend are unchanged.
  • This uses a custom executor rather than moving every method behind pluginDispatchAsync, which would have meant dropping the Actor protocol and rewriting each call site for the same effect. enqueue(_: UnownedJob) is the form available on macOS 13; built with -swift-version 6 for macOS 13, macOS 14 and iOS 18, it gives no deprecation warning.
  • The busy handler now waits on the backend's queue, and Stop still ends it through SQLiteBusyState. The agent backend's spill (perf(export): MongoDB export documents, Parquet limits and unmeasured items left from #4434 #4444) and replacing the 2 ms polling (perf(storage): Server Dashboard reads and gated idle items left from #4435 #4462) are not in this PR.

Measured

Probe on this Mac (10 cores, macOS 13 target): 12 actors wait on a database another connection holds with BEGIN EXCLUSIVE, busy timeout 0, using the plugin's 10 ms busy handler, for 5 s. Then the probe times one trivial Task.detached:

trivial task ran after
default actor (before) 5.337 s, only once the lock went
own queue (after) 0.000 s

The same probe showed that waits on the pool also held back DispatchQueue.global() work, which is why the test's watchdog runs on a queue of its own.

Test plan

  • SQLiteLocalBackendThreadTests: two more readers than there are cores wait on an exclusively locked file. Other tasks must keep running for 2 s, before a watchdog on its own queue releases the lock, and every reader then reads its count.
    With the backend's unownedExecutor removed it fails on !starved; with it, it passes.
  • After the merge with main, which touched none of these files: verify.sh test over every SQLiteDriverTests suite (35, the thread test among them): PASS, 207 executed, 207 passed.
  • verify.sh build: PASS. verify.sh plugins (libSQL is a registry plugin): PASS. iOS build-for-testing for the generic Simulator, since the iOS app compiles the SQLite plugin and links TableProSQLiteCore: TEST BUILD SUCCEEDED. verify.sh lint: PASS.
  • The iOS suites have not run on this change: this Mac has no Simulator runtime, and CI's iOS job lacks one until ci(ios): install the simulator runtime when the runner image lacks it #4607.
  • Codex review and security pass: no findings.

Closes #4551

…shared pool

The local SQLite and libSQL backends were default actors, so a statement, a
busy-handler wait on a locked database and a spill past its budget each
blocked one of the cooperative pool's threads, about one per core. Give each
backend a custom serial executor over its own dispatch queue
(SQLiteConnectionExecutor). The actor still runs one job at a time and a
statement still runs start to end without an await, so nothing else reaches
the handle mid-statement.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:50
@datlechin
datlechin merged commit bb6d4a3 into main Nov 27, 2026
32 of 35 checks passed
@datlechin
datlechin deleted the perf/sqlite-own-executor branch November 27, 2026 16:36
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(sqlite): run SQLite statements on their own thread, not Swift's shared pool

1 participant