From 9265b8a53b995c35d4fe4fb2dab14a54e3ff7bf9 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Sun, 4 Oct 2015 11:17:22 +0200 Subject: [PATCH] Clip.save: fetch annotations once, not ~ 2 * n_layers With 17 layers and 12 clipLayers, this repeated fetching was around 49% of the cost of this function, which was in turn 94% of the cost of creating many new annotations with mostly-unique endpoints. This helps a bit... If the order of clipLayers is not meant to be significant to sortvalue (which I assume it is) then this could be simpler. --- pandora/clip/models.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pandora/clip/models.py b/pandora/clip/models.py index 94b319cc0..ebb009897 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -70,19 +70,25 @@ class MetaClip: self.user = self.item.user and self.item.user.id self.sort = self.item.sort if self.id: - sortvalue = '' - if self.id: - for l in settings.CONFIG.get('clipLayers', []): - sortvalue += ''.join(filter(lambda s: s, - [a.sortvalue - for a in self.annotations.filter(layer=l).order_by('sortvalue')])) + anns = self.annotations.order_by('layer', 'sortvalue') + anns_by_layer = {} + for ann in anns: + anns_by_layer.setdefault(ann.layer, []).append(ann) + + sortvalue = ''.join(( + a.sortvalue + for l in settings.CONFIG.get('clipLayers', []) + for a in anns_by_layer.get(l, []) + if a.sortvalue + )) if sortvalue: self.sortvalue = sortvalue[:900] else: self.sortvalue = None - self.findvalue = '\n'.join(filter(None, [a.findvalue for a in self.annotations.all()])) + + self.findvalue = '\n'.join(filter(None, [a.findvalue for a in anns])) for l in [k['id'] for k in settings.CONFIG['layers']]: - setattr(self, l, self.annotations.filter(layer=l).count()>0) + setattr(self, l, len(anns_by_layer[l]) if l in anns_by_layer else 0) models.Model.save(self, *args, **kwargs) clip_keys = ('id', 'in', 'out', 'position', 'created', 'modified',