From f7aab9c92cdbd465412bf1950b0e7be5ceba48bc Mon Sep 17 00:00:00 2001 From: Signior-X Date: Wed, 7 Apr 2021 02:33:22 +0530 Subject: [PATCH] send_message: Ensure sender receives message events first. This commit implements a subtle optimization (described in more detail in the comment) that can save a few hundred milliseconds in when the sender sees that their message has sent when sending to very large streams. Fixes #17898. --- zerver/lib/actions.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index ae82636c93..ffe597df54 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1928,6 +1928,15 @@ def do_send_messages( about changing the next line. """ user_ids = send_request.active_user_ids | set(user_flags.keys()) + sender_id = send_request.message.sender_id + + # We make sure the sender is listed first in the `users` list; + # this results in the sender receiving the message first if + # there are thousands of recipients, decreasing perceived latency. + if sender_id in user_ids: + user_list = [sender_id] + list(user_ids - {sender_id}) + else: + user_list = list(user_ids) users = [ dict( @@ -1938,7 +1947,7 @@ def do_send_messages( stream_email_notify=(user_id in send_request.stream_email_user_ids), wildcard_mention_notify=(user_id in send_request.wildcard_mention_user_ids), ) - for user_id in user_ids + for user_id in user_list ] if send_request.message.is_stream_message():