Repository navigation
Skip the inline snapshot lookup when a commit only starts streams - #5671
Merged
Merged
Conversation
A single-stream inline projection asks its projection storage only for the snapshots of streams being appended to, so a commit that only starts streams asks for an empty id array. ProjectionStorage.LoadManyAsync passed that on to LightweightDocumentStorage.LoadManyAsync or LoadManyProjectedAsync, neither of which short-circuits it, so every such commit still ran `select ... where id = ANY($1)` -- one round trip, plus a connection of its own with UseIdentityMapForAggregates off, for a query that can only return no rows. Only IdentityMapDocumentStorage already skipped it (JasperFx#5258). ProjectionStorage.LoadManyAsync now returns an empty dictionary for an empty id array. Which streams are looked up is unchanged: appends still load their snapshot, and starting a stream whose id already exists still fails with ExistingStreamIdCollisionException.
Member
|
@lahma Good catch. I thought we already had that optimization |
lahma
marked this pull request as ready for review
October 15, 2026 12:04
This was referenced Oct 16, 2026
This was referenced Oct 16, 2026
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.
What was wasted
When a session that only starts streams is saved with an inline single-stream snapshot, every commit still ran a separate query before the write batch:
That query always returns no rows. It costs one round trip for each commit that starts a stream. With
UseIdentityMapForAggregatesoff, which is the default, it also opens a second connection, because the load goes throughLoadManyProjectedAsync.Why
JasperFxSingleStreamProjectionBase(in JasperFx.Events) asks projection storage only for the ids of streams withStreamActionType.Append. A commit that only starts streams therefore passes an empty id array.ProjectionStorage.LoadManyAsyncpasses the empty array on toLightweightDocumentStorage.LoadManyAsyncor toLoadManyProjectedAsync. Neither of them short-circuits an empty array.IdentityMapDocumentStoragealready returned early in that case (#5258), but lightweight sessions and the projected path did not.The change
ProjectionStorage.LoadManyAsyncnow returns an empty dictionary when it is given no ids. That one place covers every caller: single-stream and multi-stream inline projections and the daemon'sAggregationRunner, withUseIdentityMapForAggregateson or off.Why it is safe
The set of streams that get looked up does not change. The empty array was never going to find anything, so skipping it changes no result:
ExistingStreamIdCollisionExceptionat commit, and the existing snapshot is left unchanged. That id was already excluded from the lookup before this change; only the empty query is gone.Tests
New file:
EventSourcingTests/Projections/inline_snapshot_reads_when_starting_streams.cs. It counts the selects against the aggregate's table through Npgsql'sActivitySource, so the separate connection used with the flag off is counted too. Every case runs withUseIdentityMapForAggregatesboth on and off:ExistingStreamIdCollisionException; snapshot unchangedBefore the fix, 4 of the 10 cases failed: the start-only and two-tenant cases, with each flag setting. After the fix, all of them pass, and so does
inline_cache_hit_query_counts. The fullEventSourcingTestsrun on net10.0 passes, except for the fivetesting_projectionstests that hard-codelocalhost:5432; my local database was on a different port.