Commits

jason kirtland committed 99c9074

Minimize encoding operations in component output.

Comments (0)

Files changed (2)

pegboard/contrib/component/base.py

     def yield_output(self):
         iterable = self.response.iterable
         encoding = self.output_encoding
+        # buffer up a simple sequence of Unicode strings and encode them all
+        # at once.  sub-components are yielded separately.
+        buffer = []
         for item in iterable:
-            if not (item.startswith("<?") and item.endswith("?>")):
-                yield item.encode(encoding)
+            if not (item.startswith(u"<?") and item.endswith(u"?>")):
+                buffer.append(item)
                 continue
             item = item[2:-2]
             instruction, value = item.split()
-            if instruction != "component":
-                yield item
+            if instruction != u"component":
+                buffer.append(item)
                 continue
 
+            if buffer:
+                buffered = u"".join(buffer).encode(encoding)
+                del buffer[:]
+                yield buffered
+
             component = self.resolve_component_name(value)
             if hasattr(component, 'yield_output'):
                 for i in component.yield_output():
                 yield i
             if hasattr(include, 'close'):
                 include.close()
+        if buffer:
+            buffered = u"".join(buffer).encode(encoding)
+            yield buffered
         if hasattr(iterable, 'close'):
             iterable.close()

tests/component/test_genshi.py

         body, _ = self.invoke(Top)
         eq_(body, ['<t>',
                    '<a/>', '<b>', '<c/>', '</b>',
-                   'c|b1|b2|a|t1|t2', '</t>'])
+                   'c|b1|b2|a|t1|t2</t>'])