From f7650429daabb020f7640a995dd76b7ee2d4b414 Mon Sep 17 00:00:00 2001 From: conghuhu Date: Wed, 6 Dec 2023 19:44:47 +0800 Subject: [PATCH] fix: fix some review --- .../util/collection/IntMapByDynamicHash.java | 12 ++++---- .../map/MapRandomGetPutThroughputTest.java | 30 +++++++++---------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java index 429a8f75eb..a86def7f13 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java @@ -124,15 +124,17 @@ private static int tableSizeFor(int c) { } /* ---------------- Table element access -------------- */ + + private static long entryOffset(int index) { + return ((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE; + } + private static Object tableAt(Object[] array, int index) { - return UNSAFE.getObjectVolatile(array, - ((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE); + return UNSAFE.getObjectVolatile(array, entryOffset(index)); } private static boolean casTableAt(Object[] array, int index, Object expected, Object newValue) { - return UNSAFE.compareAndSwapObject(array, - ((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE, - expected, newValue); + return UNSAFE.compareAndSwapObject(array, entryOffset(index), expected, newValue); } /** diff --git a/hugegraph-server/hugegraph-test/src/test/java/org/apache/hugegraph/benchmark/map/MapRandomGetPutThroughputTest.java b/hugegraph-server/hugegraph-test/src/test/java/org/apache/hugegraph/benchmark/map/MapRandomGetPutThroughputTest.java index b22e83224a..9dcfd7f6f2 100644 --- a/hugegraph-server/hugegraph-test/src/test/java/org/apache/hugegraph/benchmark/map/MapRandomGetPutThroughputTest.java +++ b/hugegraph-server/hugegraph-test/src/test/java/org/apache/hugegraph/benchmark/map/MapRandomGetPutThroughputTest.java @@ -56,13 +56,13 @@ public class MapRandomGetPutThroughputTest { private ConcurrentHashMap concurrentHashMapNonCap; - private ConcurrentHashMap concurrentHashMap; + private ConcurrentHashMap concurrentHashMapWithCap; - private IntMap intMapBySegments; + private IntMap intMapBySegmentsWithCap; private IntMap intMapByDynamicHashNonCap; - private IntMap intMapByDynamicHash; + private IntMap intMapByDynamicHashWithCap; private static final int THREAD_COUNT = 8; @@ -71,10 +71,10 @@ public class MapRandomGetPutThroughputTest { @Setup(Level.Trial) public void prepareMap() { this.concurrentHashMapNonCap = new ConcurrentHashMap<>(); - this.concurrentHashMap = new ConcurrentHashMap<>(MAP_CAPACITY); - this.intMapBySegments = new IntMap.IntMapBySegments(MAP_CAPACITY); + this.concurrentHashMapWithCap = new ConcurrentHashMap<>(MAP_CAPACITY); + this.intMapBySegmentsWithCap = new IntMap.IntMapBySegments(MAP_CAPACITY); this.intMapByDynamicHashNonCap = new IntMapByDynamicHash(); - this.intMapByDynamicHash = new IntMapByDynamicHash(MAP_CAPACITY); + this.intMapByDynamicHashWithCap = new IntMapByDynamicHash(MAP_CAPACITY); } /** @@ -104,20 +104,20 @@ public void randomGetPutOfConcurrentHashMapWithNoneInitCap(ThreadState state) { @Threads(THREAD_COUNT) public void randomGetPutOfConcurrentHashMapWithInitCap(ThreadState state) { int key = state.next() & (MAP_CAPACITY - 1); - if (!this.concurrentHashMap.containsKey(key)) { - this.concurrentHashMap.put(key, state.next()); + if (!this.concurrentHashMapWithCap.containsKey(key)) { + this.concurrentHashMapWithCap.put(key, state.next()); } - this.concurrentHashMap.get(key); + this.concurrentHashMapWithCap.get(key); } @Benchmark @Threads(THREAD_COUNT) public void randomGetPutOfIntMapBySegmentsWithInitCap(ThreadState state) { int key = state.next() & (MAP_CAPACITY - 1); - if (!this.intMapBySegments.containsKey(key)) { - this.intMapBySegments.put(key, state.next()); + if (!this.intMapBySegmentsWithCap.containsKey(key)) { + this.intMapBySegmentsWithCap.put(key, state.next()); } - this.intMapBySegments.get(key); + this.intMapBySegmentsWithCap.get(key); } @Benchmark @@ -134,10 +134,10 @@ public void randomGetPutOfIntMapByDynamicHashWithNoneCap(ThreadState state) { @Threads(THREAD_COUNT) public void randomGetPutOfIntMapByDynamicHashWithInitCap(ThreadState state) { int key = state.next() & (MAP_CAPACITY - 1); - if (!this.intMapByDynamicHash.containsKey(key)) { - this.intMapByDynamicHash.put(key, state.next()); + if (!this.intMapByDynamicHashWithCap.containsKey(key)) { + this.intMapByDynamicHashWithCap.put(key, state.next()); } - this.intMapByDynamicHash.get(key); + this.intMapByDynamicHashWithCap.get(key); } public static void main(String[] args) throws RunnerException {