Skip to content

Use cycling iterators in RendererBase. #21696

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
Dec 16, 2021

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Nov 21, 2021

This defines all the cycling behavior in a single block, rather than
spreading modulo operations all over.

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).

gc0.set_foreground(fg)
gc0.set_dashes(*ls)
if len(ec) == 4 and ec[3] == 0.0:
gc0.set_linewidth(0)
Copy link
Member

Choose a reason for hiding this comment

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

This seems like an important special case to have a test for? If I am reading this right, this is a fast-path to set a line with 0 alpha to 0 width?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's just an optimization? In fact, backend_ps (the only backend with no support for transparency, AFAICT) already special-cases transparent lines to not be drawn in its own implementation of draw_path_collection (via _draw_ps), so I'd say we could even get rid of this special case (well, in this PR I was just keeping the old semantics, but if you're up for changing that as well...).

@tacaswell tacaswell added this to the v3.6.0 milestone Nov 22, 2021
trfs = (itertools.cycle(map(Affine2D, all_transforms)) if Ntransforms
else itertools.repeat(transforms.IdentityTransform()))
for path, trf in itertools.islice(zip(paths, trfs), N):
yield path, trf + master_transform
Copy link
Member

Choose a reason for hiding this comment

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

This is significantly harder to read than what it replaces. Is there a significant performance advantage?

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 was mostly changed for consistency with _iter_collection (where I'd argue that having all the cycling behavior in one place definitely improves readability); here I agree the benefit is less clear and can revert that if you prefer.

Copy link
Member

@timhoffm timhoffm Nov 22, 2021

Choose a reason for hiding this comment

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

I agree with @jklymak: the original version is more readable (unless you are a functional programming enthusiast, which, without judgement, only few are).

Copy link
Member

Choose a reason for hiding this comment

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

one vote for the change? I find the new version clearer - for me, the modulo indexing based cycling took a couple of minutes to parse first time I saw it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, restored the old version here.

Copy link
Member

Choose a reason for hiding this comment

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

It's probably fine if you are intimately familiar with itertools, but I definitely am not, whereas I have no problem with modulo.

Copy link
Member

Choose a reason for hiding this comment

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

itertools is the best, but 🤷‍♀️ I like the cleanup either way

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure it would be measurable here, but I've definitely run into code where itertools was >> "doing math" in terms of runtime performance. I only even mention it since we're in the render loop.

Copy link
Member

Choose a reason for hiding this comment

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

@dopplershift i can't parse your statement as itertools better or worse

Copy link
Contributor

Choose a reason for hiding this comment

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

Generally, itertools better, but not no idea if my runtime argument is meaningful here or not.

This defines all the cycling behavior in a single block, rather than
spreading modulo operations all over.
@anntzer anntzer force-pushed the rendererbase_iter_collection branch from 2ce120b to 35c4b07 Compare November 22, 2021 19:19
@QuLogic QuLogic merged commit aa5fdbb into matplotlib:main Dec 16, 2021
@anntzer anntzer deleted the rendererbase_iter_collection branch December 16, 2021 08:08
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.

7 participants