Skip to content

Commit

Permalink
optimize loop in moveObjectsByDelta
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed May 19, 2020
1 parent fff451d commit ffab7d1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions news/15.bugfix
@@ -0,0 +1,3 @@
Micro-optimization of often called loop in moveObjectsByDelta.
``x in y`` is up to 1000 times faster if y is a set and not a list.
[jensens]
6 changes: 5 additions & 1 deletion src/plone/folder/default.py
Expand Up @@ -81,8 +81,12 @@ def moveObjectsByDelta(
if delta > 0:
subset_ids.reverse()
idx = 0
# micro-optimization 1: set is 1000 time faster on contains than list
subset_ids_as_set = set(subset_ids)
# micro-optimization 2: speedup on lookup in bytecode
order_getitem = order.__getitem__
for i in range(len(order)):
if order[i] not in subset_ids:
if order_getitem(i) not in subset_ids_as_set:
continue
obj_id = subset_ids[idx]
try:
Expand Down
6 changes: 4 additions & 2 deletions src/plone/folder/partial.py
Expand Up @@ -15,7 +15,7 @@

@implementer(IExplicitOrdering)
class PartialOrdering(object):
""" this implementation uses a list ot store order information on a
""" this implementation uses a list to store order information on a
regular attribute of the folderish object; explicit ordering
is supported """
adapts(IOrderableFolder)
Expand Down Expand Up @@ -96,8 +96,10 @@ def moveObjectsByDelta(self, ids, delta, subset_ids=None,
if delta > 0:
subset_ids.reverse()
idx = 0
# micro-optimization: set is 1000 time faster on contains than list
subset_ids_as_set = set(subset_ids)
for i, value in enumerate(self.order):
if value in subset_ids:
if value in subset_ids_as_set:
id = subset_ids[idx]
try:
self.order[i] = id
Expand Down

0 comments on commit ffab7d1

Please sign in to comment.