New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Defer importing devtools
in sitecustomize.py
.
#49
Conversation
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 importingdevtools
directly insitecustomize.py
is 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
devtools
faster to import, the best solution is simply to avoid importingdevtools
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:This PR updates the
README
file to include the above snippet.