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

Commit

Permalink
Merge pull request #56 from matrix-org/room_initial_sync_perf
Browse files Browse the repository at this point in the history
During room intial sync, only calculate current state once.
  • Loading branch information
NegativeMjark committed Feb 9, 2015
2 parents 375eba6 + d94f682 commit 5c5f5c1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
21 changes: 14 additions & 7 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,27 @@ def check(self, event, auth_events):
raise

@defer.inlineCallbacks
def check_joined_room(self, room_id, user_id):
member = yield self.state.get_current_state(
room_id=room_id,
event_type=EventTypes.Member,
state_key=user_id
)
def check_joined_room(self, room_id, user_id, current_state=None):
if current_state:
member = current_state.get(
(EventTypes.Member, user_id),
None
)
else:
member = yield self.state.get_current_state(
room_id=room_id,
event_type=EventTypes.Member,
state_key=user_id
)

self._check_joined_room(member, user_id, room_id)
defer.returnValue(member)

@defer.inlineCallbacks
def check_host_in_room(self, room_id, host):
curr_state = yield self.state.get_current_state(room_id)

for event in curr_state:
for event in curr_state.values():
if event.type == EventTypes.Member:
try:
if UserID.from_string(event.state_key).domain != host:
Expand Down
32 changes: 22 additions & 10 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MessageHandler(BaseHandler):
def __init__(self, hs):
super(MessageHandler, self).__init__(hs)
self.hs = hs
self.state = hs.get_state_handler()
self.clock = hs.get_clock()
self.validator = EventValidator()

Expand Down Expand Up @@ -225,7 +226,9 @@ def get_state_events(self, user_id, room_id):
# TODO: This is duplicating logic from snapshot_all_rooms
current_state = yield self.state_handler.get_current_state(room_id)
now = self.clock.time_msec()
defer.returnValue([serialize_event(c, now) for c in current_state])
defer.returnValue(
[serialize_event(c, now) for c in current_state.values()]
)

@defer.inlineCallbacks
def snapshot_all_rooms(self, user_id=None, pagin_config=None,
Expand Down Expand Up @@ -313,7 +316,7 @@ def snapshot_all_rooms(self, user_id=None, pagin_config=None,
)
d["state"] = [
serialize_event(c, time_now, as_client_event)
for c in current_state
for c in current_state.values()
]
except:
logger.exception("Failed to get snapshot")
Expand All @@ -329,21 +332,27 @@ def snapshot_all_rooms(self, user_id=None, pagin_config=None,
@defer.inlineCallbacks
def room_initial_sync(self, user_id, room_id, pagin_config=None,
feedback=False):
yield self.auth.check_joined_room(room_id, user_id)
current_state = yield self.state.get_current_state(
room_id=room_id,
)

yield self.auth.check_joined_room(
room_id, user_id,
current_state=current_state
)

# TODO(paul): I wish I was called with user objects not user_id
# strings...
auth_user = UserID.from_string(user_id)

# TODO: These concurrently
time_now = self.clock.time_msec()
state_tuples = yield self.state_handler.get_current_state(room_id)
state = [serialize_event(x, time_now) for x in state_tuples]
state = [
serialize_event(x, time_now)
for x in current_state.values()
]

member_event = (yield self.store.get_room_member(
user_id=user_id,
room_id=room_id
))
member_event = current_state.get((EventTypes.Member, user_id,))

now_token = yield self.hs.get_event_sources().get_current_token()

Expand All @@ -360,7 +369,10 @@ def room_initial_sync(self, user_id, room_id, pagin_config=None,
start_token = now_token.copy_and_replace("room_key", token[0])
end_token = now_token.copy_and_replace("room_key", token[1])

room_members = yield self.store.get_room_members(room_id)
room_members = [
m for m in current_state.values()
if m.type == EventTypes.Member
]

presence_handler = self.hs.get_handlers().presence_handler
presence = []
Expand Down
9 changes: 6 additions & 3 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ def initial_sync_for_room(self, room_id, sync_config, now_token,
room_id, sync_config, now_token,
)

current_state_events = yield self.state_handler.get_current_state(
current_state = yield self.state_handler.get_current_state(
room_id
)
current_state_events = current_state.values()

defer.returnValue(RoomSyncResult(
room_id=room_id,
Expand Down Expand Up @@ -347,9 +348,10 @@ def incremental_sync_with_gap_for_room(self, room_id, sync_config,

# TODO(mjark): This seems racy since this isn't being passed a
# token to indicate what point in the stream this is
current_state_events = yield self.state_handler.get_current_state(
current_state = yield self.state_handler.get_current_state(
room_id
)
current_state_events = current_state.values()

state_at_previous_sync = yield self.get_state_at_previous_sync(
room_id, since_token=since_token
Expand Down Expand Up @@ -431,6 +433,7 @@ def check_joined_room(self, sync_config, room_id, state_delta):
joined = True

if joined:
state_delta = yield self.state_handler.get_current_state(room_id)
res = yield self.state_handler.get_current_state(room_id)
state_delta = res.values()

defer.returnValue(state_delta)
2 changes: 1 addition & 1 deletion synapse/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_current_state(self, room_id, event_type=None, state_key=""):
defer.returnValue(res[1].get((event_type, state_key)))
return

defer.returnValue(res[1].values())
defer.returnValue(res[1])

@defer.inlineCallbacks
def compute_event_context(self, event, old_state=None):
Expand Down

0 comments on commit 5c5f5c1

Please sign in to comment.