[pypy-svn] r70880 - pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std

arigo at codespeak.net arigo at codespeak.net
Tue Jan 26 15:11:26 CET 2010


Author: arigo
Date: Tue Jan 26 15:11:25 2010
New Revision: 70880

Modified:
   pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/formatting.py
   pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/tupleobject.py
Log:
Add the jit.unroll_safe_if_const_arg decorator
to "x in (tuple)" and to string formatting.
Unsure if it's really a good idea in general
in string formatting, we will try.


Modified: pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/formatting.py
==============================================================================
--- pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/formatting.py	(original)
+++ pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/formatting.py	Tue Jan 26 15:11:25 2010
@@ -6,6 +6,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.tool.sourcetools import func_with_new_name
 from pypy.rlib.rstring import StringBuilder, UnicodeBuilder
+from pypy.rlib import jit
 
 class BaseStringFormatter(object):
     def __init__(self, space, values_w, w_valuedict):
@@ -142,10 +143,6 @@
 
     class StringFormatter(BaseStringFormatter):
 
-        def __init__(self, space, fmt, values_w, w_valuedict):
-            BaseStringFormatter.__init__(self, space, values_w, w_valuedict)
-            self.fmt = fmt    # either a string or a unicode
-
         def peekchr(self):
             # return the 'current' character
             try:
@@ -258,8 +255,10 @@
                 c = self.peekchr()
             return result
 
-        def format(self):
-            lgt = len(self.fmt) + 4 * len(self.values_w) + 10
+        @jit.unroll_safe_if_const_arg(1)
+        def format(self, fmt):
+            self.fmt = fmt    # either a string or a unicode
+            lgt = len(fmt) + 4 * len(self.values_w) + 10
             if do_unicode:
                 result = UnicodeBuilder(lgt)
             else:
@@ -267,13 +266,9 @@
             self.result = result
             while True:
                 # fast path: consume as many characters as possible
-                fmt = self.fmt
-                i = i0 = self.fmtpos
-                while i < len(fmt):
-                    if fmt[i] == '%':
-                        break
-                    i += 1
-                else:
+                i0 = self.fmtpos
+                i = fmt.find('%', i0)
+                if i < 0:
                     result.append_slice(fmt, i0, len(fmt))
                     break     # end of 'fmt' string
                 result.append_slice(fmt, i0, i)
@@ -479,9 +474,9 @@
     "Entry point"
     if not do_unicode:
         fmt = space.str_w(w_fmt)
-        formatter = StringFormatter(space, fmt, values_w, w_valuedict)
+        formatter = StringFormatter(space, values_w, w_valuedict)
         try:
-            result = formatter.format()
+            result = formatter.format(fmt)
         except NeedUnicodeFormattingError:
             # fall through to the unicode case
             fmt = unicode(fmt)
@@ -489,8 +484,8 @@
             return space.wrap(result)
     else:
         fmt = space.unicode_w(w_fmt)
-    formatter = UnicodeFormatter(space, fmt, values_w, w_valuedict)
-    result = formatter.format()
+    formatter = UnicodeFormatter(space, values_w, w_valuedict)
+    result = formatter.format(fmt)
     return space.wrap(result)
 
 def mod_format(space, w_format, w_values, do_unicode=False):

Modified: pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/tupleobject.py
==============================================================================
--- pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/tupleobject.py	(original)
+++ pypy/branch/unroll-safe-if-const-arg/pypy/objspace/std/tupleobject.py	Tue Jan 26 15:11:25 2010
@@ -4,6 +4,7 @@
 from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
 from pypy.interpreter import gateway
 from pypy.rlib.debug import make_sure_not_resized
+from pypy.rlib import jit
 
 class W_TupleObject(W_Object):
     from pypy.objspace.std.tupletype import tuple_typedef as typedef
@@ -59,6 +60,7 @@
     start, stop = normalize_simple_slice(space, length, w_start, w_stop)
     return W_TupleObject(w_tuple.wrappeditems[start:stop])
 
+ at jit.unroll_safe_if_const_arg(1)
 def contains__Tuple_ANY(space, w_tuple, w_obj):
     for w_item in w_tuple.wrappeditems:
         if space.eq_w(w_item, w_obj):



More information about the Pypy-commit mailing list