# HG changeset patch # User cmlenz # Date 1188996304 0 # Node ID 2910e2532a60c2aa761d1d4690433540346cda1c # Parent f779e566884d9b5f7aea47743fb45b9485cdc3d7 Performance optimization for the `genshi.core._ensure` function: instead of checking whether we're dealing with a markup event stream for every item in the iterable, we now check only the first item, and treat the rest of the iterable depending on whether the first one looks like an event. diff --git a/genshi/core.py b/genshi/core.py --- a/genshi/core.py +++ b/genshi/core.py @@ -13,6 +13,7 @@ """Core classes for markup processing.""" +from itertools import chain import operator from genshi.util import plaintext, stripentities, striptags @@ -252,12 +253,24 @@ def _ensure(stream): """Ensure that every item on the stream is actually a markup event.""" - for event in stream: - if type(event) is not tuple: + stream = iter(stream) + event = stream.next() + + # Check whether the iterable is a real markup event stream by examining the + # first item it yields; if it's not we'll need to do some conversion + if type(event) is not tuple or len(event) != 3: + for event in chain([event], stream): if hasattr(event, 'totuple'): event = event.totuple() else: event = TEXT, unicode(event), (None, -1, -1) + yield event + return + + # This looks like a markup event stream, so we'll just pass it through + # unchanged + yield event + for event in stream: yield event