Mercurial > hg
view tests/test-basic.t @ 44118:f81c17ec303c
hgdemandimport: apply lazy module loading to sys.meta_path finders
Python's `sys.meta_path` finders are the primary objects whose job it
is to find a module at import time. When `import` is called, Python
iterates objects in this list and calls `o.find_spec(...)` to find
a `ModuleSpec` (or None if the module couldn't be found by that
finder). If no meta path finder can find a module, import fails.
One of the default meta path finders is `PathFinder`. Its job is to
import modules from the filesystem and is probably the most important
importer. This finder looks at `sys.path` and `sys.path_hooks` to do
its job.
The `ModuleSpec` returned by `MetaPathImporter.find_spec()` has a
`loader` attribute, which defines the concrete module loader to use.
`sys.path_hooks` is a hook point for teaching `PathFinder` to
instantiate custom loader types.
Previously, we injected a custom `sys.path_hook` that told `PathFinder`
to wrap the default loaders with a loader that creates a module object
that is lazy.
This approach worked. But its main limitation was that it only applied
to the `PathFinder` meta path importer. There are other meta path
importers that are registered. And in the case of PyOxidizer loading
modules from memory, `PathFinder` doesn't come into play since
PyOxidizer's own meta path importer was handling all imports.
This commit changes our approach to lazy module loading by proxying
all meta path importers. Specifically, we overload the `find_spec()`
method to swap in a wrapped loader on the `ModuleSpec` before it
is returned. The end result of this is all meta path importers should
be lazy.
As much as I would have loved to utilize .__class__ manipulation to
achieve this, some meta path importers are implemented in C/Rust
in such a way that they cannot be monkeypatched. This is why we
use __getattribute__ to define a proxy.
Also, this change could theoretically open us up to regressions in
meta path importers whose loader is creating module objects which
can't be monkeypatched. But I'm not aware of any of these in the
wild. So I think we'll be safe.
According to hyperfine, this change yields a decent startup time win of
5-6ms:
```
Benchmark #1: ~/.pyenv/versions/3.6.10/bin/python ./hg version
Time (mean ± σ): 86.8 ms ± 0.5 ms [User: 78.0 ms, System: 8.7 ms]
Range (min … max): 86.0 ms … 89.1 ms 50 runs
Time (mean ± σ): 81.1 ms ± 2.7 ms [User: 74.5 ms, System: 6.5 ms]
Range (min … max): 77.8 ms … 90.5 ms 50 runs
Benchmark #2: ~/.pyenv/versions/3.7.6/bin/python ./hg version
Time (mean ± σ): 78.9 ms ± 0.6 ms [User: 70.2 ms, System: 8.7 ms]
Range (min … max): 78.1 ms … 81.2 ms 50 runs
Time (mean ± σ): 73.4 ms ± 0.6 ms [User: 65.3 ms, System: 8.0 ms]
Range (min … max): 72.4 ms … 75.7 ms 50 runs
Benchmark #3: ~/.pyenv/versions/3.8.1/bin/python ./hg version
Time (mean ± σ): 78.1 ms ± 0.6 ms [User: 70.2 ms, System: 7.9 ms]
Range (min … max): 77.4 ms … 80.9 ms 50 runs
Time (mean ± σ): 72.1 ms ± 0.4 ms [User: 64.4 ms, System: 7.6 ms]
Range (min … max): 71.4 ms … 74.1 ms 50 runs
```
Differential Revision: https://phab.mercurial-scm.org/D7954
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 20 Jan 2020 23:51:25 -0800 |
parents | 69883775b27d |
children | 21733e8c924f |
line wrap: on
line source
Create a repository: #if no-extraextensions $ hg config devel.all-warnings=true devel.default-date=0 0 extensions.fsmonitor= (fsmonitor !) largefiles.usercache=$TESTTMP/.cache/largefiles lfs.usercache=$TESTTMP/.cache/lfs ui.slash=True ui.interactive=False ui.merge=internal:merge ui.mergemarkers=detailed ui.promptecho=True web.address=localhost web\.ipv6=(?:True|False) (re) web.server-header=testing stub value #endif $ hg init t $ cd t Prepare a changeset: $ echo a > a $ hg add a $ hg status A a Writes to stdio succeed and fail appropriately #if devfull $ hg status 2>/dev/full A a $ hg status >/dev/full abort: No space left on device [255] #endif #if devfull $ hg status >/dev/full 2>&1 [255] $ hg status ENOENT 2>/dev/full [255] #endif $ hg commit -m test This command is ancient: $ hg history changeset: 0:acb14030fe0a tag: tip user: test date: Thu Jan 01 00:00:00 1970 +0000 summary: test Verify that updating to revision 0 via commands.update() works properly $ cat <<EOF > update_to_rev0.py > from mercurial import commands, hg, ui as uimod > myui = uimod.ui.load() > repo = hg.repository(myui, path=b'.') > commands.update(myui, repo, rev=b"0") > EOF $ hg up null 0 files updated, 0 files merged, 1 files removed, 0 files unresolved $ "$PYTHON" ./update_to_rev0.py 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ hg identify -n 0 Poke around at hashes: $ hg manifest --debug b789fdd96dc2f3bd229c1dd8eedf0fc60e2b68e3 644 a $ hg cat a a Verify should succeed: $ hg verify checking changesets checking manifests crosschecking files in changesets and manifests checking files checked 1 changesets with 1 changes to 1 files Repository root: $ hg root $TESTTMP/t $ hg log -l1 -T '{reporoot}\n' $TESTTMP/t $ hg root -Tjson | sed 's|\\\\|\\|g' [ { "hgpath": "$TESTTMP/t/.hg", "reporoot": "$TESTTMP/t", "storepath": "$TESTTMP/t/.hg/store" } ] At the end... $ cd .. Status message redirection: $ hg init empty status messages are sent to stdout by default: $ hg outgoing -R t empty -Tjson 2>/dev/null comparing with empty searching for changes [ { "bookmarks": [], "branch": "default", "date": [0, 0], "desc": "test", "node": "acb14030fe0a21b60322c440ad2d20cf7685a376", "parents": ["0000000000000000000000000000000000000000"], "phase": "draft", "rev": 0, "tags": ["tip"], "user": "test" } ] which can be configured to send to stderr, so the output wouldn't be interleaved: $ cat <<'EOF' >> "$HGRCPATH" > [ui] > message-output = stderr > EOF $ hg outgoing -R t empty -Tjson 2>/dev/null [ { "bookmarks": [], "branch": "default", "date": [0, 0], "desc": "test", "node": "acb14030fe0a21b60322c440ad2d20cf7685a376", "parents": ["0000000000000000000000000000000000000000"], "phase": "draft", "rev": 0, "tags": ["tip"], "user": "test" } ] $ hg outgoing -R t empty -Tjson >/dev/null comparing with empty searching for changes this option should be turned off by HGPLAIN= since it may break scripting use: $ HGPLAIN= hg outgoing -R t empty -Tjson 2>/dev/null comparing with empty searching for changes [ { "bookmarks": [], "branch": "default", "date": [0, 0], "desc": "test", "node": "acb14030fe0a21b60322c440ad2d20cf7685a376", "parents": ["0000000000000000000000000000000000000000"], "phase": "draft", "rev": 0, "tags": ["tip"], "user": "test" } ] but still overridden by --config: $ HGPLAIN= hg outgoing -R t empty -Tjson --config ui.message-output=stderr \ > 2>/dev/null [ { "bookmarks": [], "branch": "default", "date": [0, 0], "desc": "test", "node": "acb14030fe0a21b60322c440ad2d20cf7685a376", "parents": ["0000000000000000000000000000000000000000"], "phase": "draft", "rev": 0, "tags": ["tip"], "user": "test" } ] Invalid ui.message-output option: $ hg log -R t --config ui.message-output=bad abort: invalid ui.message-output destination: bad [255] Underlying message streams should be updated when ui.fout/ferr are set: $ cat <<'EOF' > capui.py > from mercurial import pycompat, registrar > cmdtable = {} > command = registrar.command(cmdtable) > @command(b'capui', norepo=True) > def capui(ui): > out = ui.fout > ui.fout = pycompat.bytesio() > ui.status(b'status\n') > ui.ferr = pycompat.bytesio() > ui.warn(b'warn\n') > out.write(b'stdout: %s' % ui.fout.getvalue()) > out.write(b'stderr: %s' % ui.ferr.getvalue()) > EOF $ hg --config extensions.capui=capui.py --config ui.message-output=stdio capui stdout: status stderr: warn