[pypy-commit] pypy py3k: Another important speed-up for space initialization: instead of

arigo pypy.commits at gmail.com
Fri Aug 19 14:35:15 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3k
Changeset: r86328:1c4e34c49a4f
Date: 2016-08-19 10:54 +0200
http://bitbucket.org/pypy/pypy/changeset/1c4e34c49a4f/

Log:	Another important speed-up for space initialization: instead of
	compiling importlib/_bootstrap.py every time, marshal the bytecode
	and cache it in rpython/_cache/.

diff --git a/pypy/module/_frozen_importlib/__init__.py b/pypy/module/_frozen_importlib/__init__.py
--- a/pypy/module/_frozen_importlib/__init__.py
+++ b/pypy/module/_frozen_importlib/__init__.py
@@ -20,11 +20,10 @@
         # "from importlib/_boostrap.py import *"
         # It's not a plain "import importlib._boostrap", because we
         # don't want to freeze importlib.__init__.
-        ec = space.getexecutioncontext()
         with open(os.path.join(lib_python, 'importlib', '_bootstrap.py')) as fp:
             source = fp.read()
         pathname = "<frozen importlib._bootstrap>"
-        code_w = ec.compiler.compile(source, pathname, 'exec', 0)
+        code_w = self._cached_compile(source, pathname, 'exec', 0)
         space.setitem(self.w_dict, space.wrap('__name__'), self.w_name)
         space.setitem(self.w_dict, space.wrap('__builtins__'),
                       space.wrap(space.builtin))
@@ -32,6 +31,31 @@
 
         self.w_import = space.wrap(interp_import.import_with_frames_removed)
 
+    def _cached_compile(self, source, *args):
+        from rpython.config.translationoption import CACHE_DIR
+        from pypy.module.marshal import interp_marshal
+
+        space = self.space
+        cachename = os.path.join(CACHE_DIR, 'frozen_importlib_bootstrap')
+        try:
+            if space.config.translating:
+                raise IOError("don't use the cache when translating pypy")
+            with open(cachename, 'rb') as f:
+                previous = f.read(len(source) + 1)
+                if previous != source + '\x00':
+                    raise IOError("source changed")
+                w_bin = space.newbytes(f.read())
+                code_w = interp_marshal.loads(space, w_bin)
+        except IOError:
+            # must (re)compile the source
+            ec = space.getexecutioncontext()
+            code_w = ec.compiler.compile(source, *args)
+            w_bin = interp_marshal.dumps(space, code_w, space.wrap(2))
+            content = source + '\x00' + space.bytes_w(w_bin)
+            with open(cachename, 'wb') as f:
+                f.write(content)
+        return code_w
+
     def startup(self, space):
         """Copy our __import__ to builtins."""
         w_install = self.getdictvalue(space, '_install')


More information about the pypy-commit mailing list