为什么这个代码片段在使用热点服务器 JVM 运行时实际上需要更长的时间?

发布于 2024-11-09 03:11:39 字数 472 浏览 0 评论 0原文

这是代码片段:

https://gist.github.com/987751

对我来说,它的时间就像:

java -client:
  for loop took:     23
  method call took:  19

java -server:
  for loop took:     0   # faster, as expected
  method call took:  48  # slower--expected?

所以第一个问题是“为什么它比客户端虚拟机慢”

另外我想下一个问题是“是否有可能为方法调用方式获得超级 0ms 加速(几乎相同的代码)?”

我还认为,尽管有这种奇怪的现象,热点通常运行得更快,即使有匿名类等?

谢谢!

-罗杰-

Here's the code snippet:

https://gist.github.com/987751

For me, it gets times like:

java -client:
  for loop took:     23
  method call took:  19

java -server:
  for loop took:     0   # faster, as expected
  method call took:  48  # slower--expected?

So the first question would be "why is it slower than the client VM"

Also I guess the next question would be "is it possible to get that super 0ms speedup for the method call way (it's almost the same code)?"

Also I presume that despite this weirdness, hotspot in general runs much faster, even with, say, anonymous classes et al?

Thanks!

-roger-

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

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

发布评论

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

评论(1

极度宠爱 2024-11-16 03:11:39

这都是关于如何调整两种风格的 Hotspot:

  • 客户端已调整为快速(呃)启动。它“几乎”立即进行 JIT 编译方法。

  • 在假定为长调整服务器实例的生命周期内,服务器被调整为高(更高)吞吐量。它让解释器在 JIT 编译方法之前运行方法的时间更长(收集使用情况统计信息)。然后(我相信)它会执行更积极的优化......这需要更长的时间。

相关答案:“java -server”和“java -client”?

顺便说一句,它是运行客户端和服务器模式的同一个 Hotspot JVM。 AFAIK,差异是由于两种模式选择不同的默认 JVM 调整/配置参数造成的。


所以第一个问题是“为什么它比客户端虚拟机慢”

我不知道。

也许客户端模式可以实现特定的优化,真正有助于这个(高度人为的)基准测试。或者也许服务器模式优化之一实际上是针对此(高度人为的)基准测试的反优化。如果你真的想知道,请让 JIT 编译器转储本机代码并详细分析它。 (但我认为这是浪费时间。)

而且我猜下一个问题是“是否有可能为方法调用方式获得超级 0ms 的加速(几乎相同的代码)?”

这很容易。优化器发现该方法调用不会影响其他任何内容,并优化了该调用。然后循环也可以被优化掉。

我还认为,尽管有这种奇怪的情况,热点通常运行得更快,即使使用匿名类等?

我认为你不应该假设任何事情。

然而,我也不认为你的微基准对真实程序有任何意义。一般来说,微基准往往会产生误导,而您的微基准是有缺陷的(例如,被优化掉的循环),并且似乎没有测试典型(编写良好的)Java 程序会做的事情。

如果您确实关心 HotSpot 两种模式的相对性能,则应该在实际数据上运行并测量应用程序的性能。

...为什么服务器 JVM 会选择看起来更糟糕的东西?

优化器旨在优化实际程序......而不是花时间做与有用计算没有任何相似之处的奇怪事情的微基准。并非所有优化在所有情况下都有益,并且您可能遇到了某种边缘情况。

但只有当您在实际应用程序中看到同样的事情发生时,这才有意义。


最后,您没有提供 JVM 版本、平台和硬件的任何详细信息。这些事情可能会对相对绩效指标产生巨大影响。

Its all about how the two flavors of Hotspot are tuned:

  • Client is tuned for fast(er) startup. It JIT compiles methods "almost" straight away.

  • Server is tuned for high(er) throughput over the lifetime of a what is assumed to be a long tuning server instance. It lets the interpreter run methods much longer (gathering usage stats) before JIT compiling them. Then (I believe) it performs more aggressive optimization ... which takes longer.

Related Answer: Real differences between "java -server" and "java -client"?

By the way, it is the same Hotspot JVM that is running both client and server modes. AFAIK, the differences are due to the two modes selecting different default JVM tuning / configuration parameters.


So the first question would be "why is it slower than the client VM"

I don't know.

Perhaps the client mode enables a specific optimization that really helps this (highly artificial) benchmark. Or perhaps one of the server mode optimizations is actually an anti-optimization for this (highly artificial) benchmark. If you really want to know, get the JIT compiler to dump the native code and analyse it in detail. (But I think that's a waste of time.)

Also I guess the next question would be "is it possible to get that super 0ms speedup for the method call way (it's almost the same code)?"

That's easy. The optimizer has figured out that the method call does not affect anything else and has optimized away the call. The loop can then be optimized away as well.

Also I presume that despite this weirdness, hotspot in general runs much faster, even with, say, anonymous classes et al?

I don't think you should presume anything.

However, I also don't think your micro-benchmark says anything meaningful about real programs. Micro-benchmarks tend to be misleading in general, and yours is flawed (e.g. the loop that gets optimized away) and doesn't seem to be testing something that a typical (well written) Java program would do.

If you are really concerned about the relative performance of the two modes of HotSpot, you should run and measure the performance of your application on realistic data.

... and why would the server JVM choose something seemingly worse?

The optimizers are designed to optimize real programs ... not micro-benchmarks that spend their time doing strange things that don't bear any resemblance to a useful computation. Not all optimizations are beneficial in all situations, and you've probably hit some kind of edge case.

But this is only relevant if you see the same thing happening in a real application.


Finally, you don't give any details of your JVM version, platform and hardware. These things could make a huge difference to relative performance measures.

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