changeset 701:52a597419c0d experimental-match-fastpaths

minor speed cleanups to match_order - use a dict rather than a list, because d[id(k)] is faster than l.index(k)
author aflett
date Fri, 28 Mar 2008 17:05:04 +0000
parents 1240ada13334
children af57b12e3dd2
files genshi/template/match.py
diffstat 1 files changed, 10 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/template/match.py
+++ b/genshi/template/match.py
@@ -49,8 +49,9 @@
 
         if parent is None:
             # merely for indexing. Note that this is shared between
-            # all MatchSets that share the same root parent. We don't have to worry about exclusions here
-            self.match_order = []
+            # all MatchSets that share the same root parent. We don't
+            # have to worry about exclusions here
+            self.match_order = {}
             
             # tag_templates are match templates whose path are simply
             # a tag, like "body" or "img"
@@ -84,7 +85,9 @@
         test, path, template, hints, namespace, directives
         """
 
-        self.match_order.append(match_template)
+        # match_templates are currently tuples that contain unhashable
+        # objects. So we'll use id() for now. 
+        self.match_order[id(match_template)] = len(self.match_order)
         
         path = match_template[1]
 
@@ -118,6 +121,9 @@
         else:
             self.other_templates.remove(match_template)
 
+        # clean up match_order
+        del self.match_order[id(match_template)]
+
     def single_match(cls, match_template):
         """
         Factory for creating a MatchSet with just one match
@@ -165,7 +171,7 @@
                          self.find_raw_matches(event))
 
         # sort the results according to the order they were added
-        return sorted(matches, key=self.match_order.index)
+        return sorted(matches, key=lambda v: self.match_order[id(v)])
 
     def __nonzero__(self):
         """
Copyright (C) 2012-2017 Edgewall Software