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

Skip to content

Faster title alignment #28300

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 2 commits into from
May 28, 2024
Merged

Faster title alignment #28300

merged 2 commits into from
May 28, 2024

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented May 25, 2024

PR summary

  1. Part of the calculation of top does not depend on title, so move it outside the loop.
  2. If no titles have been set, skip this logic entirely.

I used fig_many.savefig(stream, format='svg', bbox_inches='tight') and fig_many_sharex.savefig(stream, format='svg', bbox_inches='tight') from #26150 as benchmarks. fig_many.savefig ran in ~320-330 ms regardless of this change. For fig_many_sharex I got

With main:

484 ms ± 9.87 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

After (1)

328 ms ± 8.95 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

After (2)

230 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

PR checklist

@rcomer
Copy link
Member Author

rcomer commented May 25, 2024

I admit I haven't actually figured out how the first change made the given example faster.

Edit: ax.xaxis.get_ticks_position() returns "unknown" here so ax.xaxis.get_tightbbox gets called, but I have not dug any deeper...

@rcomer rcomer marked this pull request as draft May 26, 2024 07:26
@rcomer
Copy link
Member Author

rcomer commented May 26, 2024

I think this would benefit from more digging: the fact that there is no measurable time difference without sharex suggests that this method ought to be fast and the real problem for sharex is deeper.

@rcomer
Copy link
Member Author

rcomer commented May 26, 2024

More digging completed #26150 (comment)

Comment on lines +3006 to +3007
xticklabel_top = any(tick.label2.get_visible() for tick in
[ax.xaxis.majorTicks[0], ax.xaxis.minorTicks[0]])
Copy link
Member Author

@rcomer rcomer May 27, 2024

Choose a reason for hiding this comment

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

Directly check the visibility of the tick label as I think that is the most relevant part from xaxis.get_ticks_position() and will prevent the unnecessary calls to xaxis.get_tightbbox mentioned at #26150 (comment). Only checking the first tick is consistent with get_ticks_position.

There is a question in my mind about what happens if the ticks are pointing outward but unlabelled, but I think that could already be an issue with the existing approach as you could have "default" position and outward ticks. Edit: the ticks themselves aren't included in get_tightbbox so actually this makes no difference.

@rcomer rcomer marked this pull request as ready for review May 27, 2024 19:10
@rcomer
Copy link
Member Author

rcomer commented May 28, 2024

I think CodeCov was telling me that we didn't have any tests with a child axes and an automatically positioned title, so I added one.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

This seems good to me. Does it actually speed things up?

@rcomer
Copy link
Member Author

rcomer commented May 28, 2024

Does it actually speed things up?

If I take this example from #26150
fig_many_sharex.savefig(stream, format='svg', bbox_inches='tight')

With main I get

477 ms ± 13.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

with this branch

224 ms ± 6.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

That example has no titles though, so we are now just skipping everything. If I add a title to each subplot I get
main

511 ms ± 9.67 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Branch

275 ms ± 6.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

If I add a title to each subplot and move the ticklabels to the top of each subplot, so we do actually need to calculate xaxis.get_tightbbox (but now fewer times)
main

707 ms ± 13.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Branch

464 ms ± 9.57 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

@jklymak
Copy link
Member

jklymak commented May 28, 2024

We have a benchmarking test suite. I wonder if these are caught by it? @QuLogic maintains that - I actually forget where it is...

@rcomer
Copy link
Member Author

rcomer commented May 28, 2024

We have a benchmarking test suite

This is news to me!

@timhoffm timhoffm added this to the v3.10.0 milestone May 28, 2024
@timhoffm timhoffm merged commit 9a9fce5 into matplotlib:main May 28, 2024
41 of 42 checks passed
@rcomer rcomer deleted the empty-titles branch May 29, 2024 12:08
@QuLogic
Copy link
Member

QuLogic commented May 30, 2024

We have a benchmarking test suite

This is news to me!

We do; it is here. However, we don't test bbox_inches='tight' or sharex, and unfortunately, I haven't run the benchmark in a long while now.

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.

5 participants