Speed up average case by appending remainder of original choices list unchanged
authorTodd Larsen <tlarsen@google.com>
Sat, 04 Oct 2008 04:42:37 +0000
changeset 277 85f7d537e4d7
parent 276 56357a92c110
child 278 b0e2d509b5c3
Speed up average case by appending remainder of original choices list unchanged (avoiding remaining str -> int type conversions) once the insertion point is found. Patch by: Todd Larsen Review by: to-be-reviewed
app/soc/views/helper/lists.py
--- a/app/soc/views/helper/lists.py	Sat Oct 04 04:34:16 2008 +0000
+++ b/app/soc/views/helper/lists.py	Sat Oct 04 04:42:37 2008 +0000
@@ -187,28 +187,30 @@
   """
   # determine where to insert the new limit into choices
   new_choices = []
-  inserted = False
   
-  for pagination, label in choices:
+  for index, (pagination, label) in enumerate(choices):
     items = int(pagination)
 
     if limit == items:
       # limit is already present, so just return existing choices
       return choices
 
-    if (not inserted) and (limit < items):
+    if limit < items:
       # limit needs to be inserted before the current pagination,
       # so assemble a new choice tuple and append it 
       choice = (str(limit), '%s items per page' % limit)
       new_choices.append(choice)
-      inserted = True
       
+      # append the remainder of the original list and exit early
+      # (avoiding unnecessary remaining type conversions, etc.)
+      new_choices.extend(choices[index:])
+      return new_choices
+
     # append the existing choice
     new_choices.append((pagination, label))
 
-  if not inserted:
-    # new choice must go last, past all other existing choices
-    choice = (str(limit), '%s items per page' % limit)
-    new_choices.append(choice)
+  # new choice must go last, past all other existing choices
+  choice = (str(limit), '%s items per page' % limit)
+  new_choices.append(choice)
       
   return new_choices