Class.getName() not inlined at concatenation expression

Сергей Цыпанов sergei.tsypanov at yandex.ru
Tue Feb 25 12:15:09 UTC 2020


Hello,

it seems Class.getName() cannot be inlined by optimizing compiler due to inner if-block.

As Andrey Pangin explained [1] the reason is profile polution and C1/C2 cannot eliminate inner branch.
This results in regression which can be demonstrated by this benchmark:

@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class BrokenConcatenationBenchmark {

  @Benchmark
  public String slow(Data data) {
    final Class<? extends Data> clazz = data.clazz;
    return "class " + clazz.getName();
  }

  @Benchmark
  public String fast(Data data) {
    final Class<? extends Data> clazz = data.clazz;
    final String clazzName = clazz.getName();
    return "class " + clazzName;
  }

  @State(Scope.Thread)
  public static class Data {
    final Class<? extends Data> clazz = getClass();

    @Setup
    public void setup() {
      //explicitly load name via native method Class.getName0()
      clazz.getName();
    }
  }
}

I propose to extract Class.getName() expression into a separate variable in some core classes (Class, MethodHandleProxies, Object and URLConnection).
This change is useless in later JDKs yet seems to be valid for JDK8 where we do not have indified String concatenation.
Patch is attached.


1. https://stackoverflow.com/questions/59157085/java-8-class-getname-slows-down-string-concatenation-chain
-------------- next part --------------
A non-text attachment was scrubbed...
Name: class_getName.patch
Type: text/x-diff
Size: 2305 bytes
Desc: not available
URL: <https://mail.openjdk.java.net/pipermail/jdk8u-dev/attachments/20200225/d9771b05/class_getName.patch>


More information about the jdk8u-dev mailing list