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

Replace (un)likely macro with C++20 attribute #105

Open
heinezen opened this issue Jun 12, 2023 · 1 comment
Open

Replace (un)likely macro with C++20 attribute #105

heinezen opened this issue Jun 12, 2023 · 1 comment
Labels
c++ involves C++ code good first issue Simple thing, suitable for newcomers improvement improves existing functionality

Comments

@heinezen
Copy link
Member

heinezen commented Jun 12, 2023

We currently use gcc's __builtin_expect() macro aliased as unlikely and likely for static branch prediction. Primarily, this optimizes checks for parser errors. This optimizes performance for code paths which are only taken under very specific circumstances.

Example:

if (unlikely(not this->value.exists())) {
    throw InternalError{"fetched nonexisting value of member"};
}

Since C++20, the standard supports attributes that do the same thing. Additionally, they are compiler agnostic, so they should also work in clang or msvc. They should also be more readable since they don't directly wrap the condition.

Basically the code gets changed to this:

if (not this->value.exists()) [[unlikely]] {
    throw InternalError{"fetched nonexisting value of member"};
}

If you want to discover the codebase or really like mundane tasks, then this is an issue for you :)


Also take note of the same issue in the openage repository: SFTtech/openage#1514

@heinezen heinezen added improvement improves existing functionality c++ involves C++ code good first issue Simple thing, suitable for newcomers labels Jun 12, 2023
@SamantaTarun
Copy link

I'd like to work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ involves C++ code good first issue Simple thing, suitable for newcomers improvement improves existing functionality
Projects
None yet
Development

No branches or pull requests

2 participants