changeset 306:095a754f95a8 trunk

Minor optimization for XPath evaluation.
author cmlenz
date Mon, 16 Oct 2006 16:41:06 +0000
parents 60111a041e7c
children 8d07e6fe17a4
files genshi/path.py genshi/template.py
diffstat 2 files changed, 9 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/path.py
+++ b/genshi/path.py
@@ -129,7 +129,7 @@
                         elif subevent[0] is END:
                             depth -= 1
                         yield subevent
-                        test(subevent, namespaces, variables)
+                        test(subevent, namespaces, variables, updateonly=True)
                 elif result:
                     yield result
         return Stream(_generate())
@@ -141,7 +141,10 @@
         The function returned expects the positional arguments `event`,
         `namespaces` and `variables`. The first is a stream event, while the
         latter two are a mapping of namespace prefixes to URIs, and a mapping
-        of variable names to values, respectively.
+        of variable names to values, respectively. In addition, the function
+        accepts an `updateonly` keyword argument that default to `False`. If
+        it is set to `True`, the function only updates its internal state,
+        but does not perform any tests or return a result.
         
         If the path matches the event, the function returns the match (for
         example, a `START` or `TEXT` event.) Otherwise, it returns `None`.
@@ -156,11 +159,10 @@
         """
         paths = [(p, len(p), [0], [], [0] * len(p)) for p in self.paths]
 
-        def _test(event, namespaces, variables):
+        def _test(event, namespaces, variables, updateonly=False):
             kind, data, pos = event
             retval = None
             for steps, size, cursors, cutoff, counter in paths:
-
                 # Manage the stack that tells us "where we are" in the stream
                 if kind is END:
                     if cursors:
@@ -169,7 +171,7 @@
                 elif kind is START:
                     cursors.append(cursors and cursors[-1] or 0)
 
-                if retval or not cursors:
+                if updateonly or retval or not cursors:
                     continue
                 cursor = cursors[-1]
                 depth = len(cursors)
--- a/genshi/template.py
+++ b/genshi/template.py
@@ -1129,7 +1129,7 @@
                     # Let the remaining match templates know about the event so
                     # they get a chance to update their internal state
                     for test in [mt[0] for mt in match_templates[idx + 1:]]:
-                        test(event, namespaces, ctxt)
+                        test(event, namespaces, ctxt, updateonly=True)
 
                     # Consume and store all events until an end event
                     # corresponding to this start event is encountered
@@ -1140,7 +1140,7 @@
                     content = list(content)
 
                     for test in [mt[0] for mt in match_templates]:
-                        test(tail[0], namespaces, ctxt)
+                        test(tail[0], namespaces, ctxt, updateonly=True)
 
                     # Make the select() function available in the body of the
                     # match template
Copyright (C) 2012-2017 Edgewall Software