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开发者_开发百科.
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).
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.
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).
Norman Maurer explains at his blog JVM and JIT inline functionality like that
Inlining is a technique that will basically just "inline" one method in another and so get rid of a method invocation. The JIT automatically detects "hot" methods and try to inline them for you. A method is considered "hot" if it was executed more the X times, where X is a threshold that can be configured using a JVM flag when start up java (10000 is the default). This is needed as inlining all methods would do more harm then anything else, because of the enormous produced byte-code. Beside this the JIT may "revert" previous inlined code when an optimization turns out to be wrong at a later state. Remember the JIT stands for Just in Time and so optimize (which includes inlining but also other things) while execute your code.
Also with a warning
But even if the JVM consider a method to be "hot" it may not inline it. But why? One of the most likely reasons is that it is just to big to get inlined.
And you can find a very simple code example for inlining a Java code at Eva Andreasson's Java World Post. You can find the related part of post at below.
Many optimizations try to eliminate machine-level jump instructions (e.g., JMP for x86 architectures). A jump instruction changes the instruction pointer register and thereby transfers the execution flow. This is an expensive operation relative to other ASSEMBLY instructions, which is why it is a common target to reduce or eliminate. A very useful and well-known optimization that targets this is called inlining. Since jumping is expensive, it can be helpful to inline many frequent calls to small methods, with different entry addresses, into the calling function. The Java code in Listings 3 through 5 exemplifies the benefits of inlining.
Listing 3. Caller method
int whenToEvaluateZing(int y) {
return daysLeft(y) + daysLeft(0) + daysLeft(y+1);
}
Listing 4. Called method
int daysLeft(int x){
if (x == 0)
return 0;
else
return x - 1;
}
Listing 5. Inlined method
int whenToEvaluateZing(int y){
int temp = 0;
if(y == 0) temp += 0; else temp += y - 1;
if(0 == 0) temp += 0; else temp += 0 - 1;
if(y+1 == 0) temp += 0; else temp += (y + 1) - 1;
return temp;
}
In Listings 3 through 5 the calling method makes three calls to a small method, which we assume for this example's sake is more beneficial to inline than to jump to three times.
It might not make much difference to inline a method that is called rarely, but inlining a so-called "hot" method that is frequently called could mean a huge difference in performance. Inlining also frequently makes way for further optimizations, as shown in Listing 6.
Listing 6. After inlining, more optimizations can be applied
int whenToEvaluateZing(int y){
if(y == 0) return y;
else if (y == -1) return y - 1;
else return y + y - 1;
}
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/process/coding-style.rst for more details.
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.
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
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.
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.
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.
精彩评论