tags:

views:

271

answers:

9
+2  Q: 

What is inlining?

I am referring to this discussion. I have never written any code in C or in C++ . I do not have any CS background. However I have been working as Java developer for 5 years and now I have decided to learn more about CS and do some catching up.

+4  A: 

http://en.wikipedia.org/wiki/Inlining

In computing, inline expansion, or inlining, is a compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing the size of the final program.

George
A: 

Basically, in C/C++, the compiler can inline functions, which means that rather than making a function call to do that operation, the code will be added to the calling function's block, so it will be as though it had never been a separate function call.

This will go into more detail: http://www.codersource.net/cpp_tutorial_inline_functions.html

James Black
A: 

Inlining refers to compile-time optimization where a small function of code will be injected into the calling function rather than require a separate call.

ifwdev
+5  A: 

When executing a given piece of code, whenever you call a standard function the execution time is slightly higher than dumping there the code contained into that function. Dumping every time the whole code contained in a function is on the other end unmainteinable because it obviously leads to a whole mess of duplication of code.

Inlining solves the performance and maintainability issue by letting you declare the function as inline (at least in C++), so that when you call that function - instead of having your app jumping around at runtime - the code in the inline function is injected at compile time every time that given function is called.

Downside of this is that - if you inline big functions which you call a lot of times - the size of your program may significantly increase (best practices suggest to do it only on small functions indeed).

JohnIdol
+2  A: 

As already mentioned in other answers, inlining comes with a cost. Usually this is considered small, however when actually measuring you might be surprised and learn that it might be greater than what you gain (so what other people say is true: do not optimize unless you have measured).

It is worth noting that in the Linux kernel they started un-inlining originally inlined functions some time ago because the cost was too high (larger functions consumed more of the cpu memory cache, and the resulting cache misses were more expensive than just calling the function that were intended to be inlined). See "Chapter 15: The inline disease" in doc/Documentation/CodingStyle for more details.

hlovdal
Good point and a nice example for how hardware developments can influence (in this case: invalidate) software techniques - memory bandwidth hasn't kept up with CPU speed, so anything that tries to trade memory consumption for CPU cycles can become counterproductive.
Michael Borgwardt
Yes, good point, and this is what always gets me about compiler design and api - what are we gaining if "small-tight code" is discussed and brought to design by persons who have no intuitive grasp of how machinery works. Computational devices are computing machinery, iow just machinery - nothing more. So if security through obscurity is popular, we can prove so by a proliferation of design discussions that think details are dirty.
Nicholas Jordan
A: 

The compiler optimization answers are correct. There is another usage, though - in refactoring, inlining refers to replacing a method call with the body of the method and then removing the method. See Inline Method. There are similar refactorings, such as Inline Class.

EDIT: Note that refactoring is done manually or with a tool; in either case it involves changing the source code.

TrueWill
+2  A: 

As a Java developer, you generally don't have to worry about method inlining. Java's Just-in-time compiler can and will do it automatically in most places where it makes sense.

IDEs like eclipse can have a feature that allows you to inline methods at the source code level - never do this for performance, only for code readability (e.g. when you realize that the method just calls one other method without adding anything useful itself).

Michael Borgwardt
Yes as a Java developer I never need to worry about it. But I am trying to learn here and was just wondering what ininling is.
Roger
A: 

In that discussion, Jon Skeet mentions Client jvm (hotspot) v Server jvm with the performance improvements available at run-time if the JIT ( just-in-time ) compiler is allowed to bring time-based enhancements. That is "how it's done" in Java.

Originally, small sections of code that were not called from many places would be "inlined" by the compiler, meaning that what was called a singleton would be placed directly in the instruction pointer code path, doing a function branch and return costs more processor power than just unrolling to loop or function call and placing the instructions "right there"

Today, Singleton is the subject of multi-page discussions and loop-unrolling as well as something like inlining are somewhat removed from their original context(s). You can read Dov Bulka's very informed work on the matter to get the C/C++ take on the matter. For Java, study of it's rich lib's in java.util would better serve your needs than study of inlining and deep compiler issues - you can get hung on entrenched embattled intramural warfare on data structures, which gloss over calls into 16-bit code, and go no end on your learning curve.

You can do instanceof in Java, that resembles a vf-table ( no heat folks, please ) but think of it as you have been writing in a strongly typed language - and now will be writing in a language where string can runaway easily poking around where it has no business. I recently tried to write code that constructed an Image in Java, doing that from C code. I soon found myself looking at the oxr table for strong encryption - that has nothing to do with the code I was writing.

How would you write a string class in C/C++ that has a small buffer for strings under 32 bytes and traps pointers so that they only operate on the string?

Not trying to tease you or anything, it's just a really good place to start rather than inlining and compiler science.

Nicholas Jordan
A: 

Inline functions are used typically in C++ header files not Java. A C++ header file usually does not contain implemented code and is considered an interface to the cpp file of the same name, which does usually contain the implemented code. It is legal to include an inline function in a header file, usually a small lightweight function. Inline functions do come at a cost, so they should not be large memory-intensive operations. For small routines the performance hit is minimal and they are more used for convenience.

BrentAllenParrish