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.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',