britney.py: Minor optimisation to sort_actions

Avoid some cases of O(n^2) behaviour in sort_actions and reduce the
size of n for the remaining O(n^2)-ish behaviour by filtering out
removals early on.

Signed-off-by: Niels Thykier <niels@thykier.net>
debian
Niels Thykier 11 years ago
parent d5cfd5aebd
commit bf3aa08023

@ -2745,29 +2745,39 @@ class Britney(object):
so the ones with most reverse dependencies are at the end of the loop. so the ones with most reverse dependencies are at the end of the loop.
If an action depends on another one, it is put after it. If an action depends on another one, it is put after it.
""" """
upgrade_me = [x.name for x in self.excuses if x.name in [y.uvname for y in self.upgrade_me]] uvnames = frozenset(y.uvname for y in self.upgrade_me)
for e in self.excuses: excuses = [e for e in self.excuses if e.name in uvnames]
if e.name not in upgrade_me: continue removals = []
# try removes at the end of the loop upgrade_me = []
elif e.name[0] == '-':
upgrade_me.remove(e.name) for e in excuses:
upgrade_me.append(e.name) # We order removals and regular migrations differently, so
# otherwise, put it in a good position checking its dependencies # split them out early.
if e.name[0] == '-':
removals.append(e.name)
else: else:
upgrade_me.append(e.name)
for e in excuses:
# put the item (regular migration) in a good position
# checking its dependencies
pos = [] pos = []
udeps = [upgrade_me.index(x) for x in e.deps if x in upgrade_me and x != e.name] udeps = [upgrade_me.index(x) for x in e.deps if x in upgrade_me and x != e.name]
if len(udeps) > 0: if udeps:
pos.append(max(udeps)) pos.append(max(udeps))
sdeps = [upgrade_me.index(x) for x in e.sane_deps if x in upgrade_me and x != e.name] sdeps = [upgrade_me.index(x) for x in e.sane_deps if x in upgrade_me and x != e.name]
if len(sdeps) > 0: if sdeps:
pos.append(min(sdeps)) pos.append(min(sdeps))
if len(pos) == 0: continue if not pos:
continue
upgrade_me.remove(e.name) upgrade_me.remove(e.name)
upgrade_me.insert(max(pos)+1, e.name) upgrade_me.insert(max(pos)+1, e.name)
self.dependencies[e.name] = e.deps self.dependencies[e.name] = e.deps
# replace the list of actions with the new one # replace the list of actions with the new one
self.upgrade_me = [ make_migrationitem(x, self.sources) for x in upgrade_me ] self.upgrade_me = [ make_migrationitem(x, self.sources) for x in upgrade_me ]
self.upgrade_me.extend(make_migrationitem(x, self.sources) for x in removals)
def auto_hinter(self): def auto_hinter(self):
"""Auto-generate "easy" hints. """Auto-generate "easy" hints.

Loading…
Cancel
Save