I was told that the implementation for C inline functions in different platforms (e.g. Mips vs x86) is a little different. For example, inline function for one of them (Mips or x86) still allocates stack and thus not so efficient. Is it true? What are all the difference? Thanks
-
How a compiler decides to implement particular optimizations is not set by the standard - so you can expect differences between different compilers on the same platform, and between the same compiler on different platforms. I don't think it is possible to give a "definitive" answer. My I ask why you are asking? Usually, "micro optimization" is more effort than it is worth. Do you have a case where that might not be true?Floris– Floris2014-04-24 00:03:43 +00:00Commented Apr 24, 2014 at 0:03
-
I need to implement set/get methods to access the fields of the structures, pretty like what we do in OO programming. In the consideration of the efficiency, inline function is my last choice. But I was told that inline function may not be so efficient in x86 platform. I'm not sure if that's true and I'm wondering what are all the difference...Hanks– Hanks2014-04-24 00:16:05 +00:00Commented Apr 24, 2014 at 0:16
2 Answers
It is not guaranteed that every function will be inlined when you declare it inline (it is just a suggestion to the compiler). Hence for any architecture the functions that aren't inlined will be treated as normal functions, and hence the push & pop from the stack when you call them (like any normal non-inlined function)
And it is pretty much dependent upon how your compiler is configured. For eg, GCC has flags like:
-finline-functions
Consider all functions for inlining, even if they are not declared inline. The compiler heuristically decides which functions are worth integrating in this way.
-finline-limit=n
By default, GCC limits the size of functions that can be inlined. This flag allows coarse control of this limit. n is the size of functions that can be inlined in number of pseudo instructions.
--param name=value
In some places, GCC uses various constants to control the amount of optimization that is done. For example, GCC does not inline functions that contain more than a certain number of instructions. You can control some of these constants on the command line using the --param option.
7 Comments
Assuming the compiler does decide to inline the function, the result is expected to be similar to what you'd get if you had pasted the function body in place of the call, renaming variables as needed to avoid conflicts.
If the inline function has local variables, it will often be necessary to allocate stack space for them.
The efficiency gained from inlining is mainly expected to be the overhead of the calling and return sequences. Since this is not very large, inlining is usually only beneficial for very small functions.
Optimizing compilers can calculate this overhead ratio, and when it's large they can inline the function automatically, the inline qualifier isn't needed.