计数/未数的循环和安全性 - 是`while(++ i< someinint)`被考虑到未数的循环吗?
我正在阅读这篇文章在上计数/未数循环和SafePoints
。
它告诉我的是,对于无数循环,将有SafePoint Colls
,这意味着无数环的性能比计数循环差。
在博客中有一个交织代码:
// 4. Should be counted, but treated as uncounted
int i = 0;
while (++i < reps) {
// ...
}
// 5. Should be counted, but treated as uncounted
while (i++ < reps) {
// ...
}
// 6. Should be counted, and is!
while (i < reps) {
// ...
i++;
}
基本上说在中进行变量增量,而
(无论是i ++
或++ ++ i
)将使您的循环被视为无数循环。
鉴于此,我想对于绩效关键代码,我应该避免这样做吗?
其他问题是,由于循环展开
,JVM不会进行SafePoint Colls
进行计数?还是JVM足够聪明,即使没有循环展开
,也不会这样做?
I was reading this post on Counted/Uncounted loops and Safepoints
.
What it tells me is that there will be safepoint polls
for Uncounted loop, meaning Uncounted loop has worse performance than Counted loop.
In the blog there's this intersting code:
// 4. Should be counted, but treated as uncounted
int i = 0;
while (++i < reps) {
// ...
}
// 5. Should be counted, but treated as uncounted
while (i++ < reps) {
// ...
}
// 6. Should be counted, and is!
while (i < reps) {
// ...
i++;
}
Which basically says doing variable increment in while
(be it i++
or ++i
) will make your loop be treated as uncounted loop.
Given this, I suppose for performance critical code, I should avoid doing this?
Additional question, is the fact that JVM won't do safepoint polls
to Counted loop because loop unrolling
? Or JVM is smart enough to not do it even without loop unrolling
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本文看起来已经过时了。
在现代JDK中,帖子中提到的所有6个循环都被计算在内:
带有
long
变量的循环#3在JDK 16中计数(由于jdk-8223051
)。自JDK 8以来,所有其他循环都被视为计数。我使用以下程序来验证以下选项
的运行调试构建,并具有以下选项:
traceloopopts
traceloopopts的第一行被计数:不,SafePoint民意调查与循环展开无关。
计数循环的声明内部没有任何SafePoint Coll,也已经过时了。 loop strip挖掘 JDK 10中实现的允许JIT编译器将大型计数分为两个嵌套的环路:内部(热)一个没有SafePoint Coll指令的人,而外部则是SafePoint Coll。
例如,
将这种优化转换为类似的东西,
降低了Safepoint Comhering的开销,同时使对任意大型计数循环的时间延迟延长。
最新的热点JVM在优化大多数计数循环的优化方面足够聪明,因此您通常不需要自己关心这个循环。
The article looks outdated.
In the modern JDK, all 6 loops mentioned in the post are considered counted:
The loop #3 with the
long
variable became counted in JDK 16 (as a result ofJDK-8223051
). All other loops are treated as counted since JDK 8.I used the following program to verify this:
Run debug build of JDK with the following options:
The very first line of the
TraceLoopOpts
output will confirm that the loop is Counted:No, safepoint polls have nothing to do with loop unrolling.
The statement that counted loops have no safepoint poll inside is also outdated. Loop strip mining implemented in JDK 10 allows JIT compiler to split large counted loops into two nested loops: the inner (hot) one without a safepoint poll instruction, and the outer - with a safepoint poll.
E.g.
is transformed to something like
This optimization decreases the overhead of safepoint polling, while keeping the time-to-safepoint latency small for an arbitrary large counted loop.
The up-to-date HotSpot JVM is smart enough about optimizations of most kinds of counted loops, so you don't usually need to care about this yourself.
您链接到的博客发布的问题是,作者没有清楚地说明他们正在查看的Java版本。
安全点的放置详细信息绝对是JVM版本的特定于JVM版本。如果可以在位置不好的地方确定简单(和现实的)示例,那么我想,Java维护者将弄清楚如何使优化器更聪明。因此,要知道(事先)问题是否会影响您,您将知道您正在使用与作者相同的JVM版本。显然,字节码编译器版本也很重要……根据下面的评论
Q:您是否应该修改编码样式以避免(根据博客发布)不当的情况?
答:我的建议通常是 - 否:
但是,如果您&lt;基准您的应用程序和您&gt; do&lt;发现安全点问题具有可测量的效果,一定要调整应用程序中的循环,即受影响的循环,以及2)热点。 (但不是您所有的循环!这可能是浪费时间。)
简而言之……提防过早优化。
The problem with the blog posting you linked to is that the author does not state clearly what versions of Java they are looking at.
Details of the placement of safe points are definitely JVM version specific. If simple (and realistic) examples can be identified where the placement is bad, then I would imagine that the Java maintainers will figure out how to make the optimizer smarter. So to know (before hand) if the problem is going to affect you, you would to know you are using the same JVM version as the author. Apparently, the bytecode compiler version is also significant ... according to the comments below the posting
Q: Should you modify your coding style to avoid the cases that are (according to the blog posting) mishandled?
A: My advice would be in general - No:
However, if you >do< benchmark your application and you >do< find that that the safe-point problem has a measurable effect, by all means tweak the loops in your application that are 1) affected, and 2) hotspots. (But not all of your loops! That would probably be a waste of time.)
In short ... beware of premature optimization.