-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Delayed import of pyparsing for fontconfig pattern matching #12915
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
Conversation
If you've recently modified some files (editing or switching branches, etc.), then be careful to run the test at least twice. The first import can be slowed down by updating of the bytecode files. |
1b0820f
to
1b31dbd
Compare
@QuLogic Thanks for the hint. I ran each code three times and just posted the result of the third run. Anyway, the result shows that pyparsing is not imported and thus importing rcsetup is fast. And that's the goal of the PR. |
This looks good. If pyparsing is such a heavy lift is there something lighter than can be imported? |
I'd like to ultimately get rid of fontconfig patterns altogether (#10249), but incremental improvements are fine in the meantime. |
👍 in principle, agree that incremental improvements are worth doing, but also agree with @anntzer that this may be un-done by the mathtext import. It maybe better to time importing pyplot (with Agg as the backend)? |
or something like matplotlib.figure + matplotlib.backend_bases, which should be the absolute minimum needed to do anything (e.g. using a third-party (hemhem :)) backend). |
Ok, here's the import timing of
As one can see, there's still no pyparsing import. Of course, this PR is "just" a 5% improvement, but it works as is. Really wondering what the big gap in |
My guess at the big delay is versioneer calling git via subprocess. That only happens in the dev install, if you 'properly' install it that call is replaced by a string lookup. |
I don't know why it doesn't show up in -Ximporttime output, but pyparsing is actually still being imported: this can be checked by printing out sys.modules; or one can just also check that backend_agg imports mathtext, which imports pyparsing... |
Does this mean, this PR will have no effect despite the importtime results? |
What's exactly happening is a bit mysterious to me too, but I'm just reporting my observations... |
No idea how to investigate this further. So, either we merge this and hope that it has some effect, or we simply close the PR. Both would be fine for me. |
I would prefer we figure out what's happening before taking further action either way... |
Do you have any idea what to check? |
nothing right now |
Added the needs revision tag based on the discussion above as I'm not actually convinced this works, but again I'm not really sure what's exactly happening here. |
I just rebased this on Edit: I forget to do a non-editable install like @tacaswell mentioned, but while a bit quicker, it's still not really affected by this PR. |
1b31dbd
to
4dd513e
Compare
Rebased so that it's easier to look into this further. |
AFAICS there are the following additional ways we import pyparsing:
Technical note: I've patched
|
AFAICT, recent versions of pyparsing now import much faster, and now only contribute <2% of the total import time. Unless others can repro the original problem, I propose to close this. |
Interestingly, your timings and import sequence is completely different from mine. Did you use What's more disturbing is that were now 0.1-0.2s slower overall. But execution timing is a difficult topic and I don't want to go that rabbit hole here for now. |
PR Summary
This PR aims at speeding up the matplotlib import by lazily importing pyparsing. See #11546.
Essentially this has two parts:
FontconfigPatternParser.__init__
so that it's only imported when needed.FontConfigParser
.This yields at least a 10% faster matplotlib import for standard settings. See the import timing in #11546.
I also did the timing myself, which I don't fully understand since the import without the modification of this PR has some additional import delay directly in the matplotlib base package. Anyway, the
rcsetup
und thuspyparsing
have vanished as expected.before:

after:

Note:
The
_SIMPLE_PATTERN_CACHE
may be expanded if you know other simple and common fontconfig patterns.