Scala 中的@inline 注释真的有助于提高性能吗? [英] Does the @inline annotation in Scala really help performance?

查看:51
本文介绍了Scala 中的@inline 注释真的有助于提高性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者它是否只是为了让 JIT 自动处理的事情而使代码变得混乱?

Or does it just clutter up the code for something the JIT would take care of automatically anyway?

推荐答案

我还没有找到可以提高性能的案例,而且我已经尝试了很多不同的地方.JVM 似乎在可能的情况下非常擅长内联,即使您在 Scala 中要求 @inline,它也不能总是这样做(有时我注意到它甚至在我认为应该这样做的时候也没有)能够).

I have yet to find a case where it improves performance, and I've tried in quite a few different spots. The JVM seems to be quite good at inlining when it's possible, and even if you ask for @inline in Scala, it can't always do it (and sometimes I've noticed that it doesn't even when I think it ought to be able to).

您希望看到字节码差异的地方是这样的:

The place where you expect to see a bytecode difference is in something like this:

object InlineExample {
  final class C(val i: Int) {
    @inline def t2 = i*2
    @inline def t4 = t2*2
  }
  final class D(val i: Int) {
    def t2 = i*2
    def t4 = t2*2
  }
}

当使用 -optimise 编译时.您确实看到了差异,但它通常不会运行得更快,因为 JIT 编译器可以注意到相同的优化适用于 D.

when compiled with -optimise. And you do see the difference, but it generally doesn't run any faster since the JIT compiler can notice that the same optimizations apply to D.

因此在优化的最后阶段可能值得一试,但如果不检查它是否对性能产生影响,我不会经常这样做.

So it may be worth a try in the final stages of optimization, but I wouldn't bother doing it routinely without checking to see if it makes a difference in performance.

这篇关于Scala 中的@inline 注释真的有助于提高性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆