In C or C++ you can declare a function to be static
if it is only used in a single Translation Unit (i.e. file), for example:
static int square(int a) {
return a * a;
}
This should allow the compiler to optimise it better. It's a bit ugly though (edit: opinions on the ugliness may vary; this question isn't about that), and I'd rather not have to do it if I don't need to. How much difference does it really make?
- Does it affect compile times?
- What is the overhead for calling a non-
static
function that could bestatic
? - Can the linker recognise that it is only used in one file and automatically turn it back into a
static
function (maybe with-flto
or ThinLTO?).
Edit: Answers for any achitecture & compiler are welcome, though obviously more common ones are more helpful (Clang, x86-64).