Replace (un)likely macro with C++20 attribute · Issue #105 · SFTtech/nyan · GitHub
Skip to content

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

Open

Description

@heinezen

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

Activity

  1. added
    improvementimproves existing functionality
    c++involves C++ code
    good first issueSimple thing, suitable for newcomers
    on Jun 12, 2023
  2. SamantaTarun commented on Jun 13, 2023

    @SamantaTarun

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++involves C++ codegood first issueSimple thing, suitable for newcomersimprovementimproves existing functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions