Will the JVM ever inline an object's instance variables and methods?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 08:33:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6215138/will-the-jvm-ever-inline-an-objects-instance-variables-and-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!