[pypy-svn] pypy default: Restore the fast path for imports: only read sys.modules when the module

amauryfa commits-noreply at bitbucket.org
Tue Apr 5 11:38:51 CEST 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r43164:11faf77ff2b1
Date: 2011-04-05 11:33 +0200
http://bitbucket.org/pypy/pypy/changeset/11faf77ff2b1/

Log:	Restore the fast path for imports: only read sys.modules when the
	module has already been imported, and do not acquire the import
	lock.

diff --git a/pypy/module/thread/test/test_import_lock.py b/pypy/module/thread/test/test_import_lock.py
--- a/pypy/module/thread/test/test_import_lock.py
+++ b/pypy/module/thread/test/test_import_lock.py
@@ -61,3 +61,27 @@
         assert not imp.lock_held()
         self.waitfor(lambda: done)
         assert done
+
+class TestImportLock:
+    def test_lock(self, space, monkeypatch):
+        from pypy.module.imp.importing import getimportlock, importhook
+
+        # Monkeypatch the import lock and add a counter
+        importlock = getimportlock(space)
+        original_acquire = importlock.acquire_lock
+        def acquire_lock():
+            importlock.count += 1
+            original_acquire()
+        importlock.count = 0
+        monkeypatch.setattr(importlock, 'acquire_lock', acquire_lock)
+
+        # An already imported module
+        importhook(space, 'sys')
+        assert importlock.count == 0
+        # A new module
+        importhook(space, 're')
+        assert importlock.count == 7
+        # Import it again
+        previous_count = importlock.count
+        importhook(space, 're')
+        assert importlock.count == previous_count

diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -257,16 +257,7 @@
                 space.timer.stop_name("importhook", modulename)
                 return w_mod
 
-        ## if level > 0:
-        ##     msg = "Attempted relative import in non-package"
-        ##     raise OperationError(space.w_ValueError, w(msg))
-
-        ## if not modulename:
-        ##     return None
-
-    w_mod = absolute_import_try(space, modulename, 0, fromlist_w)
-    if w_mod is None or space.is_w(w_mod, space.w_None):
-        w_mod = absolute_import(space, modulename, 0, fromlist_w, tentative=0)
+    w_mod = absolute_import(space, modulename, 0, fromlist_w, tentative=0)
     if rel_modulename is not None:
         space.setitem(space.sys.get('modules'), w(rel_modulename), space.w_None)
     space.timer.stop_name("importhook", modulename)
@@ -274,6 +265,11 @@
 
 @jit.dont_look_inside
 def absolute_import(space, modulename, baselevel, fromlist_w, tentative):
+    # Short path: check in sys.modules
+    w_mod = absolute_import_try(space, modulename, baselevel, fromlist_w)
+    if w_mod is not None and not space.is_w(w_mod, space.w_None):
+        return w_mod
+
     lock = getimportlock(space)
     lock.acquire_lock()
     try:


More information about the Pypy-commit mailing list