问题
什么是压缩的 oop/引用?压缩引用存在什么问题?
基础知识
Java 规范并未规定数据类型的存储大小。即使对于原始数据类型,它也只规定了原始类型应明确支持的范围及其操作行为,而没有规定实际的存储大小。例如,在某些实现中,这允许boolean字段占用 1、2、4 个字节。
Java 引用大小的问题比较模糊,因为规范也没有明确指出 Java 引用是什么,而是将这一决定留给了 JVM 实现。大多数 JVM 实现将 Java 引用转换为机器指针,无需额外的间接寻址,这简化了性能问题。
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class CompressedRefs {
static class MyClass {
int x;
public MyClass(int x) {
this.x = x; }
public int x() {
return x; }
}
private MyClass o = new MyClass(42);
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public int access() {
return o.x();
}
}
汇编指令如下:
....[Hottest Region 3]....................................................
c2, level 4, org.openjdk.CompressedRefs::access, version 712 (35 bytes)
[Verified Entry Point]
1.10% ...b0: mov %eax,-0x14000(%rsp) ; prolog
6.82% ...b7: push %rbp ;
0.33% ...b8: sub $0x10,%rsp ;
1.20% ...bc: mov 0x10(%rsi),%r10 ; get field "o" to %r10
5.60% ...c0: mov 0x10(%r10),%eax ; get field "o.x" to %eax
7.21% ...c4: add $0x10,%rsp ; epilog
0.50% ...c8: pop %rbp
0.54% ...c9: mov 0x108