Skip to content
0:00 / 0:00

#branchlessprogramming#cpubranchpredictor#performanceoptimization

Branchless Programming in C++ - Fedor Pikus - CppCon 2021

CppCon

189K subscribers • 101 videos on Hallway • Conference channel

YouTube
  • Recommended by 1 person: 1 on Lobsters.
  • Presented by Fedor Pikus
  • Linked in the descriptionCppCon/CppCon2021

Advanced. Deep dive for people who know the field.

200K views4 years ago1 recommendation

Systems programmingProgramming languagesC++Performance and optimizationCPUs and computer architecture

Fedor Pikus explains how conditional branches affect CPU performance and demonstrates how to write branchless C++ code.

cppcon.org/ github.com/CppCon/CppCon2021 --- Have you ever written code like this: void f(bool b, long x, long& s) { if (b) s += x; } Probably you have. Would you like me to tell you how much performance you left on the table? With a small change, that function could be made 2.5 times faster. What about this code: if (a[i] && b[i]) do_something(); else do_something_else(); Would you believe me if I told you that, under some not-so-exotic conditions, this line runs four times slower than it could be? It’s true, and I’m going to show you when and why. This presentation will explain how modern CPUs handle computations to ensure that the hardware is used efficiently (which is how you get high performance from the processor). We will then learn how conditional code disrupts the ideal flow of computations and the countermeasures employed by the CPU designers to retain good performance in the presence of such code. Sometimes these measures are sufficient, often with the help of the compiler. But when they aren’t, it is up to the programmer to recover lost performance by coding with fewer branches. --- Fedor Pikus Fedor G Pikus is a Chief Engineering Scientist in the Design to Silicon division of Mentor Graphics Corp (Siemens business). His earlier positions included a Senior Software Engineer at Google and a Chief Software Architect for Calibre PERC, LVS, DFM at Mentor Graphics. He joined Mentor Graphics in 1998 when he made a switch from academic research in computational physics to the software industry. Fedor is a recognized expert on high-performance computing and C++, he presented his works at CPPCon, SD West, DesignCon, in Software Development Journal, and is also an O’Reilly author. His responsibilities as a Chief Scientist include planning the long-term technical direction of Calibre products, directing and training the engineers who work on these products, design, and architecture of the software, and research in the new design and software technologies. Fedor has over 25 patents and over 100 papers and conference presentations on physics, EDA, software design, and C++ language. --- Videos Filmed & Edited by Bash Films: BashFilms.com YouTube Channel Managed by Digital Medium Ltd events.digital-medium.co.uk *--*

Why it's here

1 person recommends this

Moments people linked to

dbremneron Lobsters • Recommends

Branchless Programming in C++ (2021)

u/DeeBoFour20in r/C_Programming • Mentions

On “Is using a function array more efficient than an if else statement?”

It can sometimes be beneficial. It's called branchless programming. I'd recommend watching this lecture from cppcon if you want to learn more: [video The takeaway I got from the video is that it's only useful if the CPU's branch predictor cannot accurately predict the branch. Also, make sure to benchmark to be sure the optimization helps. In this simple example, I would just use an if statement. Lastly, I think `functions(strcmrgv[1, "bye") == 0)]();` is undefined behavior. The compiler will evaluate false to 0 but true can be any non-zero value. Maybe it works out to always be 1 on your platform but I think it can technically be anything other than 0.

u/DeeBoFour20in r/C_Programming • Mentions

On “Looking for a clean way to use a Lookup Table with chars”

What you're trying to do is called branchless programming. There's a good talk about the subject at cppcon on youtube. At the end, the speaker warns against doing this with function pointers. Link with timestamp here: [video You really need to measure to be sure (as is usually the case with optimizations) but it's likely that this won't actually help you. It also probably makes your code harder to read and maintain which is maybe a worthwhile tradeoff if the performance gain is significant but if it's not, you just lose all around which is why you need to measure.
2View on Reddit

u/morglodin r/ProgrammingLanguages • Mentions

On “Stack VM in Rust: Instructions as enum?”

…ings and convert it automatically, but for some compilcated thing it may leave branch. but you got the idea, less branches in hot code = better (or use some special \\[likely\\] compiler attributes) you actually should not care about it unless you think that "encoding/decoding" may be faster than big instruction sizes very good talk about it: [video…