From 29c6455de4aa6ec54ce2e251d23d3d517d13074a Mon Sep 17 00:00:00 2001 From: Christiaan Landman Date: Thu, 31 Jul 2025 11:43:33 +0200 Subject: [PATCH] Only calling db.export() if a persister is specified, exporting the db is not needed if writing would be a no-op. Improves performance. --- .changeset/short-hotels-grow.md | 5 +++++ packages/adapter-sql-js/src/SQLJSAdapter.ts | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 .changeset/short-hotels-grow.md diff --git a/.changeset/short-hotels-grow.md b/.changeset/short-hotels-grow.md new file mode 100644 index 000000000..3a6d422a2 --- /dev/null +++ b/.changeset/short-hotels-grow.md @@ -0,0 +1,5 @@ +--- +'@powersync/adapter-sql-js': patch +--- + +Only calling `db.export()` if a persister is specified, otherwise it is no-op. Improves in-memory performance. diff --git a/packages/adapter-sql-js/src/SQLJSAdapter.ts b/packages/adapter-sql-js/src/SQLJSAdapter.ts index edc3aa602..4d35ee9f7 100644 --- a/packages/adapter-sql-js/src/SQLJSAdapter.ts +++ b/packages/adapter-sql-js/src/SQLJSAdapter.ts @@ -29,7 +29,7 @@ export interface SQLJSOpenOptions extends SQLOpenOptions { } export interface ResolvedSQLJSOpenOptions extends SQLJSOpenOptions { - persister: SQLJSPersister; + persister?: SQLJSPersister; logger: ILogger; } @@ -96,21 +96,19 @@ export class SQLJSDBAdapter extends BaseObserver implements D }); this.writeScheduler = new ControlledExecutor(async (db: SQLJs.Database) => { + if (!this.options.persister) { + return; + } + await this.options.persister.writeFile(db.export()); }); } protected resolveOptions(options: SQLJSOpenOptions): ResolvedSQLJSOpenOptions { - const persister = options.persister ?? { - readFile: async () => null, - writeFile: async () => {} - }; - const logger = options.logger ?? createLogger('SQLJSDBAdapter'); return { ...options, - persister, logger }; } @@ -125,7 +123,7 @@ export class SQLJSDBAdapter extends BaseObserver implements D this.options.logger.error('[stderr]', text); } }); - const existing = await this.options.persister.readFile(); + const existing = await this.options.persister?.readFile(); const db = new SQL.Database(existing); this.dbP = db['db']; this._db = db; @@ -262,7 +260,10 @@ export class SQLJSDBAdapter extends BaseObserver implements D const db = await this.getDB(); const result = await fn(this.generateLockContext()); - this.writeScheduler.schedule(db); + // No point to schedule a write if there's no persister. + if (this.options.persister) { + this.writeScheduler.schedule(db); + } const notification: BatchedUpdateNotification = { rawUpdates: [],