Merge pull request #3043 from matrix-org/erikj/measure_state_group_creation

Measure time it takes to calculate state group ID
This commit is contained in:
Erik Johnston 2018-03-28 17:40:14 +01:00 committed by GitHub
commit 88cc9cc69e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -483,33 +483,34 @@ class StateResolutionHandler(object):
key: e_ids.pop() for key, e_ids in state.iteritems() key: e_ids.pop() for key, e_ids in state.iteritems()
} }
# if the new state matches any of the input state groups, we can with Measure(self.clock, "state.create_group_ids"):
# use that state group again. Otherwise we will generate a state_id # if the new state matches any of the input state groups, we can
# which will be used as a cache key for future resolutions, but # use that state group again. Otherwise we will generate a state_id
# not get persisted. # which will be used as a cache key for future resolutions, but
state_group = None # not get persisted.
new_state_event_ids = frozenset(new_state.itervalues()) state_group = None
for sg, events in state_groups_ids.iteritems(): new_state_event_ids = frozenset(new_state.itervalues())
if new_state_event_ids == frozenset(e_id for e_id in events): for sg, events in state_groups_ids.iteritems():
state_group = sg if new_state_event_ids == frozenset(e_id for e_id in events):
break state_group = sg
break
# TODO: We want to create a state group for this set of events, to # TODO: We want to create a state group for this set of events, to
# increase cache hits, but we need to make sure that it doesn't # increase cache hits, but we need to make sure that it doesn't
# end up as a prev_group without being added to the database # end up as a prev_group without being added to the database
prev_group = None prev_group = None
delta_ids = None delta_ids = None
for old_group, old_ids in state_groups_ids.iteritems(): for old_group, old_ids in state_groups_ids.iteritems():
if not set(new_state) - set(old_ids): if not set(new_state) - set(old_ids):
n_delta_ids = { n_delta_ids = {
k: v k: v
for k, v in new_state.iteritems() for k, v in new_state.iteritems()
if old_ids.get(k) != v if old_ids.get(k) != v
} }
if not delta_ids or len(n_delta_ids) < len(delta_ids): if not delta_ids or len(n_delta_ids) < len(delta_ids):
prev_group = old_group prev_group = old_group
delta_ids = n_delta_ids delta_ids = n_delta_ids
cache = _StateCacheEntry( cache = _StateCacheEntry(
state=new_state, state=new_state,