Skip to content

Commit 8d03a4d

Browse files
authored
Avoid re-computing the event ID when cloning events. (#19527)
`event_id` is a lazily-computed property on events, as it's a hash of the event content on room version 3 and later. The reason we do this is that it helps finding database inconsistencies by not trusting the event ID we got from the database. The thing is, when we clone events (to return them through /sync or /messages for example) we don't copy the computed hash if we already computed it, duplicating the work. This copies the internal `_event_id` property.
1 parent 18f717d commit 8d03a4d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

changelog.d/19527.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid re-computing the event ID when cloning events.

synapse/events/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from synapse.logging.opentracing import SynapseTags, set_tag, trace
4949
from synapse.types import JsonDict, Requester
5050

51-
from . import EventBase, StrippedStateEvent, make_event_from_dict
51+
from . import EventBase, FrozenEventV2, StrippedStateEvent, make_event_from_dict
5252

5353
if TYPE_CHECKING:
5454
from synapse.handlers.relations import BundledAggregations
@@ -109,6 +109,14 @@ def clone_event(event: EventBase) -> EventBase:
109109
event.get_dict(), event.room_version, event.internal_metadata.get_dict()
110110
)
111111

112+
# Starting FrozenEventV2, the event ID is an (expensive) hash of the event. This is
113+
# lazily computed when we get the FrozenEventV2.event_id property, then cached in
114+
# _event_id field. Later FrozenEvent formats all inherit from FrozenEventV2, so we
115+
# can use the same logic here.
116+
if isinstance(event, FrozenEventV2) and isinstance(new_event, FrozenEventV2):
117+
# If we already pre-computed the event ID, use it.
118+
new_event._event_id = event._event_id
119+
112120
# Copy the bits of `internal_metadata` that aren't returned by `get_dict`.
113121
new_event.internal_metadata.stream_ordering = (
114122
event.internal_metadata.stream_ordering

0 commit comments

Comments
 (0)