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

Skip to content

Minor cleanup and optimization of Sketch #24964

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
Feb 2, 2023

Conversation

oscargus
Copy link
Member

PR Summary

Getting rid of a few redundant/not required computations. Have not checked the assembly code to confirm that the compiler doesn't already optimize it away though.

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

len = sqrt(len);
*x += r * num / len;
*y += r * -den / len;
double r = sin(m_p * (2.0 * d_M_PI) / m_length) * m_scale;
Copy link
Member Author

Choose a reason for hiding this comment

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

Two minor changes since the review:

  • Brackets to enable constant folding here.
  • Change += x * -y to -= x * y below.

At least both these give simplifications in the LLVM IR (maybe the backend will still provide the same code in the end though, but probably not for the first one due to FP aspects).

@oscargus oscargus marked this pull request as draft January 19, 2023 05:00
@oscargus
Copy link
Member Author

I did a major rewrite after reading up on pow works. This is now replaced by exp, which makes it much faster on x86 and shouldn't make much change on other platforms (exp shouldn't be worse than pow).

Added some comments about how it is refactored.

For my test example, similar to #24908 (comment), the time doing pow was 33% of the total time, now the time doing exp is 16% of the total time. (Hard to compare actual times as the workloads were slightly different on the machine...)

@oscargus
Copy link
Member Author

oscargus commented Jan 19, 2023

One may want to move the random number generation and exp into the if-clause. However, that may change the outcome, although I guess that the if-clause is primarily for avoiding division by zero in rare cases rather than something that is heavily used, so there may be limited performance benefits and limited changes in the outcome.

Edit: I guess what I am trying to say is that if there is a performance benefit of moving it there, it will also modify the output to a larger extent as what we are doing is skipping updating the phase and random sequence when len is 0. So probably not a good idea.

@jklymak
Copy link
Member

jklymak commented Feb 2, 2023

Is this code path tested? Do you have evidence that this is actually a speed up?

@tacaswell tacaswell added this to the v3.8.0 milestone Feb 2, 2023
@tacaswell
Copy link
Member

We will hit this in : https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/tests/baseline_images/test_path/xkcd.png

I'm happy to take Oscar at his word that this is faster.

@tacaswell tacaswell merged commit e86ad1e into matplotlib:main Feb 2, 2023
@oscargus
Copy link
Member Author

oscargus commented Feb 3, 2023

Yes. it is faster (and tested). At least on x86, but shouldn't be slower on other architectures (as in about as fast, worst case). The background is that pow(a, b) is computed as exp(b*log(a)) internally on x86 (as there are exp and log instructions). Since a is constant in this case, one can compute log(a) once.

(Been focusing quite heavily on a work project recently...)

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.

4 participants