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;
}
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
  • 2
    A good rule of thumb is to always prefer functions to macros. – NPE Nov 22 '14 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. – Jim Lewis Nov 22 '14 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 – technosaurus Nov 22 '14 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. – Mysticial Nov 24 '14 at 02:08

3 Answers3

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.

user2008934
  • 321
  • 2
  • 9
  • 1
    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? –  Nov 22 '14 at 18:01
  • 3
    @xiver77, the primary purpose of `inline` keyword is to relax the One Definition Rule. – ach Nov 22 '14 at 18:03
  • If compiler does inlining by itself, what's the point of writing `inline` by us? – Adam Stelmaszczyk Nov 22 '14 at 19:47
  • @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. – Mysticial Nov 24 '14 at 02:06
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)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
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.

Community
  • 1
  • 1
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • This doesn't answer the question, in what is an `intline` function less readable? – Jens Gustedt Nov 22 '14 at 19:10
  • @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. – Adam Stelmaszczyk Nov 22 '14 at 19:21
  • 1
    `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. – Jens Gustedt Nov 22 '14 at 20:02