Mercurial > hg
view tests/test-stack.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 | 68fcc5503ec5 |
children |
line wrap: on
line source
This test test the low-level definition of stack, agnostic from all formatting Initial setup $ cat << EOF >> $HGRCPATH > [ui] > logtemplate = {rev} {branch} {phase} {desc|firstline}\n > [extensions] > rebase= > [experimental] > evolution=createmarkers,exchange,allowunstable > EOF $ hg init main $ cd main $ hg branch other marked working directory as branch other (branches are permanent and global, did you want a bookmark?) $ echo aaa > aaa $ hg add aaa $ hg commit -m c_a $ echo aaa > bbb $ hg add bbb $ hg commit -m c_b $ hg branch foo marked working directory as branch foo $ echo aaa > ccc $ hg add ccc $ hg commit -m c_c $ echo aaa > ddd $ hg add ddd $ hg commit -m c_d $ echo aaa > eee $ hg add eee $ hg commit -m c_e $ echo aaa > fff $ hg add fff $ hg commit -m c_f $ hg log -G @ 5 foo draft c_f | o 4 foo draft c_e | o 3 foo draft c_d | o 2 foo draft c_c | o 1 other draft c_b | o 0 other draft c_a Check that stack doesn't include public changesets -------------------------------------------------- $ hg up other 0 files updated, 0 files merged, 4 files removed, 0 files unresolved $ hg log -G -r "stack()" @ 1 other draft c_b | o 0 other draft c_a $ hg phase --public 'branch("other")' $ hg log -G -r "stack()" $ hg up foo 4 files updated, 0 files merged, 0 files removed, 0 files unresolved Simple test ----------- 'stack()' list all changeset in the branch $ hg branch foo $ hg log -G -r "stack()" @ 5 foo draft c_f | o 4 foo draft c_e | o 3 foo draft c_d | o 2 foo draft c_c | ~ Case with some of the branch unstable ------------------------------------ $ hg up 3 0 files updated, 0 files merged, 2 files removed, 0 files unresolved $ echo bbb > ddd $ hg commit --amend 2 new orphan changesets $ hg log -G @ 6 foo draft c_d | | * 5 foo draft c_f | | | * 4 foo draft c_e | | | x 3 foo draft c_d |/ o 2 foo draft c_c | o 1 other public c_b | o 0 other public c_a $ hg log -G -r "stack()" @ 6 foo draft c_d | ~ $ hg up -r "desc(c_e)" 2 files updated, 0 files merged, 0 files removed, 0 files unresolved $ hg log -G -r "stack()" @ 4 foo draft c_e | x 3 foo draft c_d | ~ $ hg up -r "desc(c_d)" 1 files updated, 0 files merged, 1 files removed, 0 files unresolved $ hg log -G -r "stack()" @ 6 foo draft c_d | ~ Case with multiple topological heads ------------------------------------ Make things linear again $ hg rebase -s 'desc(c_e)' -d 'desc(c_d) - obsolete()' rebasing 4:4f2a69f6d380 "c_e" rebasing 5:913c298d8b0a "c_f" $ hg log -G o 8 foo draft c_f | o 7 foo draft c_e | @ 6 foo draft c_d | o 2 foo draft c_c | o 1 other public c_b | o 0 other public c_a Create the second branch $ hg up 'desc(c_d)' 0 files updated, 0 files merged, 0 files removed, 0 files unresolved $ echo aaa > ggg $ hg add ggg $ hg commit -m c_g created new head $ echo aaa > hhh $ hg add hhh $ hg commit -m c_h $ hg log -G @ 10 foo draft c_h | o 9 foo draft c_g | | o 8 foo draft c_f | | | o 7 foo draft c_e |/ o 6 foo draft c_d | o 2 foo draft c_c | o 1 other public c_b | o 0 other public c_a Test output $ hg log -G -r "stack(10)" @ 10 foo draft c_h | o 9 foo draft c_g | ~ $ hg log -G -r "stack(8)" o 8 foo draft c_f | o 7 foo draft c_e | ~ $ hg log -G -r "stack(head())" @ 10 foo draft c_h | o 9 foo draft c_g | ~ o 8 foo draft c_f | o 7 foo draft c_e | ~ Check the stack order $ hg log -r "first(stack())" 9 foo draft c_g $ hg log -r "first(stack(10))" 9 foo draft c_g $ hg log -r "first(stack(8))" 7 foo draft c_e $ hg log -r "first(stack(head()))" 7 foo draft c_e Case with multiple heads with unstability involved -------------------------------------------------- We amend the message to make sure the display base pick the right changeset $ hg up 'desc(c_d)' 0 files updated, 0 files merged, 2 files removed, 0 files unresolved $ echo ccc > ddd $ hg commit --amend -m 'c_D' 4 new orphan changesets $ hg rebase -d . -s 'desc(c_g)' rebasing 9:2ebb6e48ab8a "c_g" rebasing 10:634f38e27a1d "c_h" $ hg log -G o 13 foo draft c_h | o 12 foo draft c_g | @ 11 foo draft c_D | | * 8 foo draft c_f | | | * 7 foo draft c_e | | | x 6 foo draft c_d |/ o 2 foo draft c_c | o 1 other public c_b | o 0 other public c_a We should improve stack definition to also show 12 and 13 here $ hg log -G -r "stack()" @ 11 foo draft c_D | ~