Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in inlining functions by compiler or linker?

I am wondering whether there is any difference between inlining functions on a linker level or compiler level in terms of execution speed?

e.g. if I have all my functions in .cpp files and rely on the linker to do inlining, will this inlining potentially be less efficient than say defining some functions in the headers for selected inlining on the compiler level or unity builds without any linking and all inlining done by the compiler?

If the linker is just as efficient, why would one then still bother inlining functions explicitly on the compiler level? Is that just for convenience, say there is just a one line constructor hence one can't be bothered with a .cpp file?

I suppose this might depend on the compiler, in which case I would be most interested in Visual C++ (Windows) and gcc (Linux).

Thanks

like image 800
Cookie Avatar asked May 10 '11 10:05

Cookie


1 Answers

The general rule is all else being equal the closer to execution (compiling->linking->(maybe JIT)->execution) the optimization is done the more data the optimizer has and the better optimization it can perform. So unless the optimizer is dumb you should expect better results when inlining is done by the linker - the linker will know more about the invokation context and do better optimization.

like image 121
sharptooth Avatar answered Oct 13 '22 06:10

sharptooth