[Checkins] SVN: z3c.pt/trunk/ Got rid entirely of the _escape function and inlined the actual code instead. We go up again to 12x for path and 19x for Python expressions :)

Hanno Schlichting plone at hannosch.info
Wed Jul 23 04:01:53 EDT 2008


Log message for revision 88747:
  Got rid entirely of the _escape function and inlined the actual code instead. We go up again to 12x for path and 19x for Python expressions :)
  

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/README.txt
  U   z3c.pt/trunk/src/z3c/pt/clauses.py
  U   z3c.pt/trunk/src/z3c/pt/generation.py

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-07-23 07:41:04 UTC (rev 88746)
+++ z3c.pt/trunk/CHANGES.txt	2008-07-23 08:01:52 UTC (rev 88747)
@@ -4,6 +4,10 @@
 Version 0.8.x
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Got rid entirely of the _escape function and inlined the actual code
+  instead. We go up again to 12x for path and 19x for Python expressions :)
+  [hannosch]
+
 - Avoid string concatenation and use multiple write statements instead. These
   are faster now, since we use a list append internally.
   [hannosch]

Modified: z3c.pt/trunk/README.txt
===================================================================
--- z3c.pt/trunk/README.txt	2008-07-23 07:41:04 UTC (rev 88746)
+++ z3c.pt/trunk/README.txt	2008-07-23 08:01:52 UTC (rev 88747)
@@ -5,7 +5,7 @@
 template language including i18n. It also provides a simple text
 template class that allows expression interpolation.
 
-Casual benchmarks pegs it 11x more performant than ``zope.pagetemplate``.
+Casual benchmarks pegs it 12x more performant than ``zope.pagetemplate``.
 
 In a nutshell:
 

Modified: z3c.pt/trunk/src/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/clauses.py	2008-07-23 07:41:04 UTC (rev 88746)
+++ z3c.pt/trunk/src/z3c/pt/clauses.py	2008-07-23 08:01:52 UTC (rev 88747)
@@ -3,7 +3,6 @@
 from cgi import escape
 
 from z3c.pt import types
-from z3c.pt.generation import _escape
 from z3c.pt.utils import unicode_required_flag
 
 class Assign(object):
@@ -427,7 +426,6 @@
 class Tag(object):
     """
       >>> from z3c.pt.generation import CodeIO
-      >>> from z3c.pt.generation import _escape
       >>> from z3c.pt.testing import pyexp
       >>> from StringIO import StringIO
 
@@ -505,23 +503,53 @@
                 stream.write("if isinstance(%s, unicode):" % temp)
                 stream.indent()
                 stream.write("_write(' %s=\"')" % attribute)
-                stream.write("_write(_escape(%s,1,0))" % temp)
-                stream.write("_write('\"')")
+                # Inlined escape function
+                stream.write("_esc = %s" % temp)
+                stream.write("if '&' in _esc:")
+                stream.indent()
+                stream.write("_esc = _esc.replace('&', '&')")
                 stream.outdent()
-                stream.write("elif %s is not None:" % temp)
+                stream.write("if '<' in _esc:")
                 stream.indent()
-                stream.write("_write(' %s=\"')" % attribute)
-                stream.write("_write(_escape(%s,1))" % temp)
+                stream.write("_esc = _esc.replace('<', '&lt;')")
+                stream.outdent()
+                stream.write("if '>' in _esc:")
+                stream.indent()
+                stream.write("_esc = _esc.replace('>', '&gt;')")
+                stream.outdent()
+                stream.write("if '\"' in _esc:")
+                stream.indent()
+                stream.write("_esc = _esc.replace('\"', '&quot;')")
+                stream.outdent()
+                stream.write("_write(_esc)")
                 stream.write("_write('\"')")
                 stream.outdent()
+                stream.write("elif %s is not None:" % temp)
             else:
                 stream.write("if %s is not None:" % temp)
-                stream.indent()
-                stream.write("_write(' %s=\"')" % attribute)
-                stream.write("_write(_escape(%s,1))" % temp)
-                stream.write("_write('\"')")
-                stream.outdent()
-
+            stream.indent()
+            stream.write("_write(' %s=\"')" % attribute)
+            # Inlined escape function
+            stream.write("_esc = str(%s)" % temp)
+            stream.write("if '&' in _esc:")
+            stream.indent()
+            stream.write("_esc = _esc.replace('&', '&amp;')")
+            stream.outdent()
+            stream.write("if '<' in _esc:")
+            stream.indent()
+            stream.write("_esc = _esc.replace('<', '&lt;')")
+            stream.outdent()
+            stream.write("if '>' in _esc:")
+            stream.indent()
+            stream.write("_esc = _esc.replace('>', '&gt;')")
+            stream.outdent()
+            stream.write("if '\"' in _esc:")
+            stream.indent()
+            stream.write("_esc = _esc.replace('\"', '&quot;')")
+            stream.outdent()
+            stream.write("_write(_esc)")
+            stream.write("_write('\"')")
+            stream.outdent()
             assign.end(stream)
 
         stream.restore()
@@ -649,7 +677,6 @@
 class Write(object):
     """
     >>> from z3c.pt.generation import CodeIO
-    >>> from z3c.pt.generation import _escape
     >>> from z3c.pt.testing import pyexp
     >>> from StringIO import StringIO
 

Modified: z3c.pt/trunk/src/z3c/pt/generation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/generation.py	2008-07-23 07:41:04 UTC (rev 88746)
+++ z3c.pt/trunk/src/z3c/pt/generation.py	2008-07-23 08:01:52 UTC (rev 88747)
@@ -18,7 +18,7 @@
 \t(_out, _write) = generation.initialize_stream()
 \t(_attributes, repeat) = generation.initialize_tal()
 \t(_domain, _negotiate, _translate) = generation.initialize_i18n()
-\t(_escape, _marker) = generation.initialize_helpers()
+\t_marker = generation.initialize_helpers()
 \t_path = generation.initialize_traversal()
 
 \t_target_language = _negotiate(_context, target_language)
@@ -45,23 +45,6 @@
         return target_language
     return negotiate(context)
 
-def _escape(s, quote=0, string=1):
-    """Replace special characters '&', '<' and '>' by SGML entities.
-
-    If string is set to False, we are dealing with Unicode input.
-    """
-    if string:
-        s = str(s)
-    if '&' in s:
-        s = s.replace("&", "&amp;") # Must be done first!
-    if '<' in s:
-        s = s.replace("<", "&lt;")
-    if '>' in s:
-        s = s.replace(">", "&gt;")
-    if quote:
-        s = s.replace('"', "&quot;")
-    return s
-
 def initialize_i18n():
     if DISABLE_I18N:
         return (None, _fake_negotiate, _fake_translate)
@@ -71,7 +54,7 @@
     return ({}, utils.repeatdict())
 
 def initialize_helpers():
-    return (_escape, object())
+    return (object(), )
 
 def initialize_stream():
     out = BufferIO()



More information about the Checkins mailing list