Reputation: 27259
Is there a concept of inline functions in java, or its replaced something else? If there is, how is it used? I've heard that public
, static
and final
methods are the inline functions. Can we create our own inline function?
Upvotes: 127
Views: 153654
Reputation: 3186
There are inline-form functions in Java. You can write a function inline by using Functional Interfaces introduced at Java version 8.
Here is an example;
// with a String parameter and a String return type
Function<String, String> greet = name -> "Hello " + name;
Invocation;
String result = greet.apply("Abdullah");
// result: Hello Abdullah
If your inline function doesn't need a parameter but only a return type, then you should use a Supplier.
// with a String return type
Supplier<String> greet = () -> "Hello Abdullah";
Invocation;
String result = greet.get("Abdullah");
// result: Hello Abdullah
This is not the same type of inline function you would implement in C/C++ languages. In Java, JIT compiler determines which methods to be inlined(executed in a different form than typical method calls) by criterions like to be called frequently and etc.
Upvotes: 1
Reputation: 2833
As explained by other answers, inlining in Java is mostly left to JVM's discretion and small static
, final
or private
methods have higher chances of being inlined (although once it gathers enough profiling data, JVM will try to inline virtual methods as well: see the section regarding inlining in this article).
Additionally java
command has a few options that are intended to influence inlining decisions. Of these options the most precise is -XX:CompileCommand
with inline
command that asks JIT compiler to inline the specified method. If you have a long list of methods you wish to be inlined, you can put commands into a file: see -XX:CompileCommandFile
option.
See this answer for more generic options based on method usage&size.
If at some point you start to wonder what C1 and C2 are and how they influence inlining, see this answer.
Note however that the final inlining decision is always up to JVM and its compilers and they are free to ignore any such command-line options. Furthermore, the default values for size&usage based inlining are really well picked and in my personal practice it happens extremely rarely that manipulating inlining options brings any noticable performance gains in case of long-running processes.
See also this article for examples and logging of inlining decisions.
Upvotes: 0
Reputation: 10695
Java9 has an "Ahead of time" compiler that does several optimizations at compile-time, rather than runtime, which can be seen as inlining.
Upvotes: 0
Reputation: 3985
so, it seems there arent, but you can use this workaround using guava or an equivalent Function class implementation, because that class is extremely simple, ex.:
assert false : new com.google.common.base.Function<Void,String>(){
@Override public String apply(Void input) {
//your complex code go here
return "weird message";
}}.apply(null);
yes, this is dead code just to exemplify how to create a complex code block (within {}) to do something so specific that shouldnt bother us on creating any method for it, AKA inline!
Upvotes: 1
Reputation: 41
Well, there are methods could be called "inline" methods in java, but depending on the jvm. After compiling, if the method's machine code is less than 35 byte, it will be transferred to a inline method right away, if the method's machine code is less than 325 byte, it could be transferred into a inline method, depending on the jvm.
Upvotes: 4
Reputation: 59307
In Java, the optimizations are usually done at the JVM level. At runtime, the JVM perform some "complicated" analysis to determine which methods to inline. It can be aggressive in inlining, and the Hotspot JVM actually can inline non-final methods.
The java compilers almost never inline any method call (the JVM does all of that at runtime). They do inline compile time constants (e.g. final static primitive values). But not methods.
For more resources:
Article: The Java HotSpot Performance Engine: Method Inlining Example
Wiki: Inlining in OpenJDK, not fully populated but contains links to useful discussions.
Upvotes: 134
Reputation: 21
Real life example:
public class Control {
public static final long EXPIRED_ON = 1386082988202l;
public static final boolean isExpired() {
return (System.currentTimeMillis() > EXPIRED_ON);
}
}
Then in other classes, I can exit if the code has expired. If I reference the EXPIRED_ON variable from another class, the constant is inline to the byte code, making it very hard to track down all places in the code that checks the expiry date. However, if the other classes invoke the isExpired() method, the actual method is called, meaning a hacker could replace the isExpired method with another which always returns false.
I agree it would be very nice to force a compiler to inline the static final method to all classes which reference it. In that case, you need not even include the Control class, as it would not be needed at runtime.
From my research, this cannot be done. Perhaps some Obfuscator tools can do this, or, you could modify your build process to edit sources before compile.
As for proving if the method from the control class is placed inline to another class during compile, try running the other class without the Control class in the classpath.
Upvotes: 2
Reputation: 20614
No, there is no inline function in java. Yes, you can use a public static method anywhere in the code when placed in a public class. The java compiler may do inline expansion on a static or final method, but that is not guaranteed.
Typically such code optimizations are done by the compiler in combination with the JVM/JIT/HotSpot for code segments used very often. Also other optimization concepts like register declaration of parameters are not known in java.
Optimizations cannot be forced by declaration in java, but done by compiler and JIT. In many other languages these declarations are often only compiler hints (you can declare more register parameters than the processor has, the rest is ignored).
Declaring java methods static, final or private are also hints for the compiler. You should use it, but no garantees. Java performance is dynamic, not static. First call to a system is always slow because of class loading. Next calls are faster, but depending on memory and runtime the most common calls are optimized withinthe running system, so a server may become faster during runtime!
Upvotes: 20
Reputation: 116197
Java does not provide a way to manually suggest that a method should be inlined. As @notnoop says in the comments, the inlining is typically done by the JVM at execution time.
Upvotes: 15
Reputation: 3610
What you said above is correct. Sometimes final methods are created as inline, but there is no other way to explicitly create an inline function in java.
Upvotes: 5