[Checkins] SVN: z3c.pt/trunk/ Use a simple list based BufferIO class instead of a cStringIO for the out stream. Avoiding the need to encode Unicode data is a bigger win. We do not support arbitrarily mixing of Unicode and non-ascii inside the engine. We are back at a 11.5x times faster factor.

Hanno Schlichting plone at hannosch.info
Sun Jun 15 14:08:34 EDT 2008


Log message for revision 87412:
  Use a simple list based BufferIO class instead of a cStringIO for the out stream. Avoiding the need to encode Unicode data is a bigger win. We do not support arbitrarily mixing of Unicode and non-ascii inside the engine. We are back at a 11.5x times faster factor.
  

Changed:
  U   z3c.pt/trunk/docs/HISTORY.txt
  U   z3c.pt/trunk/src/z3c/pt/clauses.py
  U   z3c.pt/trunk/src/z3c/pt/generation.py
  U   z3c.pt/trunk/src/z3c/pt/translation.txt

-=-
Modified: z3c.pt/trunk/docs/HISTORY.txt
===================================================================
--- z3c.pt/trunk/docs/HISTORY.txt	2008-06-15 15:27:07 UTC (rev 87411)
+++ z3c.pt/trunk/docs/HISTORY.txt	2008-06-15 18:08:30 UTC (rev 87412)
@@ -4,6 +4,10 @@
 Version 0.8.x
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Use a simple list based BufferIO class instead of a cStringIO for the out
+  stream. Avoiding the need to encode Unicode data is a bigger win. We do
+  not support arbitrarily mixing of Unicode and non-ascii inside the engine.
+
 - Merged two adjacent writes into one inside the Tag clause.
 
 - Applied a bunch of micro-optimizations. ''.join({}) is slightly faster

Modified: z3c.pt/trunk/src/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/clauses.py	2008-06-15 15:27:07 UTC (rev 87411)
+++ z3c.pt/trunk/src/z3c/pt/clauses.py	2008-06-15 18:08:30 UTC (rev 87412)
@@ -490,7 +490,7 @@
                 stream.write("if isinstance(%s, unicode):" % temp)
                 stream.indent()
                 stream.write("_write(' %s=\"'+_escape("
-                             "%s.encode('utf-8'), \"\\\"\")+'\"')" %
+                             "%s, \"\\\"\")+'\"')" %
                              (attribute, temp))
                 stream.outdent()
                 stream.write("elif %s is not None:" % temp)
@@ -630,7 +630,7 @@
     >>> write.begin(stream)
     >>> write.end(stream)
     >>> exec stream.getvalue()
-    >>> _out.getvalue() == 'La Pe\xc3\xb1a'
+    >>> _out.getvalue() == unicode('La Pe\xc3\xb1a', 'utf-8')
     True
     """
 
@@ -658,7 +658,7 @@
         if unicode_required_flag:
             stream.write("if isinstance(_urf, unicode):")
             stream.indent()
-            stream.write("_write(_urf.encode('utf-8'))")
+            stream.write("_write(_urf)")
             stream.outdent()
             stream.write("elif _urf is not None:")
             stream.indent()
@@ -703,7 +703,7 @@
     >>> write.begin(stream)
     >>> write.end(stream)
     >>> exec stream.getvalue()
-    >>> _out.getvalue() == 'La Pe\xc3\xb1a'
+    >>> _out.getvalue() == unicode('La Pe\xc3\xb1a', 'utf-8')
     True
 
     Invalid:
@@ -726,7 +726,7 @@
             self.assign.begin(stream, temp)
             expr = temp
 
-        stream.write("_write(%s.encode('utf-8'))" % expr)
+        stream.write("_write(%s)" % expr)
 
 class Out(object):
     """

Modified: z3c.pt/trunk/src/z3c/pt/generation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/generation.py	2008-06-15 15:27:07 UTC (rev 87411)
+++ z3c.pt/trunk/src/z3c/pt/generation.py	2008-06-15 18:08:30 UTC (rev 87412)
@@ -2,7 +2,6 @@
 
 import cgi
 import StringIO
-import cStringIO
 
 import expressions
 import utils
@@ -21,7 +20,7 @@
 
 \t_target_language = target_language
 %s
-\treturn _out.getvalue().decode('utf-8')
+\treturn _out.getvalue()
 """
 
 def initialize_i18n():
@@ -34,7 +33,7 @@
     return (cgi.escape, object())
 
 def initialize_stream():
-    out = cStringIO.StringIO()
+    out = BufferIO()
     return (out, out.write)
 
 def initialize_traversal():
@@ -56,7 +55,13 @@
         code = self.stream.getvalue()
 
         return wrapper % (args, code), {'generation': z3c.pt.generation}
-        
+
+class BufferIO(list):
+    write = list.append
+
+    def getvalue(self):
+        return ''.join(self)
+
 class CodeIO(StringIO.StringIO):
     """A I/O stream class that provides facilities to generate Python code.
 

Modified: z3c.pt/trunk/src/z3c/pt/translation.txt
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.txt	2008-06-15 15:27:07 UTC (rev 87411)
+++ z3c.pt/trunk/src/z3c/pt/translation.txt	2008-06-15 18:08:30 UTC (rev 87412)
@@ -188,8 +188,8 @@
   ...   <img alt="${'La Peña'}" />
   ...   <img alt="Hello ${'La Peña'}" />
   ...   <img alt="La Peña, oh ${'La Peña'}" />
-  ...   ${unicode('La Pe\xc3\xb1a', 'utf-8')}
-  ...   <img alt="${unicode('La Pe\xc3\xb1a', 'utf-8')}" />
+  ...   ${unicode('La Pe\xc3\xb1a', 'utf-8').encode('utf-8')}
+  ...   <img alt="${unicode('La Pe\xc3\xb1a', 'utf-8').encode('utf-8')}" />
   ...   <img alt="Hello ${unicode('La Pe\xc3\xb1a', 'utf-8').encode('utf-8')}!" />
   ... </div>""", translate_xml)
     <div>



More information about the Checkins mailing list