[pypy-svn] r13090 - in pypy/dist/pypy: interpreter objspace/flow translator/pickle

tismer at codespeak.net tismer at codespeak.net
Mon Jun 6 05:38:02 CEST 2005


Author: tismer
Date: Mon Jun  6 05:38:00 2005
New Revision: 13090

Modified:
   pypy/dist/pypy/interpreter/gateway.py
   pypy/dist/pypy/objspace/flow/model.py
   pypy/dist/pypy/translator/pickle/genpickle.py
Log:
a few small optimizations get the size of the pickle
from 68 MB down to 46 MB

Modified: pypy/dist/pypy/interpreter/gateway.py
==============================================================================
--- pypy/dist/pypy/interpreter/gateway.py	(original)
+++ pypy/dist/pypy/interpreter/gateway.py	Mon Jun  6 05:38:00 2005
@@ -493,7 +493,8 @@
 #
 
 class ApplevelClass:
-    """A container for app-level source code that should be executed
+    """NOT_RPYTHON
+    A container for app-level source code that should be executed
     as a module in the object space;  interphook() builds a static
     interp-level function that invokes the callable with the given
     name at app-level."""
@@ -548,7 +549,8 @@
 
 
 class ApplevelCache(SpaceCache):
-    """The cache mapping each applevel instance to its lazily built w_dict"""
+    """NOT_RPYTHON
+    The cache mapping each applevel instance to its lazily built w_dict"""
 
     def build(self, app):
         "NOT_RPYTHON.  Called indirectly by Applevel.getwdict()."
@@ -577,6 +579,7 @@
 # __________ geninterplevel version __________
 
 class PyPyCacheDir:
+    "NOT_RPYTHON"
     # similar to applevel, but using translation to interp-level.
     # This version maintains a cache folder with single files.
 

Modified: pypy/dist/pypy/objspace/flow/model.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/model.py	(original)
+++ pypy/dist/pypy/objspace/flow/model.py	Mon Jun  6 05:38:00 2005
@@ -6,8 +6,6 @@
 from __future__ import generators
 from pypy.tool.uid import Hashable
 
-from pypy.translator.pickle.slotted import Slotted
-
 """
     memory size before and after introduction of __slots__
     using targetpypymain with -no-c
@@ -32,7 +30,7 @@
 
 __metaclass__ = type
 
-class FunctionGraph(Slotted):
+class FunctionGraph(object):
     __slots__ = """func source name startblock returnblock exceptblock""".split()
     
     def __init__(self, name, startblock, return_var=None):
@@ -75,7 +73,7 @@
         from pypy.translator.tool.graphpage import SingleGraphPage
         SingleGraphPage(self).display()
 
-class Link(Slotted):
+class Link(object):
 
     __slots__ = """args target exitcase prevblock
                 last_exception last_exc_value""".split()
@@ -116,7 +114,7 @@
     def __repr__(self):
         return "link from %s to %s" % (str(self.prevblock), str(self.target))
 
-class Block(Slotted):
+class Block(object):
     __slots__ = """isstartblock inputargs operations exitswitch
                 exits exc_handler""".split()
     
@@ -186,7 +184,7 @@
         self.exits = exits
 
 
-class Variable(Slotted):
+class Variable(object):
     __slots__ = ["_name", "concretetype"]
 
     countall = 0
@@ -236,12 +234,41 @@
             name = '_' + name
         self._name = name + '_' + self.name[1:]
 
+    def __reduce_ex__(self, *args):
+        if hasattr(self, 'concretetype'):
+            return _buildvar, (self._name, self.concretetype)
+        else:
+            return _buildvar, (self._name,)
+    __reduce__ = __reduce_ex__
+
+def _buildvar(_name, concretetype=None):
+    v = Variable.__new__(Variable, object)
+    v._name = _name
+    if concretetype is not None:
+        v.concretetype = concretetype
+    if type(_name) is int:
+        if _name > Variable.countall:
+            Variable.countall = _name
+        if COUNTOBJECTS:
+            Variable.countcurr += 1
+            Variable.countmax = max(Variable.countmax, Variable.countcurr)
+    return v
 
-class Constant(Hashable, Slotted):
+class Constant(Hashable):
     __slots__ = ["concretetype"]
 
+    def __init__(self, value, concretetype = None):
+        Hashable.__init__(self, value)
+        if concretetype is not None:
+            self.concretetype = concretetype
+    def __reduce_ex__(self, *args):
+        if hasattr(self, 'concretetype'):
+            return Constant, (self.value, self.concretetype)
+        else:
+            return Constant, (self.value,)
+    __reduce__ = __reduce_ex__
 
-class SpaceOperation(Slotted):
+class SpaceOperation(object):
     __slots__ = "opname args result offset".split()
 
     def __init__(self, opname, args, result, offset=-1):
@@ -265,6 +292,9 @@
     def __repr__(self):
         return "%r = %s(%s)" % (self.result, self.opname, ", ".join(map(repr, self.args)))
 
+    def __reduce_ex__(self, *args):
+        return SpaceOperation, (self.opname, self.args, self.result, self.offset)
+
 class Atom:
     def __init__(self, name):
         self.name = name

Modified: pypy/dist/pypy/translator/pickle/genpickle.py
==============================================================================
--- pypy/dist/pypy/translator/pickle/genpickle.py	(original)
+++ pypy/dist/pypy/translator/pickle/genpickle.py	Mon Jun  6 05:38:00 2005
@@ -14,6 +14,7 @@
 from pypy.rpython.rarithmetic import r_int, r_uint
 from pypy.objspace.flow.model import Variable, Constant, SpaceOperation
 from pypy.objspace.flow.model import FunctionGraph, Block, Link
+from pypy.objspace.flow.flowcontext import SpamBlock, EggBlock
 from pypy.interpreter.baseobjspace import ObjSpace
 from pypy.translator.pickle import slotted
 
@@ -64,6 +65,16 @@
             'pypy.module.',
             '__main__',
             )
+        self.shortnames = {
+            SpaceOperation: 'S',
+            Variable:       'V',
+            Constant:       'C',
+            Block:          'B',
+            SpamBlock:      'SB',
+            EggBlock:       'EB',
+            Link:           'L',
+            FunctionGraph:  'F',
+            }
         self.outfile = outfile
         self._partition = 1234
 
@@ -387,13 +398,14 @@
         return name
 
     def nameof_list(self, lis):
-        name = self.uniquename('g%dlist' % len(lis))
+        name = self.uniquename('L%d' % len(lis))
+        extend = self.nameof(_ex)
         def initlist():
             chunk = 20
             for i in range(0, len(lis), chunk):
                 items = lis[i:i+chunk]
                 itemstr = self.nameofargs(items)
-                yield '%s.extend([%s])' % (name, itemstr)
+                yield '%s(%s, %s)' % (extend, name, itemstr)
         self.initcode_python(name, '[]')
         self.later(initlist())
         return name
@@ -420,7 +432,7 @@
                             module, dictname) )
                     self.picklenames[Constant(dic)] = dictname
                     return dictname
-        name = self.uniquename('g%ddict' % len(dic))
+        name = self.uniquename('D%d' % len(dic))
         def initdict():
             for k in dic:
                 try:
@@ -449,7 +461,10 @@
 
     def nameof_instance(self, instance):
         klass = instance.__class__
-        name = self.uniquename('ginst_' + klass.__name__)
+        if klass in self.shortnames:
+            name = self.uniquename(self.shortnames[klass])
+        else:
+            name = self.uniquename('ginst_' + klass.__name__)
         cls = self.nameof(klass)
         if hasattr(klass, '__base__'):
             base_class = builtin_base(instance)
@@ -464,7 +479,9 @@
                 yield '%s.__setstate__(%s)' % (name, args)
                 return
             elif type(restorestate) is tuple:
-                slotted.__setstate__(instance, restorestate)
+                setstate = self.nameof(slotted.__setstate__)
+                args = self.nameof(restorestate)
+                yield '%s(%s, %s)' % (setstate, name, args)
                 return
             assert type(restorestate) is dict, (
                 "%s has no dict and no __setstate__" % name)
@@ -491,10 +508,12 @@
                     ' about __slots__ in %s instance without __setstate__,'
                     ' please update %s' % (cls.__name__, __name__) )
                 restorestate = slotted.__getstate__(instance)
-                restorer = _reconstructor
+                restorer = _rec
                 restoreargs = klass, object, None
             else:
                 restorer = reduced[0]
+                if restorer is _reconstructor:
+                    restorer = _rec
                 restoreargs = reduced[1]
                 if len(reduced) > 2:
                     restorestate = reduced[2]
@@ -702,3 +721,11 @@
             func.func_defaults, (cel,))
     func = new.function(*args)
     return func()
+
+# some shortcuts, to make the pickle smaller
+
+def _ex(lis, *args):
+    lis.extend(args)
+
+def _rec(*args):
+    return _reconstructor(*args)



More information about the Pypy-commit mailing list