[pypy-svn] r58240 - in pypy/branch/tuple-nonresizable-395/pypy: module/marshal objspace/std

arigo at codespeak.net arigo at codespeak.net
Fri Sep 19 19:50:07 CEST 2008


Author: arigo
Date: Fri Sep 19 19:50:06 2008
New Revision: 58240

Modified:
   pypy/branch/tuple-nonresizable-395/pypy/module/marshal/interp_marshal.py
   pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py
   pypy/branch/tuple-nonresizable-395/pypy/objspace/std/marshal_impl.py
   pypy/branch/tuple-nonresizable-395/pypy/objspace/std/seqinterface.py
   pypy/branch/tuple-nonresizable-395/pypy/objspace/std/tupleobject.py
Log:
Kill kill code duplication in interp_marshal.py.  This makes marshalling
lists a bit slower, but I don't really care as marshal is really meant
for tuples and other immutable types anyway.

This also removes the only place that used the W_SeqObject interface,
so we'll see if it makes sense to keep it or not...


Modified: pypy/branch/tuple-nonresizable-395/pypy/module/marshal/interp_marshal.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/module/marshal/interp_marshal.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/module/marshal/interp_marshal.py	Fri Sep 19 19:50:06 2008
@@ -234,18 +234,10 @@
             self._overflow()
         self.nesting -= 1
 
-    # this function is inlined below
-    def put_list_w(self, list_w, lng):
-        self.nesting += 1
-        self.put_int(lng)
-        idx = 0
-        while idx < lng:
-            self.put_w_obj(list_w[idx])
-            idx += 1
-        self.nesting -= 1
-
-    def put_list_w(self, lst_w, lng):
+    def put_tuple_w(self, typecode, lst_w):
         self.nesting += 1
+        self.start(typecode)
+        lng = len(lst_w)
         self.put_int(lng)
         idx = 0
         space = self.space
@@ -258,21 +250,6 @@
             self._overflow()
         self.nesting -= 1
 
-    def put_w_seq(self, w_seq):
-        self.nesting += 1
-        lng = w_seq.getlength()
-        self.put_int(lng)
-        idx = 0
-        space = self.space
-        if self.nesting < MAX_MARSHAL_DEPTH:
-            while idx < lng:
-                w_obj = w_seq.getitem(idx)
-                self.space.marshal_w(w_obj, self)
-                idx += 1
-        else:
-            self._overflow()
-        self.nesting -= 1
-
     def _overflow(self):
         self.raise_exc('object too deeply nested to marshal')
 
@@ -458,18 +435,6 @@
         lng = self.get_lng()
         return self.get(lng)
 
-    # this function is inlined below
-    def get_list_w(self):
-        self.nesting += 1
-        lng = self.get_lng()
-        res_w = [None] * lng
-        idx = 0
-        while idx < lng:
-            res_w[idx] = self.get_w_obj(False)
-            idx += 1
-        self.nesting -= 1
-        return res_w
-
     def get_w_obj(self, allow_null):
         self.nesting += 1
         space = self.space
@@ -486,34 +451,31 @@
         return w_ret
 
     # inlined version to save a nesting level
-    def _new_get_list_w():
-        def get_list_w(self):
-            self.nesting += 1
-            lng = self.get_lng()
-            res_w = [None] * lng
-            idx = 0
-            space = self.space
-            w_ret = space.w_None # something not None
-            if self.nesting < MAX_MARSHAL_DEPTH:
-                while idx < lng:
-                    tc = self.get1()
-                    w_ret = self._dispatch[ord(tc)](space, self, tc)
-                    if w_ret is None:
-                        break
-                    res_w[idx] = w_ret
-                    idx += 1
-            else:
-                self._overflow()
-            if w_ret is None:
-                raise OperationError(space.w_TypeError, space.wrap(
-                    'NULL object in marshal data'))
-            self.nesting -= 1
-            return res_w
-        return get_list_w
-
-    get_list_w = _new_get_list_w()
-    # another version not to degenerate resulting list to resizable
-    get_tuple_w = _new_get_list_w()
+    def get_tuple_w(self):
+        self.nesting += 1
+        lng = self.get_lng()
+        res_w = [None] * lng
+        idx = 0
+        space = self.space
+        w_ret = space.w_None # something not None
+        if self.nesting < MAX_MARSHAL_DEPTH:
+            while idx < lng:
+                tc = self.get1()
+                w_ret = self._dispatch[ord(tc)](space, self, tc)
+                if w_ret is None:
+                    break
+                res_w[idx] = w_ret
+                idx += 1
+        else:
+            self._overflow()
+        if w_ret is None:
+            raise OperationError(space.w_TypeError, space.wrap(
+                'NULL object in marshal data'))
+        self.nesting -= 1
+        return res_w
+
+    def get_list_w(self):
+        return self.get_tuple_w()[:]
 
     def _overflow(self):
         self.raise_exc('object too deeply nested to unmarshal')

Modified: pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/objspace/std/listobject.py	Fri Sep 19 19:50:06 2008
@@ -29,7 +29,7 @@
     def getlength(self):
         return len(self.wrappeditems)
 
-    def getitem(self, i):
+    def getitemfast(self, i):
         return self.wrappeditems[i]
 
 

Modified: pypy/branch/tuple-nonresizable-395/pypy/objspace/std/marshal_impl.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/objspace/std/marshal_impl.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/objspace/std/marshal_impl.py	Fri Sep 19 19:50:06 2008
@@ -87,9 +87,7 @@
 put_int(int)                puts an integer
 put_pascal(s)               puts a short string
 put_w_obj(w_obj)            puts a wrapped object
-put_list_w(lst_w, lng)      puts a list with lng of wrapped objects
-put_w_seq(w_seq)            puts a w_seqobject subclass
-
+put_tuple_w(TYPE, tuple_w)  puts tuple_w, an unwrapped list of wrapped objects
 """
 
 handled_by_any = []
@@ -315,8 +313,8 @@
 register(TYPE_STRINGREF, unmarshal_stringref)
 
 def marshal_w__Tuple(space, w_tuple, m):
-    m.start(TYPE_TUPLE)
-    m.put_w_seq(w_tuple)
+    items = w_tuple.wrappeditems
+    m.put_tuple_w(TYPE_TUPLE, items)
 
 def unmarshal_Tuple(space, u, tc):
     items_w = u.get_tuple_w()
@@ -324,8 +322,8 @@
 register(TYPE_TUPLE, unmarshal_Tuple)
 
 def marshal_w__List(space, w_list, m):
-    m.start(TYPE_LIST)
-    m.put_w_seq(w_list)
+    items = w_list.wrappeditems[:]
+    m.put_tuple_w(TYPE_LIST, items)
 
 def unmarshal_List(space, u, tc):
     items_w = u.get_list_w()
@@ -378,8 +376,7 @@
     m.put_int(x.co_stacksize)
     m.put_int(x.co_flags)
     m.atom_str(TYPE_STRING, x.co_code)
-    m.start(TYPE_TUPLE)
-    m.put_list_w(x.co_consts_w[:], len(x.co_consts_w))
+    m.put_tuple_w(TYPE_TUPLE, x.co_consts_w)
     m.atom_strlist(TYPE_TUPLE, TYPE_INTERNED, [space.str_w(w_name) for w_name in x.co_names_w])
     m.atom_strlist(TYPE_TUPLE, TYPE_INTERNED, x.co_varnames)
     m.atom_strlist(TYPE_TUPLE, TYPE_INTERNED, x.co_freevars)
@@ -460,46 +457,38 @@
 register(TYPE_UNICODE, unmarshal_Unicode)
 
 app = gateway.applevel(r'''
-    def set_to_list(theset):
-        return [item for item in theset]
-
-    def list_to_set(datalist, frozen=False):
+    def tuple_to_set(datalist, frozen=False):
         if frozen:
             return frozenset(datalist)
         return set(datalist)
 ''')
 
-set_to_list = app.interphook('set_to_list')
-list_to_set = app.interphook('list_to_set')
+tuple_to_set = app.interphook('tuple_to_set')
 
 # not directly supported:
 def marshal_w_set(space, w_set, m):
-    w_lis = set_to_list(space, w_set)
     # cannot access this list directly, because it's
     # type is not exactly known through applevel.
-    lis_w = space.unpackiterable(w_lis)
-    m.start(TYPE_SET)
-    m.put_list_w(lis_w, len(lis_w))
+    lis_w = space.viewiterable(w_set)
+    m.put_tuple_w(TYPE_SET, lis_w)
 
 handled_by_any.append( ('set', marshal_w_set) )
 
 # not directly supported:
 def marshal_w_frozenset(space, w_frozenset, m):
-    w_lis = set_to_list(space, w_frozenset)
-    lis_w = space.unpackiterable(w_lis)
-    m.start(TYPE_FROZENSET)
-    m.put_list_w(lis_w, len(lis_w))
+    lis_w = space.viewiterable(w_frozenset)
+    m.put_tuple_w(TYPE_FROZENSET, lis_w)
 
 handled_by_any.append( ('frozenset', marshal_w_frozenset) )
 
 def unmarshal_set_frozenset(space, u, tc):
-    items_w = u.get_list_w()
+    items_w = u.get_tuple_w()
     if tc == TYPE_SET:
         w_frozen = space.w_False
     else:
         w_frozen = space.w_True
-    w_lis = space.newlist(items_w)
-    return list_to_set(space, w_lis, w_frozen)
+    w_tup = space.newtuple(items_w)
+    return tuple_to_set(space, w_tup, w_frozen)
 register(TYPE_SET + TYPE_FROZENSET, unmarshal_set_frozenset)
 
 # dispatching for all not directly dispatched types

Modified: pypy/branch/tuple-nonresizable-395/pypy/objspace/std/seqinterface.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/objspace/std/seqinterface.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/objspace/std/seqinterface.py	Fri Sep 19 19:50:06 2008
@@ -11,5 +11,5 @@
     def getlength(self):
         raise NotImplementedError
 
-    def getitem(self, i):
+    def getitemfast(self, i):
         raise NotImplementedError

Modified: pypy/branch/tuple-nonresizable-395/pypy/objspace/std/tupleobject.py
==============================================================================
--- pypy/branch/tuple-nonresizable-395/pypy/objspace/std/tupleobject.py	(original)
+++ pypy/branch/tuple-nonresizable-395/pypy/objspace/std/tupleobject.py	Fri Sep 19 19:50:06 2008
@@ -29,7 +29,7 @@
     def getlength(self):
         return len(self.wrappeditems)
 
-    def getitem(self, i):
+    def getitemfast(self, i):
         return self.wrappeditems[i]
 
 registerimplementation(W_TupleObject)



More information about the Pypy-commit mailing list