Commits

Rick Copeland committed b5295ff

Some performance improvements

Comments (0)

Files changed (7)

         return PyLine(self._filename, self._lineno, self._text, self._indent + sz)
 
     def __str__(self):
+        return (' ' * self._indent) + self._text
         if self._lineno:
             return (' ' * self._indent) + self._text + '\t# %s:%d' % (self._filename, self._lineno)
         else:

kajiki/perf/genshi_bench/basic.py

 import timeit
 
 __all__ = ['kajiki', 'mako', 'jinja2', 'genshi', 'genshi_text']
-# __all__ = ['fastpt' ]
+__all__ = ['kajiki' ]
 
 def kajiki(dirname, verbose=False):
     from kajiki import FileLoader
     loader = FileLoader(base=dirname)
     template = loader.load('template.html')
+    print template.py_text
     def render():
         data = dict(title='Just a test', user='joe',
                     items=['Number %d' % num for num in range(1, 15)])

kajiki/perf/genshi_bench/kajiki/base.html

-<html xmlns:py="http://genshi.edgewall.org/">
+<html>
   <head>
     <title>${title}</title>
   </head>

kajiki/perf/genshi_bench/kajiki/lib.html

-<html xmlns:py="http://genshi.edgewall.org/"
-      py:strip="True">
-
+<html>
   <p py:def="greeting(name)">
     Hello, ${name}!
   </p>
-
 </html>

kajiki/template.py

             yield unicode(chunk)
 
     def render(self):
-        return u''.join(self)
+        # return self.__main__()
+        return self.__main__().accumulate_str()
 
     def _extend(self, parent):
         if isinstance(parent, basestring):
         return r
 
     def _escape(self, value):
-        if isinstance(value, flattener):
-            return u''.join(value) # assume flattener results are already escaped
+        if type(value) == flattener:
+            return value
         if hasattr(value, '__html__'):
             return value.__html__()
         else:

kajiki/tests/test_runtime.py

             @kajiki.expose
             def __main__():
                 for i in range(2):
-                    yield i
+                    yield local.__kj__.escape(i)
                     yield ' is '
                     local.__kj__.push_switch(i % 2)
                     if local.__kj__.case(0):
             @kajiki.expose
             def __main__():
                 for i in range(2):
-                    yield i
+                    yield local.__kj__.escape(i)
                     yield ' is '
                     yield evenness(i)
                     yield '\n'
                 @__kj__.flattener.decorate
                 def _kj_lambda(n):
                     yield 'Nevermore '
-                    yield n
+                    yield local.__kj__.escape(n)
                 yield quote(_kj_lambda, 'the raven')
                 del _kj_lambda
         self.tpl = tpl
             @kajiki.expose
             def half_evenness(n):
                 yield ' half of '
-                yield n
+                yield local.__kj__.escape(n)
                 yield ' is '
                 yield evenness(n/2)
         @kajiki.Template
             def __main__():
                 simple_function = lib(dict(globals()))
                 for i in range(4):
-                    yield i
+                    yield local.__kj__.escape(i)
                     yield ' is '
                     yield simple_function.evenness(i)
                     yield simple_function.half_evenness(i)
             return cls(func(*args, **kwargs))
         return inner
 
+    def accumulate_str(self):
+        if type(self.iterator) == flattener:
+            return self.iterator.accumulate_str()
+        s = u''
+        iter_stack = [ self.iterator ]
+        while iter_stack:
+            try:
+                x = iter_stack[-1].next()
+            except StopIteration:
+                iter_stack.pop()
+                continue
+            if type(x) == flattener:
+                iter_stack.append(x.iterator)
+            else:
+                s += x
+        return s
+
     def __iter__(self):
         for x in self.iterator:
-            if isinstance(x, flattener):
+            if type(x) == flattener:
                 for xx in x:
                     yield xx
             else: