changeset 37843:670eb4fa1b86

demandimport: make module ignores a set (API) 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 Differential Revision: https://phab.mercurial-scm.org/D3440
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 05 May 2018 18:41:51 -0700
parents 853001c84114
children 8fb9985382be
files hgdemandimport/__init__.py hgdemandimport/demandimportpy2.py hgdemandimport/demandimportpy3.py hgext/convert/bzr.py hgext/highlight/highlight.py
diffstat 5 files changed, 20 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/hgdemandimport/__init__.py	Thu Apr 19 20:33:43 2018 +0900
+++ b/hgdemandimport/__init__.py	Sat May 05 18:41:51 2018 -0700
@@ -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
--- a/hgdemandimport/demandimportpy2.py	Thu Apr 19 20:33:43 2018 +0900
+++ b/hgdemandimport/demandimportpy2.py	Sat May 05 18:41:51 2018 -0700
@@ -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
--- a/hgdemandimport/demandimportpy3.py	Thu Apr 19 20:33:43 2018 +0900
+++ b/hgdemandimport/demandimportpy3.py	Sat May 05 18:41:51 2018 -0700
@@ -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
--- a/hgext/convert/bzr.py	Thu Apr 19 20:33:43 2018 +0900
+++ b/hgext/convert/bzr.py	Sat May 05 18:41:51 2018 -0700
@@ -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',
--- a/hgext/highlight/highlight.py	Thu Apr 19 20:33:43 2018 +0900
+++ b/hgext/highlight/highlight.py	Sat May 05 18:41:51 2018 -0700
@@ -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,