From b9b45494ce32a128a25e9d9f3595057940714246 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Mon, 16 Nov 2026 16:19:22 +0100 Subject: [PATCH] perf: start statements directly when the queue is free Every async statement went through the queue's new Promise, an entry object, two closures and an array push and splice, even when nothing was waiting. When no transaction or synchronous call holds the queue and nothing is queued, a statement now starts right away and chains the queue update onto its own promise. Statements behind a transaction still wait in order. Co-Authored-By: Claude Opus 5.5 --- .../src/DatabaseQueue.ts | 62 +++++++++++++++---- .../src/__tests__/DatabaseQueue.test.ts | 35 +++++++++++ 2 files changed, 86 insertions(+), 11 deletions(-) diff --git a/packages/react-native-nitro-sqlite/src/DatabaseQueue.ts b/packages/react-native-nitro-sqlite/src/DatabaseQueue.ts index 3f84c1a0..1f65d1d1 100644 --- a/packages/react-native-nitro-sqlite/src/DatabaseQueue.ts +++ b/packages/react-native-nitro-sqlite/src/DatabaseQueue.ts @@ -93,18 +93,15 @@ function enqueueOperation( return Promise.reject(error) } + // Nothing is waiting and no transaction or synchronous call holds the queue. + const canStartStatementNow = + kind === 'statement' && + databaseQueue.queue.length === 0 && + (!databaseQueue.inProgress || databaseQueue.activeStatements > 0) + if (canStartStatementNow) return startStatement(databaseQueue, callback) + return new Promise((resolve, reject) => { - const finish = () => { - if (kind === 'statement') { - databaseQueue.activeStatements-- - if (databaseQueue.activeStatements === 0) { - databaseQueue.inProgress = false - } - } else { - databaseQueue.inProgress = false - } - startNextOperations(databaseQueue) - } + const finish = () => finishOperation(databaseQueue, kind) // Chains the operation directly instead of awaiting it in an async function, // which saves a promise and several microtask turns per queued query. @@ -139,6 +136,49 @@ function enqueueOperation( }) } +/** Starts a statement right away and chains the queue update onto its promise, + * without the queued path's extra promise, entry object and closures. + */ +function startStatement( + databaseQueue: DatabaseQueue, + callback: () => Promise, +): Promise { + databaseQueue.inProgress = true + databaseQueue.activeStatements++ + let operation: Promise + try { + operation = Promise.resolve(callback()) + } catch (error) { + finishOperation(databaseQueue, 'statement') + return Promise.reject(error) + } + return operation.then( + (result) => { + finishOperation(databaseQueue, 'statement') + return result + }, + (error: unknown) => { + finishOperation(databaseQueue, 'statement') + throw error + }, + ) +} + +function finishOperation( + databaseQueue: DatabaseQueue, + kind: QueuedOperation['kind'], +) { + if (kind === 'statement') { + databaseQueue.activeStatements-- + if (databaseQueue.activeStatements === 0) { + databaseQueue.inProgress = false + } + } else { + databaseQueue.inProgress = false + } + startNextOperations(databaseQueue) +} + function startNextOperations(queue: DatabaseQueue) { if (queue.draining || (queue.inProgress && queue.activeStatements === 0)) { return diff --git a/packages/react-native-nitro-sqlite/src/__tests__/DatabaseQueue.test.ts b/packages/react-native-nitro-sqlite/src/__tests__/DatabaseQueue.test.ts index d4ac5985..6289762a 100644 --- a/packages/react-native-nitro-sqlite/src/__tests__/DatabaseQueue.test.ts +++ b/packages/react-native-nitro-sqlite/src/__tests__/DatabaseQueue.test.ts @@ -99,8 +99,12 @@ describe('DatabaseQueue', () => { const failed = queueStatementAsync(dbName, () => { throw new Error('sync failure') }) + const failedExclusive = queueOperationAsync(dbName, () => { + throw new Error('exclusive sync failure') + }) await expect(failed).rejects.toThrow('sync failure') + await expect(failedExclusive).rejects.toThrow('exclusive sync failure') expect(getDatabaseQueue(dbName)).toMatchObject({ inProgress: false, activeStatements: 0, @@ -129,6 +133,37 @@ describe('DatabaseQueue', () => { expect(getDatabaseQueue(dbName).inProgress).toBe(false) }) + it('starts statements without queuing them unless a transaction holds or awaits the queue', async () => { + openDatabaseQueue(dbName) + const transactionMayFinish = deferred() + const order: string[] = [] + + const before = queueStatementAsync(dbName, async () => { + order.push('before') + }) + expect(order).toEqual(['before']) + expect(getDatabaseQueue(dbName).queue).toHaveLength(0) + await before + + const transaction = queueOperationAsync(dbName, async () => { + order.push('transaction') + await transactionMayFinish.promise + }) + const during = queueStatementAsync(dbName, async () => { + order.push('during') + }) + expect(order).toEqual(['before', 'transaction']) + expect(getDatabaseQueue(dbName).queue).toHaveLength(1) + + transactionMayFinish.resolve() + await Promise.all([transaction, during]) + expect(order).toEqual(['before', 'transaction', 'during']) + expect(getDatabaseQueue(dbName)).toMatchObject({ + inProgress: false, + activeStatements: 0, + }) + }) + it('waits for every earlier statement before starting a transaction', async () => { openDatabaseQueue(dbName) const first = deferred()