JVM 是否会内联对象的实例变量和方法?
假设我有一个非常紧密的内部循环,每次迭代都会访问和改变一个簿记对象,该对象存储一些有关算法的简单数据,并具有简单的操作逻辑
。簿记对象是私有的和最终的,并且它的所有方法都是私有的,最终和@inline。下面是一个示例(使用 Scala 语法):
object Frobnicate {
private class DataRemaining(val start: Int, val end: Int) {
@inline private def nextChunk = ....
}
def frobnicate {
// ...
val bookkeeper = new DataRemaining(0, 1000)
while( bookeeper.hasData ) {
val data = bookkeeper.nextChunk
// ......
}
}
}
JVM 是否会将整个 DataRemaining 对象内联到 Frobnicate.frobnicate
中?也就是说,它会将 start
和 end
视为局部变量,并将 nextChunk 代码直接内联到 frobnicate
中吗?
Suppose I have a very tight inner loop, each iteration of which accesses and mutates a single bookkeeping object that stores some simple data about the algorithm and has simple logic for manipulating it
The bookkeeping object is private and final and all of its methods are private, final and @inline. Here's an example (in Scala syntax):
object Frobnicate {
private class DataRemaining(val start: Int, val end: Int) {
@inline private def nextChunk = ....
}
def frobnicate {
// ...
val bookkeeper = new DataRemaining(0, 1000)
while( bookeeper.hasData ) {
val data = bookkeeper.nextChunk
// ......
}
}
}
Will the JVM ever inline the whole DataRemaining object into Frobnicate.frobnicate
? That is, will it treat start
and end
as local variables and inline the nextChunk code directly into frobnicate
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Java 中,它可以在您所遇到的情况下内联字段和方法。它并没有完全消除对象,但已经接近了。我认为 Scala 也会以类似的方式工作。
In Java it can inline fields and methods in a situation as you have. It does not eliminate the Object completely, but gets close. I assume Scala would work similarly.