Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit a576fa3

Browse files
authored
Update the stream_cache less often. (#16586)
Frequently only a single event is persisted at a time so this will not have an impact, but when multiple events are persisted at once then there is no need to continually update the stream change cache *except* for the last change.
1 parent 3157522 commit a576fa3

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

changelog.d/16586.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid updating the stream cache unnecessarily.

synapse/storage/databases/main/events.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,24 +1315,34 @@ def _update_room_depths_txn(
13151315
room_id: The room ID
13161316
events_and_contexts: events we are persisting
13171317
"""
1318+
stream_ordering: Optional[int] = None
13181319
depth_update = 0
13191320
for event, context in events_and_contexts:
1320-
# Then update the `stream_ordering` position to mark the latest
1321-
# event as the front of the room. This should not be done for
1322-
# backfilled events because backfilled events have negative
1323-
# stream_ordering and happened in the past so we know that we don't
1324-
# need to update the stream_ordering tip/front for the room.
1321+
# Don't update the stream ordering for backfilled events because
1322+
# backfilled events have negative stream_ordering and happened in the
1323+
# past, so we know that we don't need to update the stream_ordering
1324+
# tip/front for the room.
13251325
assert event.internal_metadata.stream_ordering is not None
13261326
if event.internal_metadata.stream_ordering >= 0:
1327-
txn.call_after(
1328-
self.store._events_stream_cache.entity_has_changed,
1329-
event.room_id,
1330-
event.internal_metadata.stream_ordering,
1331-
)
1327+
if stream_ordering is None:
1328+
stream_ordering = event.internal_metadata.stream_ordering
1329+
else:
1330+
stream_ordering = max(
1331+
stream_ordering, event.internal_metadata.stream_ordering
1332+
)
13321333

13331334
if not event.internal_metadata.is_outlier() and not context.rejected:
13341335
depth_update = max(event.depth, depth_update)
13351336

1337+
# Then update the `stream_ordering` position to mark the latest event as
1338+
# the front of the room.
1339+
if stream_ordering is not None:
1340+
txn.call_after(
1341+
self.store._events_stream_cache.entity_has_changed,
1342+
room_id,
1343+
stream_ordering,
1344+
)
1345+
13361346
self._update_min_depth_for_room_txn(txn, room_id, depth_update)
13371347

13381348
def _update_outliers_txn(

0 commit comments

Comments
 (0)