Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove Forward definitions where possible. #22875

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

Merged
merged 1 commit into from
Apr 30, 2022

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 21, 2022

pyparsing.Forward only needs to be used for mutually recursive mathtext
elements; others can be defined directly (using = instead of <<=).
This speeds up

MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)'

by around 6%.

(This was the first suggestion in #20821 (comment).)

PR Summary

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

pyparsing.Forward only needs to be used for mutually recursive mathtext
elements; others can be defined directly (using `=` instead of `<<=`).
This speeds up
```
MPLBACKEND=agg python -c 'import time; from pylab import *; from matplotlib.tests.test_mathtext import math_tests; fig = figure(figsize=(3, 10)); fig.text(0, 0, "\n".join(filter(None, math_tests)), size=6); start = time.perf_counter(); [fig.canvas.draw() for _ in range(10)]; print((time.perf_counter() - start) / 10)'
```
by around 6%.
p.non_math + ZeroOrMore(p.math_string + p.non_math) + StringEnd()
)
set_names_and_parse_actions() # for leaf definitions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to call this 3x?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because e.g. float_literal is defined at the very top (root definition), but then gets used as float_literal("rulesize") in the mutually recursive definitions block; this creates a copy of float_literal and adds a name ("rulesize") to it. Therefore, float_literal must already have its parse action set at that point. So it's not possible to set all parse actions at the end after all definitions. (It's also not possible to set the parse actions all together after declaring all blocks (as was done before), as the point of the PR is exactly to not have to declare blocks anymore.)

Technically, this does mean that the same parse action gets re-set three times on the root elements and two times on the mutually recursive elements, but this isn't a problem at all, afaict.

@tacaswell tacaswell merged commit e9e62ea into matplotlib:main Apr 30, 2022
@anntzer anntzer deleted the mathtextunforward branch May 1, 2022 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants