summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2018-07-24 13:40:42 +0100
committerErik Johnston <erik@matrix.org>2018-07-24 14:18:23 +0100
commit811ac73a42db16db7211a7517ad5651a1f6aee71 (patch)
tree138681a72b9bc06c472a5057cc39d82261bbfe35 /synapse
parentHave _get_new_state_after_events return delta (diff)
downloadsynapse-811ac73a42db16db7211a7517ad5651a1f6aee71.tar.xz
Don't fetch state from the database unless needed
Diffstat (limited to 'synapse')
-rw-r--r--synapse/storage/events.py30
1 files changed, 18 insertions, 12 deletions
diff --git a/synapse/storage/events.py b/synapse/storage/events.py
index 6d2aae9c4a..eb8bbe13ab 100644
--- a/synapse/storage/events.py
+++ b/synapse/storage/events.py
@@ -631,27 +631,33 @@ class EventsStore(EventsWorkerStore):
         if old_state_groups == new_state_groups:
             defer.returnValue((None, None))
 
-        # Now that we have calculated new_state_groups we need to get
-        # their state IDs so we can resolve to a single state set.
-        missing_state = new_state_groups - set(state_groups_map)
-        if missing_state:
-            group_to_state = yield self._get_state_for_groups(missing_state)
-            state_groups_map.update(group_to_state)
-
         if len(new_state_groups) == 1:
             # If there is only one state group, then we know what the current
             # state is.
-            new_state_group = new_state_groups.pop()
-
-            delta_ids = None
             if len(old_state_groups) == 1:
-                old_state_group = old_state_groups.pop()
+                new_state_group = next(iter(new_state_groups))
+                old_state_group = next(iter(old_state_groups))
 
                 delta_ids = state_group_deltas.get(
                     (old_state_group, new_state_group,), None
                 )
+                if delta_ids is not None:
+                    # We have a delta from the existing to new current state,
+                    # so lets just return that. If we happen to already have
+                    # the current state in memory then lets also return that,
+                    # but it doesn't matter if we don't.
+                    new_state = state_groups_map.get(new_state_group)
+                    defer.returnValue((new_state, delta_ids))
 
-            defer.returnValue((state_groups_map[new_state_group], delta_ids))
+        # Now that we have calculated new_state_groups we need to get
+        # their state IDs so we can resolve to a single state set.
+        missing_state = new_state_groups - set(state_groups_map)
+        if missing_state:
+            group_to_state = yield self._get_state_for_groups(missing_state)
+            state_groups_map.update(group_to_state)
+
+        if len(new_state_groups) == 1:
+            defer.returnValue((state_groups_map[new_state_groups.pop()], None))
 
         # Ok, we need to defer to the state handler to resolve our state sets.