2

Recently, I have learned the existence of "backtrace" function. This function allow one to retrieve, at some conditions, the callstack of an ELF running program compiled without debugging information.

It's perfect for me (I can't insert debugging symbol in production program), but for "backtrace" to work, there is (roughly) two condition :

  • Tell the linker to add extra information (by passing -rdynamic option).
  • Convert all "static" function to "non-static" function.

My worries is that if I fullfill this two condition, my program will be slower (because compiler can't optimise non-static function as he optimize static function ?). As far as I know, adding extra information with -rdynamic doesn't impact program's performance : it's just add a little weight to the ELF binary.

So here's my question :

What is the effect in term of running performance when all static function become non-static function ?

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
Tom's
  • 2,448
  • 10
  • 22
  • 'static' only affects scope/visiblity, no? – ThingyWotsit Apr 03 '17 at 09:08
  • I forgot to write it down, but as far as I know, yes, static affect scope/visibility. Where my doubt start is if 'static' ONLY affect scope/visibility. Someone I know say that may affect compiler optimisation too, but he can't back up what he say with source, and he's not really sure either. If there is a down-time at compilation-time because the compilator need to compute extra step in order to do the same optimisation, it doesn't bother me. but if there is a down-time at running time, it worth to think about it. – Tom's Apr 03 '17 at 09:10
  • Is this concerning C or C++? Possibly related: http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program If I'm not mistaken, `static` in C can be used for variables only, not for functions; what do you mean by _statc_ function? – Codor Apr 03 '17 at 09:11
  • It for [c] only. A static function is a function only visible inside the .c file where this function is declared. – Tom's Apr 03 '17 at 09:14

3 Answers3

5

Yes, your worries are correct: Declaring a function as static provides a good hint to the compiler, which it can turn into better optimization. The amount of speedup that you receive from static depends on your precise situation, though, so there's only truth in measurement (as always when it comes to performance).

The point about declaring a function as static is, that the compiler knows definitely, that it sees all the calling sites of a function. And if it sees, that the function is only called from one single place, it will generally always inline it, no matter how long it is. And the inlining may unlock further opportunities for optimization. This avoids the function call overhead, both in terms of size and speed. In this, static is actually a stronger hint than inline.

Of course, the effect on the performance depends on the frequency of calls to the static function. So, as I said, you need measurement to assess how much performance you gain from the static keyword.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
  • 1
    But extern functions can also be inlined in their translation units. The only difference with `static` is that for extern function the compiler always has to generate a non-inline body, while for static function the body might not be necessary (if all calls were inlined). But when it comes to calls made from the same translation unit, `static` does not "unlock" any additional inlining opportunities. Inside that translation unit these opportunities are exactly the same for static and extern functions. – AnT stands with Russia Apr 03 '17 at 09:23
  • @AnT That additional non-inlined copy is precisely the problem: It takes up space. When you have a large enough function and declare it as `inline`, the compiler may well decide to ignore your hint as the cost of adding that second copy is too great. (It's a heuristic decision that weights the costs of inlining against the costs of not inlining.) When the compiler sees that a second copy is not needed at all, the cost of inlining becomes precisely zero, allowing functions to be inlined which would not be inlined without the `static` keyword. And the inlining unlocks the further optimizations. – cmaster - reinstate monica Apr 03 '17 at 10:17
3

What is the effect in term of running performance when all static function become non-static function ?

The answer is "some", I think. The only way you can be sure what the performance issue is likely to be is to measure the performance of your program with and without static functions. The most obvious thing I can think of is that the optimiser might not be able to inline non static functions.

You actually don't need to make the static functions non static, the only issue will be the lack of symbolic names in the backtrace.

However, I think you have bigger problems. From your link to the man page:

Omission of the frame pointers (as implied by any of gcc(1)'s nonzero optimization levels) may cause these assumptions to be violated.

For backstrace to work reliably, it looks like you will have to compile without optimisation. That will certainly have a huge impact on performance. I think I'd manage without it.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • That little line escaped me the first time ... I guess I will leave all static keyword and all gcc's optimization, and if by chance I have a proper callstack, well, I will be happy. Thanks. – Tom's Apr 03 '17 at 09:23
  • Deugging with optimized code is misleading and painful anyway. – ThingyWotsit Apr 03 '17 at 09:41
-2

What is the effect in term of running performance when all static function become non-static function ?

Ans - None

A Static function means that function is only visible to that file only and cannot be called outside that file. i.e. it has file scope.

Static functions are typically used in large programs when you do not want your functions to clash with other people's functions. A static function ensures that you can define a function and use it inside your own file, and somebody else can also define the same static function name in other file.

If your code compiles after removing the static keyword, then you do not have two functions with the same name and it will work just like any normal function.

It will have no run time penalty.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • 1
    This is not true. `static` makes function inling a lot easier for the compiler, which can affect performance. Whether it's detectable difference or not, depends on the use case. – user694733 Apr 03 '17 at 09:15