1

Would it be better to define the below functions as macro or inline functions? Or is it better to use normal functions and trust the compiler, to maintain (in my opinion) better readability?

typedef unsigned char byte; void set_size_mark(byte size_mark, byte *ptr) { *ptr += size_mark << 1; } byte get_size_mark(byte *ptr) { return *ptr >> 1; } 
4
  • 2
    A good rule of thumb is to always prefer functions to macros. Commented Nov 22, 2014 at 17:43
  • If you have a particular compiler, OS, and architecture in mind, you could write a simple benchmark to see whether your hand optimizations beat the compiler. Commented Nov 22, 2014 at 17:56
  • To expand on NPE use a macro only if the process needs to work with different type values. For example #define ARRAYLEN(x) (sizeof(x)/sizeof(*x)) would be difficult to do as a function.... or when you need something to happen at compile time, such as a compile time assert Commented Nov 22, 2014 at 20:44
  • I've found that sometimes a compiler will sometimes refuse to inline even simple functions even if they are marked inline. They tend to do that if the function is called many times in the same place (which is also when it is the more important to inline!) That's what force inline is for. The exact syntax various with compilers. Commented Nov 24, 2014 at 2:08

3 Answers 3

2

You should inline those. The compiler would almost certainly do this itself anyway.

The code is one line and is unlikely to create code bloat, so it is probably more efficient to inline. The compiler knows this, however, if this is incorrect I think the compiler can ignore the inline keyword.

Generally speaking you should avoid macros - They cause many unexpected problems and are usually just as bad as inline functions while being far harder to use. There are uses for macros, but this is definitely not the appropriate place for one.

Sign up to request clarification or add additional context in comments.

4 Comments

My question is that if the compiler knows whether a function is more efficient to be inlined, what is the point of explicitly stating inline?
@xiver77, the primary purpose of inline keyword is to relax the One Definition Rule.
If compiler does inlining by itself, what's the point of writing inline by us?
@AdamStelmaszczyk They didn't always do it. In the past, GCC did not inline functions that were not marked inline unless you turned on a compiler flag.
1

You probably should declare these as static inline functions and define them in a header file.

(Alternatively, enable link-time optimization by compiling and linking with gcc -flto -O2, using a recent GCC)

Comments

0

First of all, definitely not macros. Now, we have to choose between:

  1. Never writing inline.
  2. Writing inline sometimes.

GCC in the current version will decide for you whether the function should be inlined or not. So, why bother? That's why I would go for option 1. Shorter code and less time spent by writer(s) and reader(s) of the code.

3 Comments

This doesn't answer the question, in what is an intline function less readable?
@JensGustedt OP asks whether to believe the compiler and use normal function or not - I'm pointing to believe and don't do any extra work. In general, if writing something doesn't give me anything, why waste time of writer and reader? :) It clutters up the code. Plus, reader and writer have to think whether this function should be marked inline or not... Why is it marked and the other one not... Maybe it should... So, my advice is just to not write inline and leave the choice to compiler.
inline needs more than just marking it as such, you'd have to put it inside the header file and mark it with inline. Only then the compiler really has a choice. Otherwise it can only inline it in the same compilation unit were the definition is actually given.