importer: drop redundant prefix from pkg_present

For the 52 submodules of ansible.modules.system, this produced a 1602
byte pkg_present list. After stripping it becomes 406 bytes, and the
entire LOAD_MODULE size drops from 1988 bytes to 792 bytes (-60%).

For the 68 submodules of ansible.module_utils, 1902 bytes pkg_present
becomes 474 bytes (-75%), and LOAD_MODULE size drops from 2867 bytes to
1439 bytes (-49%).

In a simple test running Ansible's "setup" module followed by its "apt"
module, wire bytes sent drops from 140,357 to 135,531 (-3.4%).
pull/87/head
David Wilson 7 years ago
parent 698996dc27
commit 7af97c0365

@ -352,21 +352,18 @@ Helper Functions
.. currentmodule:: mitogen.master .. currentmodule:: mitogen.master
.. function:: get_child_modules (path, fullname) .. function:: get_child_modules (path)
Return the canonical names of all submodules of a package `module`. Return the suffixes of submodules directly neated beneath of the package
directory at `path`.
:param str path: :param str path:
Path to the module's source code on disk, or some PEP-302-recognized Path to the module's source code on disk, or some PEP-302-recognized
equivalent. Usually this is the module's ``__file__`` attribute, but equivalent. Usually this is the module's ``__file__`` attribute, but
is specified explicitly to avoid loading the module. is specified explicitly to avoid loading the module.
:param str fullname:
The module's canonical name. This is the module's ``__name__``
attribute, but is specified explicitly to avoid loading the module.
:return: :return:
List of canonical submodule names. List of submodule name suffixes.
.. currentmodule:: mitogen.parent .. currentmodule:: mitogen.parent

@ -402,14 +402,14 @@ class Importer(object):
def __init__(self, router, context, core_src, whitelist=(), blacklist=()): def __init__(self, router, context, core_src, whitelist=(), blacklist=()):
self._context = context self._context = context
self._present = {'mitogen': [ self._present = {'mitogen': [
'mitogen.compat', 'compat',
'mitogen.compat.pkgutil', 'compat.pkgutil',
'mitogen.fakessh', 'fakessh',
'mitogen.master', 'master',
'mitogen.parent', 'parent',
'mitogen.ssh', 'ssh',
'mitogen.sudo', 'sudo',
'mitogen.utils', 'utils',
]} ]}
self._lock = threading.Lock() self._lock = threading.Lock()
self.whitelist = whitelist or [''] self.whitelist = whitelist or ['']
@ -444,9 +444,10 @@ class Importer(object):
_tls.running = True _tls.running = True
fullname = fullname.rstrip('.') fullname = fullname.rstrip('.')
try: try:
pkgname, _, _ = fullname.rpartition('.') pkgname, dot, _ = fullname.rpartition('.')
_v and LOG.debug('%r.find_module(%r)', self, fullname) _v and LOG.debug('%r.find_module(%r)', self, fullname)
if fullname not in self._present.get(pkgname, (fullname,)): suffix = fullname[len(pkgname+dot):]
if suffix not in self._present.get(pkgname, (suffix,)):
_v and LOG.debug('%r: master doesn\'t know %r', self, fullname) _v and LOG.debug('%r: master doesn\'t know %r', self, fullname)
return None return None

@ -56,9 +56,9 @@ from mitogen.core import LOG
RLOG = logging.getLogger('mitogen.ctx') RLOG = logging.getLogger('mitogen.ctx')
def get_child_modules(path, fullname): def get_child_modules(path):
it = pkgutil.iter_modules([os.path.dirname(path)]) it = pkgutil.iter_modules([os.path.dirname(path)])
return ['%s.%s' % (fullname, name) for _, name, _ in it] return [name for _, name, _ in it]
def scan_code_imports(co, LOAD_CONST=dis.opname.index('LOAD_CONST'), def scan_code_imports(co, LOAD_CONST=dis.opname.index('LOAD_CONST'),
@ -489,7 +489,7 @@ class ModuleResponder(object):
raise ImportError('could not find %r' % (fullname,)) raise ImportError('could not find %r' % (fullname,))
if is_pkg: if is_pkg:
pkg_present = get_child_modules(path, fullname) pkg_present = get_child_modules(path)
LOG.debug('_build_tuple(%r, %r) -> %r', LOG.debug('_build_tuple(%r, %r) -> %r',
path, fullname, pkg_present) path, fullname, pkg_present)
else: else:

Loading…
Cancel
Save