I'm working on an iOS app using C and Objective-C, and I want to write a very small piece of code that will be executed thousands of times from more than one place. Is it safe to make this an inline
function and be sure that it will always be expanded (I won't ever be taking its address) or should I make it a macro? The code is small and it will be executed very frequently, so I'd like to make sure I won't end up with thousands of function calls for it, but still I'd like the type safety of the function approach if possible...

- 61,078
- 31
- 152
- 193
-
2Yes - use an inline function - it's just as efficient as a macro and a lot more robust. – Paul R Sep 13 '12 at 22:42
-
@PaulR, so I can be assured that it will never be called as a function? From what I understand, `inline` is just a hint, and the compiler can choose to ignore it. – rid Sep 13 '12 at 22:46
-
With gcc *et al* you can use `__attribute__ ((always_inline))` to force inlining, but this shouldn't be necessary if it's just a small function. – Paul R Sep 13 '12 at 22:49
-
Or use `gcc -Winline` to alert you if it can't be inlined. (Though it sounds like OP isn't using gcc.) – ron rothman Sep 13 '12 at 22:50
-
Won't most sensible compilers do this automatically as one of the speed optimisations? Also inline is not a guarantee but a hint to many compilers – hookenz Sep 13 '12 at 22:54
-
Also: I suggest you profile your code before you spend very much time worrying about line-level optimizations. "Premature optimization is the root of all evil." – ron rothman Sep 13 '12 at 23:03
3 Answers
If you want to be sure that a function is inlined, make it "extern inline" (this is a GNU-C feature). Such functions are only used for inlining; the compiler will never generate a "real" function for it. Thus, if the inlining fails, you should be getting linker errors. I assume clang has "inherited" this feature.
In general, always use inline instead of macros, if possible. There's a reason why many C-compilers had it for ages, and C++ finally added it as a core feature; it makes things a lot safer and reliable to use. There are still things that need macros, but those are few and far between.

- 9,954
- 24
- 23
Yes, you should use an inline function over a macro.
The performance will be identical to a macro (the code is inline, after all) and you'll get type safety as well.
N.B., this assumes that your function is simple enough for the compiler to inline. gcc's -Winline
option warns if this isn't the case; not sure what flags do the same on your platform.
Also see this post for cases when you might prefer a macro (e.g., deferred evaluation)--but based on your question it sounds like inline function is the clear choice.

- 1
- 1

- 17,348
- 7
- 41
- 43
I may be wrong, but I understand a compiler can only inline functions which are in the same source file. If your inline function is in file A and you're trying to use it elsewhere, it cannot be inlined, unless the linker does link-time optimization.
This is because the compiler only compiles one C file at a time into one object file. It cannot obtain the inlined function from another object file, because firstly, it may not yet have been compiled and secondly, it wouldn't know which object file to look for anyway.