Code Monkey home page Code Monkey logo

Comments (3)

johnezang avatar johnezang commented on September 24, 2024

The JK_EXPECT_T and JK_EXPECT_F are preprocessor macros that are used to abstract a feature often provided by optimizing compilers. Specifically, it allows you to give the optimizer a hint as to which condition is most likely to occur: the true condition, or the false condition. The __builtin_expect() "function" has the following behavior:

  • __builtin_expect(x, y), where x is 0, returns a result of 0.
  • __builtin_expect(x, y), where x is != 0, returns a result of 1.

In either case of x, y does not change the result that is "returned". In other words, __builtin_expect() effectively "does nothing".

However, internally the optimizer makes a note of the value of y. When a modern CPU encounters a conditional branch, it may not have all the information needed at that point in time to know exactly which branch it should follow- the true or false side (this is due to pipelining, cache and memory latencies, etc). Modern CPU's will typically being to speculatively execute one of the paths, and if it later determines that it picked the wrong one, it requires an expensive flush of the CPU state to undo all the speculatively execute instructions, and then restart instruction execution on the correct path.

By using __builtin_expect(), you can help the CPU pick the correct path to follow "most of the time", which translates in to better performance.

from jsonkit.

jasongregori avatar jasongregori commented on September 24, 2024

Thanks John

from jsonkit.

steipete avatar steipete commented on September 24, 2024

If i get this right, clang/llvm doesn't use these hints?

from jsonkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.