Skip to content

Commit 705eeab

Browse files
fix(unreal): suppress spurious OnInsert on overlapping subscription refcount bump
ClientCache::ApplyDiff phase 2 unconditionally appended every incoming row to Diff.Inserts, even when the row was already cached and only the refcount needed to bump. BroadcastDiff then fired OnInsert for every entry, so any table subscribed via two overlapping queries (e.g. a global SELECT plus a per-row WHERE) re-fired its insert handler on every later subscription apply. Mirror the delete path: only emit a Diff.Inserts entry when the row-bytes key transitions from absent to refcount=1. Real updates are unaffected — update bytes differ from old bytes, take the !Entry path, and DeriveUpdatesByPrimaryKey still reconciles them into OnUpdate.
1 parent 458eac8 commit 705eeab

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

  • sdks/unreal/src/SpacetimeDbSdk/Source/SpacetimeDbSdk/Public/DBCache

sdks/unreal/src/SpacetimeDbSdk/Source/SpacetimeDbSdk/Public/DBCache/ClientCache.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,24 @@ class UClientCache
120120

121121
TSharedPtr<RowType> NewRow = MakeShared<RowType>(Row);
122122

123-
// This is a new row or an update to an existing row that was not deleted.
124123
FRowEntry<RowType>* Entry = Table->Entries.Find(Key);
125124
if (!Entry)
126125
{
127-
// True insert
126+
// True insert — these row-bytes are not cached yet. Either a
127+
// genuinely new row, or the insert half of an update (the
128+
// paired delete of different bytes is tracked in phase 1/3;
129+
// DeriveUpdatesByPrimaryKey reconciles them by PK afterward).
128130
Table->Entries.Add(Key, FRowEntry<RowType>{NewRow, 1});
131+
Diff.Inserts.Add(Key, *NewRow);
129132
}
130133
else
131134
{
132-
// True update
135+
// Refcount bump — an overlapping subscription brought an
136+
// identical row already in cache. Mirror the delete path
137+
// (which only emits Diff.Deletes on refcount == 0) by not
138+
// emitting a spurious Diff.Inserts entry here.
133139
Table->Entries.Add(Key, FRowEntry<RowType>{NewRow, Entry->RefCount + 1});
134140
}
135-
136-
Diff.Inserts.Add(Key, *NewRow);
137141
}
138142

139143
// Phase 3: Finalize Deletes and Update Indices

0 commit comments

Comments
 (0)