From f3fdded07dec064a891f2c9b7fa234537fda6120 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Sun, 2 Aug 2015 16:02:47 +0200 Subject: [PATCH] Edit.json: preload annotation users The expensive part of fetching an edit is JSONifying the clips' annotations. Profiling showed that the main cost was Annotation.json(), and within that: File: /srv/pandora/pandora/annotation/models.py Function: json at line 216 Line # Hits Time Per Hit % Time Line Contents ============================================================== 216 def json(self, layer=False, keys=None, user=None): 217 632 827 1.3 0.1 j = { 218 632 1048170 1658.5 89.6 'user': self.user.username, 219 } Obviously this join just moves some of the cost further out, but it brings my micro-benchmark down from 1.3s to 0.3s. --- pandora/edit/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandora/edit/models.py b/pandora/edit/models.py index 2991e0a3e..ad33d438b 100644 --- a/pandora/edit/models.py +++ b/pandora/edit/models.py @@ -522,7 +522,7 @@ class Clip(models.Model): if user and user.is_anonymous(): user = None qs = qs.filter(user=user) - for a in qs.order_by('start'): + for a in qs.order_by('start').select_related('user'): ll.append(a.json(user=user)) return layers