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

Better codegen / perf form Math.Max and Math.Min #33851

Merged
merged 5 commits into from
Apr 14, 2020

Conversation

gfoidl
Copy link
Member

@gfoidl gfoidl commented Mar 20, 2020

Based on #33057 (comment) I picked the variant MinReorder as it has best overall performance on average.
With #33476 there will be "another boost" for this method.

Added more testcases for Max / Min.

Fixes #33057

/cc: @tannergooding

@gfoidl gfoidl changed the title Math max min double Better codegen / perf form Math.Max and Math.Min Mar 20, 2020
Copy link
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for doing this and increasing the test coverage.

It might be worth looking at a similar optimization for MaxMagnitude and MinMagnitude which are functionally similar but which don't propagate NaN

@tannergooding
Copy link
Member

CC. @pgovind

@gfoidl
Copy link
Member Author

gfoidl commented Mar 20, 2020

It might be worth looking at a similar optimization for MaxMagnitude and MinMagnitude which are functionally similar but which don't propagate NaN

I will have a look over the weekend / next week.

@jkotas
Copy link
Member

jkotas commented Mar 20, 2020

Any performance numbers?

How much of the improvement is due to AggressiveInlining vs. the other changes? Also, what is the code side growth due to AggressiveInlining?

@tannergooding
Copy link
Member

Various perf numbers listed starting here: #33057 (comment), with additional analysis and graphs shown below that in the additional conversation.

It is a 2-3x perf increase from aggressive inlining alone. The code reorder increases perf an additional 10% or so from the instruction reordering (depending on inputs).

Assuming nothing gets dropped from inlining (both inputs are non-constant) the bloat isn't trivial: https://gist.github.com/tannergooding/d81a6cd7530ec1cdc27e08530922f02a, it would be ~67 bytes for just aggressive inlining and ~66 bytes for the branch reorder. The SIMD version is almost half that (~35 bytes), but doesn't perform as well on Intel CPUs (we get less than a 2x perf increase).

@tannergooding
Copy link
Member

@jkotas, do you have any concerns with the change based on the numbers and code size growth?

@jkotas
Copy link
Member

jkotas commented Apr 13, 2020

Math.Max/Min is not used that often. I can live with this.

@tannergooding
Copy link
Member

Closing and reopening to retrigger tests.

@tannergooding
Copy link
Member

Thanks for the improvement @gfoidl!

@tannergooding tannergooding merged commit 7b5b7f1 into dotnet:master Apr 14, 2020
@gfoidl gfoidl deleted the math-max-min-double branch April 14, 2020 14:59
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance issues for Math.Min, Math.Max for double/float
4 participants