What is java escape analysis?

Preface

I was touching Graal and didn't know what the partial escape analysis was, so I looked it up.

↓ Articles written about Graal / GraalVM https://qiita.com/kinshotomoya/items/39a821dd6a6a52202c0a

The sample code is written in scala.

What is escape

The object reference exists outside the method or in a different thread. When escaped, you can refer to an object from an unspecified number of places.

Escape conditions

When can we say that an object is escaped in the actual code? There are mainly the following three conditions.

  1. Specify as a method argument
  2. Return with return (specify as return value)

1. Specify as a method argument

A reference to a, which is an instance of Object, can be referenced beyond the hoge method. Can also be referenced from the foo method

case class Object()

object Test {
  def hoge: Object = {
    val a = Object()
    foo(a)
  }
  
  def foo(obj: Object) = ???
}

2. Return with return (specify as return value)

References to instance b of an Object can be referenced beyond the hoge method. The b2 variable has a reference to object a.

case class Object()

object Test {
  def hoge: Object = {
    val b = Object()
    b
  }
  
  val b2 = hoge()
}

In other words, it is possible to refer to the above example instances a and b beyond the method.

What is escape analysis?

Parse whether the instance reference escapes outside the method or to another thread. There are various algorithms.

What if it is parsed if it is not escaped?

If the escape analysis shows that the reference is closed inside the method,

1. Store the instance in the stack, not the heap.

If the instance is used only within the method, it is effective to store it in the stack area that is released after the method ends.

2. Ignore unnecessary synchronization when executing methods

References from only a single thread eliminate the need for synchronization between threads.

Optimization example

As a result of escape analysis, the compiler optimizes the code. An example is shown.

Inline

I have the following code.


class Person {
  def get(name: String) = {
    val person: Person = Person(name)
    if (person.name === "hoge") {
    }
    ...
  }
}

case class Person(name: String)

An instance of the Person object, person, is not escaped. In such cases, the compiler inlines and optimizes.

class Person {
  def get(name: String) = {
    // val person: Person = Person(name)
    if (name === "hoge") { //Inline

    }
    ...
  }
}

case class Person(name: String)


Partial escape analysis

This is one of the features of the new JIT compiler Graal. It can be partially optimized.

If you have the following code.

class Person {
  def get(name: String) = {
    val person: Person = Person(name)
    val cachePerson = Cache.get(name)
    if (cachePerson.isDefined) {
      cachePerson
    } else {
     addToCache(person)
     person
    }
  }
}

case class Person(name: String)

If cachePerson exists, the object person will not be escaped. The compiler parses that and modifies it into the following code.


class Person {
  def get(name: String) = {
    val cachePerson = Cache.get(name)
    if (cachePerson.isDefined) {
      cachePerson
    } else {
     val person: Person = Person(name)
     addToCache(person)
      person
    }
  }
}

case class Person(name: String)

You can move val person: Person = Person (name) under ʻelse and, if cachePerson` exists, eliminate the memory allocation to the heap area. Instead, it is stored in the stack area, improving processing efficiency.

If cachePerson does not exist, person is escaped and stored in the heap area.

reference

Recommended Posts

What is java escape analysis?
What is java
What is Java <>?
What is Java
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is ArrayList?
What is Java Assertion? Summary.
What is a Java collection?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is JVM (Java Virtual Machine)?
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is a lambda expression (Java)
What is Cubby
What is a class in Java language (3 /?)
What is null? ]
What is Keycloak
[Java] Escape sequence
What is maven?
What is Jackson?
What is ArgumentMatcher?
What is IM-Juggling?
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is params
What is SLF4J?
What is Java and Development Environment (MAC)
What is Facade? ??
What is Gradle?
What is POJO
What is the main method in Java?
What is centOS
What is RubyGem?
What is before_action?
What is Byte?
What is Tomcat
What is the Java Servlet / JSP MVC model?
What is the volatile modifier for Java variables?
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is a stream
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is @ (instance variable)?
What is Gradle's Artifact?