[pypy-svn] r26858 - in pypy/dist/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 6 13:18:39 CEST 2006


Author: arigo
Date: Sat May  6 13:18:37 2006
New Revision: 26858

Added:
   pypy/dist/pypy/objspace/std/smallintobject.py
      - copied, changed from r26843, pypy/dist/pypy/objspace/std/intobject.py
   pypy/dist/pypy/objspace/std/test/test_smallintobject.py
      - copied, changed from r26843, pypy/dist/pypy/objspace/std/test/test_intobject.py
Modified:
   pypy/dist/pypy/objspace/std/boolobject.py
   pypy/dist/pypy/objspace/std/inttype.py
   pypy/dist/pypy/objspace/std/model.py
Log:
Added "small integers" as a tagged, odd-valued pointer.  Disabled by
default, see WITHSMALLINT.  Gives a small slow-down in pypy-c.  Some
more optimizations could be done in rtagged.py, though.



Modified: pypy/dist/pypy/objspace/std/boolobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/boolobject.py	(original)
+++ pypy/dist/pypy/objspace/std/boolobject.py	Sat May  6 13:18:37 2006
@@ -1,5 +1,5 @@
 from pypy.objspace.std.objspace import *
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import W_IntObject
 
 
 class W_BoolObject(W_Object):
@@ -22,8 +22,12 @@
 
 # bool-to-int delegation requires translating the .boolvar attribute
 # to an .intval one
-def delegate_Bool2Int(space, w_bool):
-    return wrapint(int(w_bool.boolval))
+def delegate_Bool2IntObject(space, w_bool):
+    return W_IntObject(int(w_bool.boolval))
+
+def delegate_Bool2SmallInt(space, w_bool):
+    from pypy.objspace.std.smallintobject import W_SmallIntObject
+    return W_SmallIntObject(int(w_bool.boolval))   # cannot overflow
 
 
 def nonzero__Bool(space, w_bool):

Modified: pypy/dist/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/dist/pypy/objspace/std/inttype.py	(original)
+++ pypy/dist/pypy/objspace/std/inttype.py	Sat May  6 13:18:37 2006
@@ -3,9 +3,23 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.gateway import NoneNotWrapped
 
-def wrapint(x):
-    from pypy.objspace.std.intobject import W_IntObject
-    return W_IntObject(x)
+# ____________________________________________________________
+
+from pypy.objspace.std.model import WITHSMALLINT
+if WITHSMALLINT:
+    def wrapint(x):
+        from pypy.objspace.std.smallintobject import W_SmallIntObject
+        try:
+            return W_SmallIntObject(x)
+        except OverflowError:
+            from pypy.objspace.std.intobject import W_IntObject
+            return W_IntObject(x)
+else:
+    def wrapint(x):
+        from pypy.objspace.std.intobject import W_IntObject
+        return W_IntObject(x)
+
+# ____________________________________________________________
 
 def retry_to_w_long(space, parser, base=0):
     parser.rewind()
@@ -85,6 +99,9 @@
                                  space.wrap(
                 "long int too large to convert to int"))          
         return w_longval
+    elif space.is_w(w_inttype, space.w_int):
+        # common case
+        return wrapint(value)
     else:
         w_obj = space.allocate_instance(W_IntObject, w_inttype)
         W_IntObject.__init__(w_obj, value)

Modified: pypy/dist/pypy/objspace/std/model.py
==============================================================================
--- pypy/dist/pypy/objspace/std/model.py	(original)
+++ pypy/dist/pypy/objspace/std/model.py	Sat May  6 13:18:37 2006
@@ -9,6 +9,7 @@
 import pypy.interpreter.special
 
 WITHSET = False
+WITHSMALLINT = False
 
 class StdTypeModel:
 
@@ -49,6 +50,8 @@
         from pypy.objspace.std import complexobject
         if WITHSET:
             from pypy.objspace.std import setobject
+        if WITHSMALLINT:
+            from pypy.objspace.std import smallintobject
         from pypy.objspace.std import tupleobject
         from pypy.objspace.std import listobject
         from pypy.objspace.std import dictobject
@@ -92,6 +95,8 @@
             self.typeorder[setobject.W_SetObject] = []
             self.typeorder[setobject.W_FrozensetObject] = []
             self.typeorder[setobject.W_SetIterObject] = []
+        if WITHSMALLINT:
+            self.typeorder[smallintobject.W_SmallIntObject] = []
         for type in self.typeorder:
             self.typeorder[type].append((type, None))
 
@@ -104,8 +109,19 @@
         # register the order in which types are converted into each others
         # when trying to dispatch multimethods.
         # XXX build these lists a bit more automatically later
+        if WITHSMALLINT:
+            self.typeorder[boolobject.W_BoolObject] += [
+                (smallintobject.W_SmallIntObject, boolobject.delegate_Bool2SmallInt),
+                ]
+            self.typeorder[smallintobject.W_SmallIntObject] += [
+                (intobject.W_IntObject, smallintobject.delegate_SmallInt2Int),
+                (longobject.W_LongObject, smallintobject.delegate_SmallInt2Long),
+                (floatobject.W_FloatObject, smallintobject.delegate_SmallInt2Float),
+                (complexobject.W_ComplexObject, smallintobject.delegate_SmallInt2Complex),
+                ]
+
         self.typeorder[boolobject.W_BoolObject] += [
-            (intobject.W_IntObject,     boolobject.delegate_Bool2Int),
+            (intobject.W_IntObject,     boolobject.delegate_Bool2IntObject),
             (longobject.W_LongObject,   longobject.delegate_Bool2Long),
             (floatobject.W_FloatObject, floatobject.delegate_Bool2Float),
             (complexobject.W_ComplexObject, complexobject.delegate_Bool2Complex),



More information about the Pypy-commit mailing list