messages: Eliminate redundant realm fetch in has_message_access.

Accessing .realm will cause a fetch query from the database if the
attribute hasn't been fetched already earlier in the codepath. That's
completely redundant if we're just comparing realms, and we should only
access .realm_id attribute. This seems to eliminate a query in some
codepaths, which is nice in this performance-sensitive function.
This commit is contained in:
Mateusz Mandera 2023-01-10 22:41:07 +01:00 committed by Tim Abbott
parent a7fd994cbd
commit 89d1f1f385
2 changed files with 5 additions and 5 deletions

View File

@ -826,7 +826,7 @@ def has_message_access(
else:
assert stream.recipient_id == message.recipient_id
if stream.realm != user_profile.realm:
if stream.realm_id != user_profile.realm_id:
# You can't access public stream messages in other realms
return False

View File

@ -1412,7 +1412,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_mutes(desdemona, muted_topics)
set_topic_mutes(cordelia, muted_topics)
with self.assert_database_query_count(31):
with self.assert_database_query_count(30):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@ -1443,7 +1443,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_mutes(desdemona, muted_topics)
set_topic_mutes(cordelia, muted_topics)
with self.assert_database_query_count(33):
with self.assert_database_query_count(32):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@ -1476,7 +1476,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_mutes(desdemona, muted_topics)
set_topic_mutes(cordelia, muted_topics)
with self.assert_database_query_count(31):
with self.assert_database_query_count(30):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@ -1499,7 +1499,7 @@ class EditMessageTest(EditMessageTestCase):
second_message_id = self.send_stream_message(
hamlet, stream_name, topic_name="changed topic name", content="Second message"
)
with self.assert_database_query_count(25):
with self.assert_database_query_count(24):
check_update_message(
user_profile=desdemona,
message_id=second_message_id,