Java 优化:仅字节码与 JIT

发布于 2024-10-14 17:44:57 字数 164 浏览 5 评论 0原文

为 Android 设备开发游戏,我需要针对根本没有 JIT、仅依赖字节码优化的设备。我想知道这些优化的集合是否为空...

实际上,java编译器(最难的,javac,不是JIT)是否会进行任何优化,例如将a / 4转换为a >> > 2 ?或者说每一次优化都是 JIT 的工作?

Developping games for android devices, I need to target devices that have no JIT at all, and only rely on bytecode optimizations. I wonder if the set of these optimization is empty or not...

Actually, does the java compiler (the hard one, javac, not a JIT) makes any optimization like transforming a / 4 into a >> 2 ? Or is every optimization a work for the JIT ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无敌元气妹 2024-10-21 17:44:57

标准 Java 编译器会进行一些优化,但将大部分优化留给 JIT。

JIT 确切地知道程序正在哪个处理器上运行,并且还可以访问运行时信息,因此它可以比 Java 编译器提前进行更多的优化。此外,提前进行广泛的优化可能会在一定程度上“混淆”字节代码,从而使 JIT 更难对其进行优化。

我不知道 Google 编译器在将 Java 字节代码转换为 Dalvik 代码时会做什么 - 它可能会进行更广泛的优化。

也许这个工具对您有用:Dalvik Optimization and Verification With dexopt

顺便说一下,你提到的例子并不总是有效;将 a / 4 转换为 a >>> 2 不保证您的程序在任何处理器上运行得更快。我曾经在某处读过一篇文章(抱歉,现在找不到......),它解释了在(我认为)现代 x86 处理器上, a >> 2 甚至可能比 a / 4 慢。

无论如何,不​​要进行过早的优化,例如将 a / 4 转换为 a >>> 2 在源代码中手动执行,除非您有真正的证据(来自性能测量)表明值得这样做。

The standard Java compiler does some optimizations, but it leaves most of them to the JIT.

The JIT knows on which processor exactly the program is running and also has access to runtime information, and therefore it can do more optimizations than the Java compiler could do in advance. Also, doing extensive optimizations in advance could "obfuscate" the byte code somewhat, making it harder for the JIT to optimize it.

I don't know what Google's compiler does when it translates your Java byte code to Dalvik code - it might be doing more extensive optimizations.

Maybe this tool will be useful for you: Dalvik Optimization and Verification With dexopt

By the way, the example you mention is not always valid; transforming a / 4 into a >> 2 is not guaranteed to make your program run faster on any processor. I read an article somewhere once (sorry, can't find it right now...) that explained that on (I think) modern x86 processors, a >> 2 might even be slower than a / 4.

In any case, don't do premature optimizations like transforming a / 4 to a >> 2 by hand in your source code unless you have real evidence (from performance measurements) that it's worthwhile to do it.

独享拥抱 2024-10-21 17:44:57

如果您的执行平台确实执行字节码,那么您对诸如 a / 4 之类的东西的直觉会比 a >>> 更快。 2 可能是错误的。您需要进行一些认真的应用程序分析,以弄清楚:

  • 是否值得优化、
  • 应将精力集中在哪里以及
  • 哪些(微)优化实际上有效。

FWIW,javac 编译器不太可能尝试微优化算术。最佳的本机代码取决于实际执行平台的硬件,如果 javac 尝试优化字节码,则可能会使 JIT 编译器的任务变得更加困难。

If your execution platform is really executing bytecodes, your intuitions about things like a / 4 being faster than a >> 2 are likely to be wrong. You need to do some serious application profiling to figure out:

  • whether it is worthwhile optimizing at all,
  • where to focus your efforts, and
  • what (micro-)optimizations actually work.

FWIW, the javac compiler is unlikely to attempt to micro-optimize arithmetic. The optimal native code depends on the hardware of the actual execution platform, and if javac attempted to optimize the bytecodes it is likely to make the task of the JIT compiler more difficult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文