Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

I’ve worked in 1 company where we had tools that would re-organize the code to make the common code path be the fall through as much as possible

This was done before linking, based on analysis of the common paths from a running program

What this also did - which was the important bit at the time - was move all the error code to pages that wouldn’t be loaded for most users

So it moved error handling out of common code pages and changed branch instructions to mostly fall through

This was many years ago - do similar tools exist in the OSS community?



All good compilers do this today. They use heuristics to guess what the likely outcome of branches, but you can provide them specific information with profile guided optimization, or with manual branch hits (the __builtin_expect I used in the example above is one such hint).

Similarly, you can annotate certain functions or paths "hot" or "cold", and compilers even understand that functions like abort() are cold (they can be called at most once, after all) and hence paths leading up to them are also cold.

Similarly, JIT compilers use the runtime observed branching behavior to organize branches so that fall-through is the common case.


Grab a look at the FDO and AutoFDO sections from GCC's wiki:

https://gcc.gnu.org/wiki/AutoFDO/Tutorial

You'll also find it's referred to as Profile Guided Optimization, there's docs here for clang: https://clang.llvm.org/docs/UsersManual.html#profile-guided-...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: