Defer importing devtools in sitecustomize.py.#49
Defer importing devtools in sitecustomize.py.#49kalekundert wants to merge 4 commits intosamuelcolvin:masterfrom
devtools in sitecustomize.py.#49Conversation
It's convenient to be able to use `debug()` as if it were a builtin
function, but importing `devtools` directly in `sitecustomize.py` is a
pretty significant performance hit for every invocation of python that
doesn't use `debug()`. Specifically, this import takes 100-200 ms,
which is a noticeable delay for interactive sessions and short-running
scripts:
$ python -X importtime -c 'import devtools'
While it would probably be possible to make `devtools` faster to import,
the best solution is simply to avoid importing `devtools` unless it will
be used. It took me a while to figure out how to do this, but in the
end the code is pretty clear and succinct, and I think it will be useful
to others.
Codecov Report
@@ Coverage Diff @@
## master #49 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 6 6
Lines 499 499
Branches 79 79
=========================================
Hits 499 499 |
|
I hadn't noticed this, thanks so much for pointing it out. The main problem with this approach is that other tools attached to I know 99.9% of debug usage is just There's also an example in the docs that isn't updated. The other problem is that debug import could be speeded up a lot, see pydantic/pydantic#1127 for a discussion of something similar. In summary I think we should:
from devtools.lazy import lazy_debug
__builtins__['debug'] = lazy_debug |
|
See #50 - I think if we can improve the import time from 100-200ms to 10-20ms, perhaps we don't need a lazy import workaround? |
|
That all sounds really good, especially the idea of adding a I didn't know about |
|
okay, happy to accept this PR, but could you update the duplicate code in the docs |
|
Sorry, you mentioned that earlier and I didn't notice. Should be up to date now. |
|
I think this is solved by #50, I'll make a release soon. |
|
FWIW here's how I solve a similar problem in birdseye: https://github.com/alexmojaki/birdseye/blob/master/birdseye/__init__.py It's a lazy object similar to this PR but it's part of the library itself, not just a sitecustomize recipe. It's worked great for me. |
It's convenient to be able to use
debug()as if it were a builtin function, but importingdevtoolsdirectly insitecustomize.pyis a pretty significant performance hit for every invocation of python that doesn't usedebug(). Specifically, this import takes 100-200 ms, which is a noticeable delay for interactive sessions and short-running scripts:While it would probably be possible to make
devtoolsfaster to import, the best solution is simply to avoid importingdevtoolsunless it will be used. It took me a while to figure out how to do this, but in the end the code is pretty clear and succinct, and I think it will be useful to others:This PR updates the
READMEfile to include the above snippet.