当我测试直接java.nio.ByteBuffer的读取性能时,我注意到绝对读取的平均速度比相对读取快2倍.此外,如果我比较相对与绝对读取的源代码,除了相对读取维护和内部计数器之外,代码几乎相同.我想知道为什么我在速度上看到如此大的差异?
以下是我的JMH基准测试的源代码:
public class DirectByteBufferReadBenchmark {
private static final int OBJ_SIZE = 8 + 4 + 1;
private static final int NUM_ELEM = 10_000_000;
@State(Scope.Benchmark)
public static class Data {
private ByteBuffer directByteBuffer;
@Setup
public void setup() {
directByteBuffer = ByteBuffer.allocateDirect(OBJ_SIZE * NUM_ELEM);
for (int i = 0; i < NUM_ELEM; i++) {
directByteBuffer.putLong(i);
directByteBuffer.putInt(i);
directByteBuffer.put((byte) (i & 1));
}
}
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public long testReadAbsolute(Data d) throws InterruptedException {
long val = 0l;
for (int i = …Run Code Online (Sandbox Code Playgroud) 我不小心从Kamon Monitoring工具中遇到了Striped64.java类.在第95行,我发现了这个评论:
JVM intrinsics note: It would be possible to use a release-only
form of CAS here, if it were provided.
Run Code Online (Sandbox Code Playgroud)
虽然我理解CAS是什么,但我无法找出CAS的唯一形式.有人可以对此有所了解吗?