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

Use likely/unlikely to improve certain vector ops #66

Merged
merged 4 commits into from
Dec 5, 2020

Conversation

lgritz
Copy link
Contributor

@lgritz lgritz commented Nov 15, 2020

Some compilers have special ways to indicate that a branch of an if
is especially likely or unlikely to be taken, and can do some
micro-optimizations based on that. Establish IMATH_LIKELY and
IMATH_UNLIKELY macros to implement this. (In C++20 and beyond, there
will be standard attributes for this, but it'll be a while before we
can rely on those.)

For a few conditions in the length and normalize methods of vectors,
in OSL we had replaced these functions with our own modified versions
after analysis from Intel engineers that these hints really did help
generate more performant code in these specific places. In this PR,
I'm pushing those improvements back to the Imath project itself.

These new macros should be used with caution -- programmers
notoriously guess wrong and can hurt performance by using them
inappropriately. "My intuition is that it will take this branch most
of the time" is not good enough. Truly exceptional conditions (like
branches that handle very rarely encountered errors) can safely be
marked, but most ordinary control flow should not use these except in
response to clear indications from performance tests.

Signed-off-by: Larry Gritz lg@larrygritz.com

Some compilers have special ways to indicate that a branch of an `if`
is especially likely or unlikely to be taken, and can do some
micro-optimizations based on that. Establish IMATH_LIKELY and
IMATH_UNLIKELY macros to implement this. (In C++20 and beyond, there
will be standard attributes for this, but it'll be a while before we
can rely on those.)

For a few conditions in the length and normalize methods of vectors,
in OSL we had replaced these functions with our own modified versions
after analysis from Intel engineers that these hints really did help
generate more performant code in these specific places. In this PR,
I'm pushing those improvements back to the Imath project itself.

These new macros should be used with caution -- programmers
notoriously guess wrong and can hurt performance by using them
inappropriately.  "My intuition is that it will take this branch most
of the time" is not good enough.  Truly exceptional conditions (like
branches that handle very rarely encountered errors) can safely be
marked, but most ordinary control flow should not use these except in
response to clear indications from performance tests.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Copy link
Contributor

@meshula meshula left a comment

Choose a reason for hiding this comment

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

nice!

@cary-ilm
Copy link
Member

Since the builtin directive is named "expected", would it reduce the cognitive load a bit to call the new macro IMATH_EXPECTED()? Is there precedence for using the term "likely", or a reason for it to differ?

@lgritz
Copy link
Contributor Author

lgritz commented Nov 19, 2020

In C++20, it turns into a standard attribute, and the nomenclature will be likely/unlikely.

@lgritz lgritz merged commit 306e9b3 into AcademySoftwareFoundation:master Dec 5, 2020
@lgritz lgritz deleted the lg-likely branch December 5, 2020 23:27
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.

None yet

3 participants