java中有内联函数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2096361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:30:25  来源:igfitidea点击:

Are there inline functions in java?

javainline-code

提问by giri

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, staticand finalmethods are the inline functions. Can we create our own inline function?

java中是否有内联函数的概念,或者它取代了其他东西?如果有,如何使用?我听说public,staticfinalmethods 是内联函数。我们可以创建自己的内联函数吗?

回答by GreenieMeanie

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.

你上面说的都是对的。有时 final 方法被创建为内联,但没有其他方法可以在 java 中显式创建内联函数。

回答by notnoop

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.

在 Java 中,优化通常在 JVM 级别完成。在运行时,JVM 执行一些“复杂”的分析来确定要内联的方法。它可以积极地进行内联,而 Hotspot JVM 实际上可以内联非 final 方法。

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.

java 编译器几乎从不内联任何方法调用(JVM 在运行时完成所有这些)。它们执行内联编译时常量(例如最终静态原始值)。但不是方法。

For more resources:

更多资源:

  1. Article: The Java HotSpot Performance Engine: Method Inlining Example

  2. Wiki: Inlining in OpenJDK, not fully populated but contains links to useful discussions.

  1. 文章:Java HotSpot 性能引擎:方法内联示例

  2. Wiki:在 OpenJDK 中内联,未完全填充但包含有用讨论的链接。

回答by Thomas Owens

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.

Java 没有提供手动建议应该内联方法的方法。正如@notnoop 在评论中所说,内联通常由 JVM 在执行时完成。

回答by Arne Burmeister

No, there is no inline functionin 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 expansionon a static or final method, but that is not guaranteed.

不,Java 中没有内联函数。是的,当放置在公共类中时,您可以在代码中的任何位置使用公共静态方法。java 编译器可能会对静态或最终方法进行内联扩展,但这并不能保证。

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.

通常,此类代码优化由编译器结合 JVM/JIT/HotSpot 完成,用于经常使用的代码段。在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).

优化不能通过 java 中的声明来强制执行,而是由编译器和 JIT 来完成。在许多其他语言中,这些声明通常只是编译器提示(您可以声明比处理器更多的寄存器参数,其余的将被忽略)。

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!

声明 java 方法 static、final 或 private 也是编译器的提示。你应该使用它,但没有保证。Java 性能是动态的,而不是静态的。由于类加载,首次调用系统总是很慢。下一次调用会更快,但根据内存和运行时间,最常见的调用会在运行系统内进行优化,因此服务器在运行时可能会变得更快!

回答by Billy Guthrie

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.

然后在其他课程中,如果代码已过期,我可以退出。如果我从另一个类中引用 EXPIRED_ON 变量,则该常量内联到字节码中,因此很难跟踪检查到期日期的代码中的所有位置。但是,如果其他类调用 isExpired() 方法,则调用实际方法,这意味着黑客可以用另一个总是返回 false 的方法替换 isExpired 方法。

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.

我同意强制编译器将静态最终方法内联到引用它的所有类会非常好。在这种情况下,您甚至不需要包含 Control 类,因为在运行时不需要它。

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.

至于证明在编译过程中是否将控件类的方法内联到另一个类中,请尝试在类路径中没有Control类的情况下运行另一个类。

回答by Ethan

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.

好吧,在java中有一些方法可以称为“内联”方法,但这取决于jvm。编译后,如果方法的机器码小于35字节,会立即转为内联方法,如果方法的机器码小于325字节,可以转为内联方法,具体取决于jvm。

回答by Aquarius Power

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.:

因此,似乎没有,但是您可以使用番石榴或等效的 Function 类实现来使用此解决方法,因为该类非常简单,例如:

    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!

是的,这是死代码只是为了举例说明如何创建一个复杂的代码块(在 {} 内)来做一些如此具体的事情,不应该打扰我们为它创建任何方法,也就是内联!

回答by tkruse

Java9 has an "Ahead of time" compiler that does several optimizations at compile-time, rather than runtime, which can be seen as inlining.

Java9 有一个“Ahead of time”编译器,它在编译时进行多项优化,而不是运行时,这可以看作是内联。