D3440: demandimport: make module ignores a set (API)

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Sun May 6 01:58:31 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The list of modules to ignore is used for membership testing. Yet
  it is defined as a list.
  
  Sets are more efficient for membership testing. So this commit converts
  the module list to a set.
  
  Since we took an API hit, I renamed the variable to further clarify
  the change.
  
  This appears to reduce the CPU time for running 300 invocations of
  `hg log -r. -T '{rev}'` on my i7-6700K:
  
  before: 18.64s
  after:  18.44s

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3440

AFFECTED FILES
  hgdemandimport/__init__.py
  hgdemandimport/demandimportpy2.py
  hgdemandimport/demandimportpy3.py
  hgext/convert/bzr.py
  hgext/highlight/highlight.py

CHANGE DETAILS

diff --git a/hgext/highlight/highlight.py b/hgext/highlight/highlight.py
--- a/hgext/highlight/highlight.py
+++ b/hgext/highlight/highlight.py
@@ -11,7 +11,7 @@
 from __future__ import absolute_import
 
 from mercurial import demandimport
-demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__'])
+demandimport.IGNORES.update(['pkgutil', 'pkg_resources', '__main__'])
 
 from mercurial import (
     encoding,
diff --git a/hgext/convert/bzr.py b/hgext/convert/bzr.py
--- a/hgext/convert/bzr.py
+++ b/hgext/convert/bzr.py
@@ -19,7 +19,7 @@
 from . import common
 
 # these do not work with demandimport, blacklist
-demandimport.ignore.extend([
+demandimport.IGNORES.update([
         'bzrlib.transactions',
         'bzrlib.urlutils',
         'ElementPath',
diff --git a/hgdemandimport/demandimportpy3.py b/hgdemandimport/demandimportpy3.py
--- a/hgdemandimport/demandimportpy3.py
+++ b/hgdemandimport/demandimportpy3.py
@@ -40,7 +40,7 @@
     """
     def exec_module(self, module):
         """Make the module load lazily."""
-        if _deactivated or module.__name__ in ignore:
+        if _deactivated or module.__name__ in ignores:
             self.loader.exec_module(module)
         else:
             super().exec_module(module)
@@ -62,11 +62,11 @@
         (_bytecode_loader, importlib.machinery.BYTECODE_SUFFIXES),
     )
 
-ignore = []
+ignores = set()
 
-def init(ignorelist):
-    global ignore
-    ignore = ignorelist
+def init(ignoreset):
+    global ignores
+    ignores = ignoreset
 
 def isenabled():
     return _makefinder in sys.path_hooks and not _deactivated
diff --git a/hgdemandimport/demandimportpy2.py b/hgdemandimport/demandimportpy2.py
--- a/hgdemandimport/demandimportpy2.py
+++ b/hgdemandimport/demandimportpy2.py
@@ -162,7 +162,7 @@
 _pypy = '__pypy__' in sys.builtin_module_names
 
 def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
-    if locals is None or name in ignore or fromlist == ('*',):
+    if locals is None or name in ignores or fromlist == ('*',):
         # these cases we can't really delay
         return _hgextimport(_origimport, name, globals, locals, fromlist, level)
     elif not fromlist:
@@ -209,7 +209,7 @@
                     # while processing the import statement.
                     return
                 mn = '%s.%s' % (mod.__name__, attr)
-                if mn in ignore:
+                if mn in ignores:
                     importfunc = _origimport
                 else:
                     importfunc = _demandmod
@@ -273,11 +273,11 @@
 
         return mod
 
-ignore = []
+ignores = set()
 
-def init(ignorelist):
-    global ignore
-    ignore = ignorelist
+def init(ignoreset):
+    global ignores
+    ignores = ignoreset
 
 def isenabled():
     return builtins.__import__ == _demandimport
diff --git a/hgdemandimport/__init__.py b/hgdemandimport/__init__.py
--- a/hgdemandimport/__init__.py
+++ b/hgdemandimport/__init__.py
@@ -21,8 +21,9 @@
 else:
     from . import demandimportpy2 as demandimport
 
-# Extensions can add to this list if necessary.
-ignore = [
+# Full module names which can't be lazy imported.
+# Extensions can add to this set.
+IGNORES = {
     '__future__',
     '_hashlib',
     # ImportError during pkg_resources/__init__.py:fixup_namespace_package
@@ -55,17 +56,15 @@
     '__builtin__',
     'builtins',
     'urwid.command_map', # for pudb
-    ]
+}
 
 _pypy = '__pypy__' in sys.builtin_module_names
 
 if _pypy:
-    ignore.extend([
-        # _ctypes.pointer is shadowed by "from ... import pointer" (PyPy 5)
-        '_ctypes.pointer',
-    ])
+    # _ctypes.pointer is shadowed by "from ... import pointer" (PyPy 5)
+    IGNORES.add('_ctypes.pointer')
 
-demandimport.init(ignore)
+demandimport.init(IGNORES)
 
 # Re-export.
 isenabled = demandimport.isenabled



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list