Why does a simple C++ program generate so many branch commands? Using perf on Linux

Viewed 111

I must be doing something stupid or using perf incorrectly ?

#include <iostream>

int main()
{
        return 0;
}

Compile command (Using g++-9.2.1)

g++ -std=c++17 -Wall -Wextra -pedantic -O3 Source.cpp -o prog

Following the tutorial

stat Run a command and gather performance counter statistics

I attempted

perf stat ./prog

And in the output

       560,957      branches                  #  303.607 M/sec
        16,181      branch-misses             #    2.88% of all branches

The question is why? should I "clean" the registers before running this command? is this normal?

1 Answers

About 80% of the branching comes from dynamic linking. Files need to be opened and then the dynamic libraries need to be parsed. This requires a lot of decision making as the contents of the file have to be tested to see what their format is, what sections they have, and so on.

Most of the remaining 20% is precisely that same kind of logic operating on the executable. It has a complex format and code has to parse that format to figure out what sections it has, find the endings of each section, and decide how to lay them out in memory before the program begins executing.

Related