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.
This commit is contained in:
Will Thompson 2015-10-04 11:17:22 +02:00 committed by j
parent 2a55bd3eec
commit 9265b8a53b

View file

@ -70,19 +70,25 @@ class MetaClip:
self.user = self.item.user and self.item.user.id self.user = self.item.user and self.item.user.id
self.sort = self.item.sort self.sort = self.item.sort
if self.id: if self.id:
sortvalue = '' anns = self.annotations.order_by('layer', 'sortvalue')
if self.id: anns_by_layer = {}
for l in settings.CONFIG.get('clipLayers', []): for ann in anns:
sortvalue += ''.join(filter(lambda s: s, anns_by_layer.setdefault(ann.layer, []).append(ann)
[a.sortvalue
for a in self.annotations.filter(layer=l).order_by('sortvalue')])) sortvalue = ''.join((
a.sortvalue
for l in settings.CONFIG.get('clipLayers', [])
for a in anns_by_layer.get(l, [])
if a.sortvalue
))
if sortvalue: if sortvalue:
self.sortvalue = sortvalue[:900] self.sortvalue = sortvalue[:900]
else: else:
self.sortvalue = None 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']]: 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) models.Model.save(self, *args, **kwargs)
clip_keys = ('id', 'in', 'out', 'position', 'created', 'modified', clip_keys = ('id', 'in', 'out', 'position', 'created', 'modified',