Mercurial > hg
view tests/test-pull-bundle.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 | d7304434390f |
children | 3d5fb6cab832 |
line wrap: on
line source
#require no-chg $ hg init repo $ cd repo $ echo foo > foo $ hg ci -qAm 'add foo' $ echo >> foo $ hg ci -m 'change foo' $ hg up -qC 0 $ echo bar > bar $ hg ci -qAm 'add bar' $ hg log changeset: 2:effea6de0384 tag: tip parent: 0:bbd179dfa0a7 user: test date: Thu Jan 01 00:00:00 1970 +0000 summary: add bar changeset: 1:ed1b79f46b9a user: test date: Thu Jan 01 00:00:00 1970 +0000 summary: change foo changeset: 0:bbd179dfa0a7 user: test date: Thu Jan 01 00:00:00 1970 +0000 summary: add foo $ cd .. Test pullbundle functionality $ cd repo $ cat <<EOF > .hg/hgrc > [server] > pullbundle = True > [extensions] > blackbox = > EOF $ hg bundle --base null -r 0 .hg/0.hg 1 changesets found $ hg bundle --base 0 -r 1 .hg/1.hg 1 changesets found $ hg bundle --base 1 -r 2 .hg/2.hg 1 changesets found $ cat <<EOF > .hg/pullbundles.manifest > 2.hg BUNDLESPEC=none-v2 heads=effea6de0384e684f44435651cb7bd70b8735bd4 bases=bbd179dfa0a71671c253b3ae0aa1513b60d199fa > 1.hg BUNDLESPEC=bzip2-v2 heads=ed1b79f46b9a29f5a6efa59cf12fcfca43bead5a bases=bbd179dfa0a71671c253b3ae0aa1513b60d199fa > 0.hg BUNDLESPEC=gzip-v2 heads=bbd179dfa0a71671c253b3ae0aa1513b60d199fa > EOF $ hg --config blackbox.track=debug --debug serve -p $HGPORT2 -d --pid-file=../repo.pid listening at http://*:$HGPORT2/ (bound to $LOCALIP:$HGPORT2) (glob) (?) $ cat ../repo.pid >> $DAEMON_PIDS $ cd .. $ hg clone -r 0 http://localhost:$HGPORT2/ repo.pullbundle adding changesets adding manifests adding file changes added 1 changesets with 1 changes to 1 files new changesets bbd179dfa0a7 (1 drafts) updating to branch default 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ cd repo.pullbundle $ hg pull -r 1 pulling from http://localhost:$HGPORT2/ searching for changes adding changesets adding manifests adding file changes added 1 changesets with 1 changes to 1 files new changesets ed1b79f46b9a (1 drafts) (run 'hg update' to get a working copy) $ hg pull -r 2 pulling from http://localhost:$HGPORT2/ searching for changes adding changesets adding manifests adding file changes added 1 changesets with 1 changes to 1 files (+1 heads) new changesets effea6de0384 (1 drafts) (run 'hg heads' to see heads, 'hg merge' to merge) $ cd .. $ killdaemons.py $ grep 'sending pullbundle ' repo/.hg/blackbox.log * sending pullbundle "0.hg" (glob) * sending pullbundle "1.hg" (glob) * sending pullbundle "2.hg" (glob) $ rm repo/.hg/blackbox.log Test pullbundle functionality for incremental pulls $ cd repo $ hg --config blackbox.track=debug --debug serve -p $HGPORT2 -d --pid-file=../repo.pid listening at http://*:$HGPORT2/ (bound to $LOCALIP:$HGPORT2) (glob) (?) $ cat ../repo.pid >> $DAEMON_PIDS $ cd .. $ hg clone http://localhost:$HGPORT2/ repo.pullbundle2 requesting all changes adding changesets adding manifests adding file changes adding changesets adding manifests adding file changes adding changesets adding manifests adding file changes added 3 changesets with 3 changes to 3 files (+1 heads) new changesets bbd179dfa0a7:ed1b79f46b9a (3 drafts) updating to branch default 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ killdaemons.py $ grep 'sending pullbundle ' repo/.hg/blackbox.log * sending pullbundle "0.hg" (glob) * sending pullbundle "2.hg" (glob) * sending pullbundle "1.hg" (glob) $ rm repo/.hg/blackbox.log Test pullbundle functionality for incoming $ cd repo $ hg --config blackbox.track=debug --debug serve -p $HGPORT2 -d --pid-file=../repo.pid listening at http://*:$HGPORT2/ (bound to $LOCALIP:$HGPORT2) (glob) (?) $ cat ../repo.pid >> $DAEMON_PIDS $ cd .. $ hg clone http://localhost:$HGPORT2/ repo.pullbundle2a -r 0 adding changesets adding manifests adding file changes added 1 changesets with 1 changes to 1 files new changesets bbd179dfa0a7 (1 drafts) updating to branch default 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ cd repo.pullbundle2a $ hg incoming -r ed1b79f46b9a comparing with http://localhost:$HGPORT2/ searching for changes changeset: 1:ed1b79f46b9a tag: tip user: test date: Thu Jan 01 00:00:00 1970 +0000 summary: change foo $ cd .. $ killdaemons.py $ grep 'sending pullbundle ' repo/.hg/blackbox.log * sending pullbundle "0.hg" (glob) * sending pullbundle "1.hg" (glob) $ rm repo/.hg/blackbox.log Test recovery from misconfigured server sending no new data $ cd repo $ cat <<EOF > .hg/pullbundles.manifest > 0.hg heads=ed1b79f46b9a29f5a6efa59cf12fcfca43bead5a bases=bbd179dfa0a71671c253b3ae0aa1513b60d199fa > 0.hg heads=bbd179dfa0a71671c253b3ae0aa1513b60d199fa > EOF $ hg --config blackbox.track=debug --debug serve -p $HGPORT2 -d --pid-file=../repo.pid listening at http://*:$HGPORT2/ (bound to $LOCALIP:$HGPORT2) (glob) (?) $ cat ../repo.pid >> $DAEMON_PIDS $ cd .. $ hg clone -r 0 http://localhost:$HGPORT2/ repo.pullbundle3 adding changesets adding manifests adding file changes added 1 changesets with 1 changes to 1 files new changesets bbd179dfa0a7 (1 drafts) updating to branch default 1 files updated, 0 files merged, 0 files removed, 0 files unresolved $ cd repo.pullbundle3 $ hg pull -r 1 pulling from http://localhost:$HGPORT2/ searching for changes adding changesets adding manifests adding file changes added 0 changesets with 0 changes to 1 files abort: 00changelog.i@ed1b79f46b9a: no node! [255] $ cd .. $ killdaemons.py $ grep 'sending pullbundle ' repo/.hg/blackbox.log * sending pullbundle "0.hg" (glob) * sending pullbundle "0.hg" (glob) $ rm repo/.hg/blackbox.log