The Wayback Machine - https://web.archive.org/web/20210904155918/https://github.com/matplotlib/matplotlib/issues/20821
Skip to content
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

[MNT]: mathtext.MathTextParser is slow #20821

Open
HDembinski opened this issue Aug 10, 2021 · 2 comments
Open

[MNT]: mathtext.MathTextParser is slow #20821

HDembinski opened this issue Aug 10, 2021 · 2 comments

Comments

@HDembinski
Copy link

@HDembinski HDembinski commented Aug 10, 2021

Summary

The mathtext.MathTextParser is a performance bottleneck.

I profiled my interactive app that uses PyQt and matplotlib to draw complex plots with very little text and yet significant time (20 %) is spend in the mathtext.MathTextParser. See
profile_graph.pdf

Proposed fix

The parser is using pyparsing internally and could potentially be speed up by switching to lark.

@tacaswell
Copy link
Member

@tacaswell tacaswell commented Aug 10, 2021

Effectively re-writing the mathtext module does not seem a small project (as I assume the output of the parsing step will be different enough that we will have to re-write the code that consumes it). It might be a good GSOC project?

A less invasive fix would might to add some strategic @functools.lru_cache. Looking at the pdf from the function names it looks like we are doing caching but either the caching is still slow (maybe too many function calls?) or you are generating cache misses. It would be good to sort out which one of those it is.

If your text is not changing frequently you may also get a speed win (at the cost of an expensive first draw) by switching to usetex as we aggressively cache those results.

@HDembinski
Copy link
Author

@HDembinski HDembinski commented Aug 30, 2021

It is probably not easy to replace the parser, true.

The hot-fix for my app was to remove all LaTeX, which produced a perceivable performance gain.

Some of the LaTeX was static, some was dynamically changing. I did not try to remove only the dynamic components which cannot be cached.

@tacaswell tacaswell added this to the unassigned milestone Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants