# HG changeset patch # User aflett # Date 1207697780 0 # Node ID 0e8b92905741e25f4a68f7837e6e3c4dd7482762 # Parent b33c14a1f5796bdbcc1f77ef3cd0992b058b2a9f a performance breakthrough - bring this branch inline with the bigtable benchmark by lazily creating ctxt._match_set in a way that doesn't barf diff --git a/genshi/template/base.py b/genshi/template/base.py --- a/genshi/template/base.py +++ b/genshi/template/base.py @@ -25,7 +25,6 @@ from genshi.core import Attrs, Stream, StreamEventKind, START, TEXT, _ensure from genshi.input import ParseError -from genshi.template.match import MatchSet __all__ = ['Context', 'Template', 'TemplateError', 'TemplateRuntimeError', 'TemplateSyntaxError', 'BadDirectiveError'] @@ -137,7 +136,7 @@ self.frames = deque([data]) self.pop = self.frames.popleft self.push = self.frames.appendleft - self._match_set = MatchSet() + self._match_set = None self._choice_stack = [] # Helper functions for use in expressions diff --git a/genshi/template/directives.py b/genshi/template/directives.py --- a/genshi/template/directives.py +++ b/genshi/template/directives.py @@ -457,6 +457,10 @@ attach = classmethod(attach) def __call__(self, stream, directives, ctxt, **vars): + # create this lazily because it's much faster to check 'if + # match_set' when match_set is None + if ctxt._match_set is None: + ctxt._match_set = MatchSet() ctxt._match_set.add((self.path.test(ignore_context=True), self.path, list(stream), self.hints, self.namespaces, directives)) diff --git a/genshi/template/markup.py b/genshi/template/markup.py --- a/genshi/template/markup.py +++ b/genshi/template/markup.py @@ -229,9 +229,6 @@ """Internal stream filter that applies any defined match templates to the stream. """ - if match_set is None: - match_set = ctxt._match_set - tail = [] def _strip(stream): depth = 1 @@ -249,6 +246,11 @@ for event in stream: + # we may have discovered a py:match while processing the + # stream.. so keep checking for ctxt._match_set + if match_set is None: + match_set = ctxt._match_set + # We (currently) only care about start and end events for matching # We might care about namespace events in the future, though if not match_set or (event[0] is not START and