views:

875

answers:

7

Visual Studio includes support for __forceinline. The Microsoft Visual Studio 2005 documentation states:

The __forceinline keyword overrides the cost/benefit analysis and relies on the judgment of the programmer instead.

This raises the question: When is the compiler's cost/benefit analysis wrong? And, how am I supposed to know that it's wrong?

In what scenario is it assumed that I know better than my compiler on this issue?

+2  A: 

The only way to be sure is to measure performance with and without. Unless you are writing highly performance critical code, this will usually be unnecessary.

Greg Hewgill
+15  A: 

You know better than the compiler only when your profiling data tells you so.

Don Neufeld
A: 

There are several situations where the compiler is not able to determine categorically whether it is appropriate or beneficial to inline a function. Inlining may involve trade-off's that the compiler is unwilling to make, but you are (e.g,, code bloat).

In general, modern compilers are actually pretty good at making this decision.

+2  A: 

The compiler is making its decisions based on static code analysis, whereas if you profile as don says, you are carrying out a dynamic analysis that can be much farther reaching. The number of calls to a specific piece of code is often largely determined by the context in which it is used, e.g. the data. Profiling a typical set of use cases will do this. Personally, I gather this information by enabling profiling on my automated regression tests. In addition to forcing inlines, I have unrolled loops and carried out other manual optimizations on the basis of such data, to good effect. It is also imperative to profile again afterwards, as sometimes your best efforts can actually lead to decreased performance. Again, automation makes this a lot less painful.

More often than not though, in my experience, tweaking alogorithms gives much better results than straight code optimization.

Shane MacLaughlin
+2  A: 

I've developed software for limited resource devices for 9 years or so and the only time I've ever seen the need to use __forceinline was in a tight loop where a camera driver needed to copy pixel data from a capture buffer to the device screen. There we could clearly see that the cost of a specific function call really hogged the overlay drawing performance.

Johann Gerell
+8  A: 

The one place I am using it is licence verification.

One important factor to protect against easy* cracking is to verify being licenced in multiple places rather than only one, and you don't want these places to be the same function call.


*) Please don't turn this in a discussion that everything can be cracked - I know. Also, this alone does not help much.

peterchen
+1, clever thinking and an idea I'll borrow.
Shane MacLaughlin
+1  A: 

The inline directive will be totally of no use when used for functions which are:

recursive, long, composed of loops,

If you want to force this decision using __forceinline