From 6ce36014e49081308202302a5ca887575fc20cb4 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Wed, 9 Apr 2025 12:08:50 -0700 Subject: [PATCH] fix: prevent redundant pushes --- runner/src/storage/cache.ts | 38 +++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/runner/src/storage/cache.ts b/runner/src/storage/cache.ts index 038996ae0..8a05b0919 100644 --- a/runner/src/storage/cache.ts +++ b/runner/src/storage/cache.ts @@ -798,10 +798,10 @@ export class Provider implements StorageProvider { return entity?.is as StorageValue | undefined; } async send( - changes: { entityId: EntityId; value: StorageValue }[], + batch: { entityId: EntityId; value: StorageValue }[], ): Promise< Result< - Commit, + Unit, | ConflictError | TransactionError | ConnectionError @@ -810,17 +810,31 @@ export class Provider implements StorageProvider { | StoreError > > { - const { the } = this; - - const result = await this.workspace.push(changes.map((change) => ({ - the, - of: Provider.toEntity(change.entityId), - // ⚠️ We do JSON roundtrips to strip of the undefined values that - // cause problems with serialization. - is: JSON.parse(JSON.stringify(change.value)), - }))); + const { the, workspace } = this; + + const changes = []; + for (const { entityId, value } of batch) { + const of = Provider.toEntity(entityId); + const content = JSON.stringify(value); + + const current = workspace.get({ the, of }); + if (JSON.stringify(current) !== content) { + changes.push({ + the, + of, + // ⚠️ We do JSON roundtrips to strip of the undefined values that + // cause problems with serialization. + is: JSON.parse(content) as JSONValue, + }); + } + } - return result; + if (changes.length > 0) { + const result = await this.workspace.push(changes); + return result.error ? result : { ok: {} }; + } else { + return { ok: {} }; + } } parse(source: string): Memory.ProviderCommand {