Repository navigation
perf(sqlite): run SQLite statements on their own thread, not Swift's shared pool - #4565
Merged
Merged
Conversation
…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
enabled auto-merge (squash)
November 26, 2026 01:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SQLiteLocalBackendin the SQLite and libSQL plugins was a default actor, sosqlite3_step, the busy handler'susleepon a locked database, andPluginRowSpill.deliverpast its budget all blocked a thread of Swift's cooperative pool (about one per core).SQLiteConnectionExecutorinTableProSQLiteCore, SE-0392unownedExecutor). 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.pluginDispatchAsync, which would have meant dropping theActorprotocol and rewriting each call site for the same effect.enqueue(_: UnownedJob)is the form available on macOS 13; built with-swift-version 6for macOS 13, macOS 14 and iOS 18, it gives no deprecation warning.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 trivialTask.detached: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
unownedExecutorremoved it fails on!starved; with it, it passes.verify.sh testover everySQLiteDriverTestssuite (35, the thread test among them): PASS, 207 executed, 207 passed.verify.sh build: PASS.verify.sh plugins(libSQL is a registry plugin): PASS. iOSbuild-for-testingfor the generic Simulator, since the iOS app compiles the SQLite plugin and linksTableProSQLiteCore: TEST BUILD SUCCEEDED.verify.sh lint: PASS.Closes #4551