gather_subscriptions: Fix making O(streams) database queries.

This cuts the MIT Zephyr home page load time and also #subscriptions
database-sourced load time for me from about a second to about 50ms on
my laptop.  The home page still takes about 600ms to load due to
templating.

(imported from commit 1cfd8c750301abe6b6a881be4b2857532be947ec)
This commit is contained in:
Tim Abbott
2013-02-12 14:42:59 -05:00
parent ea7005e8e3
commit 2f4b62048e

View File

@@ -427,18 +427,25 @@ def gather_subscriptions(user_profile):
with_color = StreamColor.objects.filter(subscription__in = subs).select_related()
no_color = subs.exclude(id__in = with_color.values('subscription_id')).select_related()
stream_ids = [sc.subscription.recipient.type_id for sc in with_color] + \
[sub.recipient.type_id for sub in no_color]
stream_hash = {}
for stream in Stream.objects.filter(id__in=stream_ids):
stream_hash[stream.id] = (stream.name, stream.invite_only)
result = []
for sc in with_color:
stream_name = get_display_recipient(sc.subscription.recipient)
(stream_name, invite_only) = stream_hash[sc.subscription.recipient.type_id]
result.append({'name': stream_name,
'in_home_view': sc.subscription.in_home_view,
'invite_only': get_stream(stream_name, user_profile.realm).invite_only,
'invite_only': invite_only,
'color': sc.color})
for sub in no_color:
stream_name = get_display_recipient(sub.recipient)
(stream_name, invite_only) = stream_hash[sub.recipient.type_id]
result.append({'name': stream_name,
'in_home_view': sub.in_home_view,
'invite_only': get_stream(stream_name, user_profile.realm).invite_only,
'invite_only': invite_only,
'color': StreamColor.DEFAULT_STREAM_COLOR})
return sorted(result)