Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use `inline` on heavily used functions?

Tags:

c++

inline

I have a class cnVector that represents a point in 3 dimensional space. Its operators + - * / are used intensively.
Their implementation is very short:

cnVector cnVector::operator + (const cnVector& v) const {
    return cnVector(
        x + v.x,
        y + v.y,
        z + v.z );
}

My question is, because this function is very short, should I inline it although its intensive use? or would it generate too much code when using it that much?

like image 991
Niklas R Avatar asked Oct 23 '11 17:10

Niklas R


People also ask

When we should not use inline function?

5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.

Why large functions should not be made inline?

inline functions might make it slower: Too much inlining might cause code bloat, which might cause “thrashing” on demand-paged virtual-memory systems. In other words, if the executable size is too big, the system might spend most of its time going out to disk to fetch the next chunk of code.

When should we use inline and when shouldn't give an example?

One should use the inline function qualifier only when the function code is small. If the functions are larger you should prefer the normal functions since the saving in memory space is worth the comparatively small sacrifice in execution speed.

Why would you want to use inline?

Answer. An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.


4 Answers

Yes you probably should. The good use case for the inline keyword in c++ is: small functions, heavily used.

See also http://msdn.microsoft.com/en-us/library/1w2887zk%28v=vs.80%29.aspx

like image 137
log0 Avatar answered Sep 25 '22 06:09

log0


Remember that using inline is never a guarantee, it just gives a hint to the compiler. I doubt inlining will actually increase executable size a lot, the function is very small in itself.
Calling the function is almost the same size as the function itself.

like image 20
paul23 Avatar answered Sep 22 '22 06:09

paul23


Apply inline to all functions that you define in your header on namespace scope to avoid breaking the One Definition Rule. This, by the way, is completely unrelated to inlining, despite the keyword name. (Or put them inside an anonymous namespace.)

inline also gives a hint to the compiler to inline calls to said function, but as the comments have pointed out the compiler is quite capable of figuring that out by itself so the keyword isn’t really needed for that.

like image 40
Konrad Rudolph Avatar answered Sep 21 '22 06:09

Konrad Rudolph


The compiler is perfectly capable of making a decision as to whether to inline a function or not depending on the chosen optimization profile.

You should inline a function if the compiler doesn't, and profiling with a realistic data set shows you're spending a significant amount of time in the function, the algorithm using said function is an efficient one, and if inlining it shows a speed improvement in a benchmark with said data set.

like image 39
millimoose Avatar answered Sep 22 '22 06:09

millimoose