Skip to content

Commit

Permalink
fix: fix some review
Browse files Browse the repository at this point in the history
  • Loading branch information
conghuhu committed Dec 6, 2023
1 parent 9854e06 commit f765042
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public class MapRandomGetPutThroughputTest {

private ConcurrentHashMap<Integer, Integer> concurrentHashMapNonCap;

private ConcurrentHashMap<Integer, Integer> concurrentHashMap;
private ConcurrentHashMap<Integer, Integer> concurrentHashMapWithCap;

private IntMap intMapBySegments;
private IntMap intMapBySegmentsWithCap;

private IntMap intMapByDynamicHashNonCap;

private IntMap intMapByDynamicHash;
private IntMap intMapByDynamicHashWithCap;

private static final int THREAD_COUNT = 8;

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit f765042

Please sign in to comment.