Mercurial > hg
view tests/test-narrow-trackedcmd.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 | 40f78072fda9 |
children | 5c2a4f37eace |
line wrap: on
line source
#testcases flat tree $ . "$TESTDIR/narrow-library.sh" #if tree $ cat << EOF >> $HGRCPATH > [experimental] > treemanifest = 1 > EOF #endif $ hg init master $ cd master $ cat >> .hg/hgrc <<EOF > [narrow] > serveellipses=True > EOF $ mkdir inside $ echo 'inside' > inside/f $ hg add inside/f $ hg commit -m 'add inside' $ mkdir widest $ echo 'widest' > widest/f $ hg add widest/f $ hg commit -m 'add widest' $ mkdir outside $ echo 'outside' > outside/f $ hg add outside/f $ hg commit -m 'add outside' $ cd .. narrow clone the inside file $ hg clone --narrow ssh://user@dummy/master narrow --include inside requesting all changes adding changesets adding manifests adding file changes added 2 changesets with 1 changes to 1 files new changesets *:* (glob) updating to branch default 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ cd narrow $ hg tracked I path:inside $ ls inside $ cat inside/f inside $ cd .. add more upstream files which we will include in a wider narrow spec $ cd master $ mkdir wider $ echo 'wider' > wider/f $ hg add wider/f $ echo 'widest v2' > widest/f $ hg commit -m 'add wider, update widest' $ echo 'widest v3' > widest/f $ hg commit -m 'update widest v3' $ echo 'inside v2' > inside/f $ hg commit -m 'update inside' $ mkdir outside2 $ echo 'outside2' > outside2/f $ hg add outside2/f $ hg commit -m 'add outside2' $ echo 'widest v4' > widest/f $ hg commit -m 'update widest v4' $ hg log -T "{if(ellipsis, '...')}{rev}: {desc}\n" 7: update widest v4 6: add outside2 5: update inside 4: update widest v3 3: add wider, update widest 2: add outside 1: add widest 0: add inside $ cd .. Testing the --import-rules flag of `hg tracked` command $ cd narrow $ hg tracked --import-rules hg tracked: option --import-rules requires argument hg tracked [OPTIONS]... [REMOTE] show or change the current narrowspec options ([+] can be repeated): --addinclude VALUE [+] new paths to include --removeinclude VALUE [+] old paths to no longer include --auto-remove-includes automatically choose unused includes to remove --addexclude VALUE [+] new paths to exclude --import-rules VALUE import narrowspecs from a file --removeexclude VALUE [+] old paths to no longer exclude --clear whether to replace the existing narrowspec --force-delete-local-changes forces deletion of local changes when narrowing --update-working-copy update working copy when the store has changed -e --ssh CMD specify ssh command to use --remotecmd CMD specify hg command to run on the remote side --insecure do not verify server certificate (ignoring web.cacerts config) (use 'hg tracked -h' to show more help) [255] $ hg tracked --import-rules doesnotexist abort: cannot read narrowspecs from '$TESTTMP/narrow/doesnotexist': $ENOENT$ [255] $ cat > specs <<EOF > %include foo > [include] > path:widest/ > [exclude] > path:inside/ > EOF $ hg tracked --import-rules specs abort: including other spec files using '%include' is not supported in narrowspec [255] $ cat > specs <<EOF > [include] > outisde > [exclude] > inside > EOF $ hg tracked --import-rules specs comparing with ssh://user@dummy/master searching for changes looking for local changes to affected paths deleting data/inside/f.i deleting meta/inside/00manifest.i (tree !) saved backup bundle to $TESTTMP/narrow/.hg/strip-backup/*-widen.hg (glob) adding changesets adding manifests adding file changes added 2 changesets with 0 changes to 0 files $ hg tracked I path:outisde X path:inside Testing the --import-rules flag with --addinclude and --addexclude $ cat > specs <<EOF > [include] > widest > EOF $ hg tracked --import-rules specs --addinclude 'wider/' comparing with ssh://user@dummy/master searching for changes saved backup bundle to $TESTTMP/narrow/.hg/strip-backup/*-widen.hg (glob) adding changesets adding manifests adding file changes added 3 changesets with 1 changes to 1 files $ hg tracked I path:outisde I path:wider I path:widest X path:inside $ cat > specs <<EOF > [exclude] > outside2 > EOF $ hg tracked --import-rules specs --addexclude 'widest' comparing with ssh://user@dummy/master searching for changes looking for local changes to affected paths deleting data/widest/f.i deleting meta/widest/00manifest.i (tree !) $ hg tracked I path:outisde I path:wider X path:inside X path:outside2 X path:widest $ hg tracked --import-rules specs --clear abort: the --clear option is not yet supported [255] Testing with passing a out of wdir file $ cat > ../nspecs <<EOF > [include] > widest > EOF $ hg tracked --import-rules ../nspecs comparing with ssh://user@dummy/master searching for changes saved backup bundle to $TESTTMP/narrow/.hg/strip-backup/*-widen.hg (glob) adding changesets adding manifests adding file changes added 3 changesets with 0 changes to 0 files $ cd .. Testing tracked command on a non-narrow repo $ hg init non-narrow $ cd non-narrow $ hg tracked --addinclude foobar abort: the tracked command is only supported on repositories cloned with --narrow [255]