I am testing a simple code which calculates Mandelbrot fractal. I have been checking its performance depending on the number of iterations in the function
As other people already pointed out -fPIC
forces GCC to disable many optimizations e.g. inlining and cloning. I'd like to point out several ways to overcome this:
-fPIC
with -fPIE
if you are compiling main executable (not libraries) as this allows compiler to assume that interposition is not possible;-fvisibility=hidden
and __attribute__((visibility("default")))
to export only necessary functions from the library and hide the rest; this would allow GCC to optimize hidden functions more aggressively;__attribute__((alias ("__f")));
) to refer to library functions from within the library; this would again untie GCC's hands-fno-semantic-interposition
flag that was added in recent GCC versionsIt's interesting to note that Clang is different from GCC as it allows all optimizations by default regardless of -fPIC
(can be overridden with -fsemantic-interposition
to obtain GCC-like behavior).