hgdemandimport/__init__.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 09 Apr 2024 22:36:35 +0200
changeset 51594 e3a5ec2d236a
parent 50728 80c8dcfb73c6
permissions -rw-r--r--
outgoing: rework the handling of the `missingroots` case to be faster The previous implementation was slow, to the point it was taking a significant amount of `hg bundle --type none-streamv2` call. We rework the code to compute the same value much faster, making the operation disappear from the `hg bundle --type none-streamv2` profile. Someone would remark that producing a streamclone does not requires an `outgoing` object. However that is a matter for another day. There is other user of `missingroots` (non stream `hg bundle` call for example), and they will also benefit from this rework. We implement an old TODO in the process, directly computing the missing and common attribute as we have most element at hand already. ### benchmark.name = hg.command.bundle # bin-env-vars.hg.flavor = default # bin-env-vars.hg.py-re2-module = default # benchmark.variants.revs = all # benchmark.variants.type = none-streamv2 ## data-env-vars.name = heptapod-public-2024-03-25-zstd-sparse-revlog before: 7.750458 after: 6.665565 (-14.00%, -1.08) ## data-env-vars.name = mercurial-public-2024-03-22-zstd-sparse-revlog before: 0.700229 after: 0.496050 (-29.16%, -0.20) ## data-env-vars.name = mozilla-try-2023-03-22-zstd-sparse-revlog before: 346.508952 after: 316.749699 (-8.59%, -29.76) ## data-env-vars.name = pypy-2024-03-22-zstd-sparse-revlog before: 3.401700 after: 2.915810 (-14.28%, -0.49) ## data-env-vars.name = tryton-public-2024-03-22-zstd-sparse-revlog before: 1.870798 after: 1.461583 (-21.87%, -0.41) note: this whole `missingroots` of outgoing has a limited number of callers and could likely be replace by something simpler (like taking an explicit "missing_revs" set for example). However this is a wider change and we focus on a small impact, quick rework that does not change the API for now.

# hgdemandimport - global demand-loading of modules for Mercurial
#
# Copyright 2017 Facebook Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

'''demandimport - automatic demand-loading of modules'''

# This is in a separate package from mercurial because in Python 3,
# demand loading is per-package. Keeping demandimport in the mercurial package
# would disable demand loading for any modules in mercurial.


import os
import sys

from . import demandimportpy3 as demandimport

# 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
    '_imp',
    '_xmlplus',
    'fcntl',
    'nt',  # pathlib2 tests the existence of built-in 'nt' module
    'win32com.gen_py',
    'win32com.shell',  # 'appdirs' tries to import win32com.shell
    '_winreg',  # 2.7 mimetypes needs immediate ImportError
    'pythoncom',
    # imported by tarfile, not available under Windows
    'pwd',
    'grp',
    # imported by profile, itself imported by hotshot.stats,
    # not available under Windows
    'resource',
    # this trips up many extension authors
    'gtk',
    # setuptools' pkg_resources.py expects "from __main__ import x" to
    # raise ImportError if x not defined
    '__main__',
    '_ast',  # https://bugs.python.org/issue41631
    '_ssl',  # conditional imports in the stdlib, issue1964
    '_sre',  # issue4920
    'rfc822',
    'mimetools',
    'sqlalchemy.events',  # has import-time side effects (issue5085)
    'sqlalchemy.dialects',  # similar problems as above
    # setuptools 8 expects this module to explode early when not on windows
    'distutils.msvc9compiler',
    '__builtin__',
    'builtins',
    'urwid.command_map',  # for pudb
    'lzma',
    # setuptools uses this hack to inject it's own distutils at import time
    'setuptools',
    '_distutils_hack.override',
}

_pypy = '__pypy__' in sys.builtin_module_names

if _pypy:
    # _ctypes.pointer is shadowed by "from ... import pointer" (PyPy 5)
    IGNORES.add('_ctypes.pointer')
    # pure Python module on PyPy, must be loaded to raise ModuleNotFoundError
    # on non-Windows platforms
    IGNORES.add('msvcrt')

demandimport.init(IGNORES)

# Re-export.
isenabled = demandimport.isenabled
disable = demandimport.disable
deactivated = demandimport.deactivated


def enable():
    # chg pre-imports modules so do not enable demandimport for it
    if (
        'CHGINTERNALMARK' not in os.environ
        and os.environ.get('HGDEMANDIMPORT') != 'disable'
    ):
        demandimport.enable()