Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

performance: Avoid Recipient lookup for stream messages.

Steve Howell85ed6f332a86
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 99448bdc91a..e2bf6c82bd5 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -2335,9 +2335,20 @@ def check_message(sender: UserProfile, client: Client, addressee: Addressee,
stream = addressee.stream()
assert stream is not None
- recipient = stream.recipient
-
- # This will raise JsonableError if there are problems.
+ # To save a database round trip, we construct the Recipient
+ # object for the Stream rather than fetching it from the
+ # database using the stream.recipient foreign key.
+ #
+ # This is simpler than ensuring that code paths that fetch a
+ # Stream that will be used for sending a message have a
+ # `select_related("recipient"), which would also needlessly
+ # expand Stream objects in memory (all the fields of Recipient
+ # are already known given the Stream object).
+ recipient = Recipient(
+ id=stream.recipient_id,
+ type_id=stream.id,
+ type=Recipient.STREAM,
+ )
if sender.bot_type != sender.OUTGOING_WEBHOOK_BOT:
access_stream_for_send_message(
diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py
index 104526003f4..2259065cc1c 100644
--- a/zerver/tests/test_message_edit.py
+++ b/zerver/tests/test_message_edit.py
@@ -963,7 +963,7 @@ class EditMessageTest(ZulipTestCase):
'propagate_mode': 'change_all',
'topic': 'new topic',
})
- self.assertEqual(len(queries), 51)
+ self.assertEqual(len(queries), 50)
messages = get_topic_messages(user_profile, old_stream, "test")
self.assertEqual(len(messages), 1)
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index f27b5d14c57..19dfeae1871 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -1194,7 +1194,7 @@ class StreamMessagesTest(ZulipTestCase):
body=content,
)
- self.assert_length(queries, 14)
+ self.assert_length(queries, 13)
def test_stream_message_dict(self) -> None:
user_profile = self.example_user('iago')
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index d94e252b2f1..c63be7357f7 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -3838,7 +3838,7 @@ class SubscriptionAPITest(ZulipTestCase):
principals=orjson.dumps([user1.id, user2.id]).decode(),
),
)
- self.assert_length(queries, 49)
+ self.assert_length(queries, 48)
class GetStreamsTest(ZulipTestCase):
def test_streams_api_for_bot_owners(self) -> None: