From cb3832a14761b7f008d2f22735265f29d46a0784 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 8 Sep 2017 10:05:13 -0700 Subject: [PATCH] Use sets, not lists, for mobile_user_ids. This avoids an O(N-squared) hit during presence queries. The speedup here is probably negligible compared to everything else going on, but sets are more semantically correct, anyway. --- zerver/models.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/zerver/models.py b/zerver/models.py index 447bcd0175..151966d441 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1627,10 +1627,9 @@ class UserPresence(models.Model): 'user_profile__enable_offline_push_notifications', ) + mobile_user_ids = set() # type: Set[int] if PushDeviceToken.objects.filter(user=user_profile).exists(): - mobile_user_ids = [user_profile.id] # type: List[int] - else: - mobile_user_ids = [] + mobile_user_ids.add(user_profile.id) return UserPresence.get_status_dicts_for_query(query, mobile_user_ids) @@ -1669,17 +1668,19 @@ class UserPresence(models.Model): 'user_profile__enable_offline_push_notifications', ) - mobile_user_ids = [row['user'] for row in PushDeviceToken.objects.filter( + mobile_query = PushDeviceToken.objects.filter( user__realm_id=realm_id, user__is_active=True, user__is_bot=False, - ).distinct("user").values("user")] + ) + + mobile_user_ids = set(mobile_query.distinct("user_id").values_list("user_id", flat=True)) return UserPresence.get_status_dicts_for_query(query, mobile_user_ids) @staticmethod def get_status_dicts_for_query(query, mobile_user_ids): - # type: (QuerySet, List[int]) -> Dict[Text, Dict[Any, Any]] + # type: (QuerySet, Set[int]) -> Dict[Text, Dict[Any, Any]] info_row_dct = defaultdict(list) # type: DefaultDict[Text, List[Dict[str, Any]]] for row in query: