From 1951d75796c05e7f386b99e8fcd9e4f0e4a2bf64 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 18 Oct 2020 12:10:22 +0000 Subject: [PATCH] performance: Avoid select_related("realm"). We also move this query up in the function for some future refactorings. --- zerver/lib/actions.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index d30efc281e..ac2bf1644d 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -4904,6 +4904,18 @@ def get_web_public_subs(realm: Realm) -> SubHelperT: # subscriptions, so it's worth optimizing. def gather_subscriptions_helper(user_profile: UserProfile, include_subscribers: bool=True) -> SubHelperT: + realm = user_profile.realm + all_streams = get_active_streams(realm).values( + *Stream.API_FIELDS, + # The realm_id and recipient_id are generally not needed in the API. + "realm_id", + "is_web_public", + "recipient_id", + # email_token isn't public to some users with access to + # the stream, so doesn't belong in API_FIELDS. + "email_token", + ) + sub_dicts = get_stream_subscriptions_for_user(user_profile).values( *Subscription.API_FIELDS, "recipient_id").order_by("recipient_id") @@ -4922,17 +4934,6 @@ def gather_subscriptions_helper(user_profile: UserProfile, recent_traffic = get_streams_traffic(stream_ids=stream_ids) - all_streams = get_active_streams(user_profile.realm).select_related( - "realm").values( - *Stream.API_FIELDS, - # The realm_id and recipient_id are generally not needed in the API. - "realm_id", - "is_web_public", - "recipient_id", - # email_token isn't public to some users with access to - # the stream, so doesn't belong in API_FIELDS. - "email_token") - stream_dicts = [stream for stream in all_streams if stream['id'] in stream_ids] stream_hash = {} web_public_stream_ids = [stream['id'] for stream in all_streams if stream['is_web_public']]