Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add IntMapByDynamicHash V1 implement #2377

Merged
merged 7 commits into from
Dec 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: fix pr review
  • Loading branch information
conghuhu committed Dec 6, 2023
commit 375e29d4f0a41b5b94f6d3ca9afe6c5b4fe0e845
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ public class IntMapByDynamicHash implements IntMap {
*/
private int[] partitionedSize;

private static final Entry RESIZING = new Entry(NULL_VALUE, NULL_VALUE, 1);
private static final Entry RESIZED = new Entry(NULL_VALUE, NULL_VALUE, 2);
/**
* updated via atomic field updater
*/
@SuppressWarnings("UnusedDeclaration")
private volatile int size;

private static final Entry RESIZING = new Entry(NULL_VALUE, NULL_VALUE, (byte) 1);
private static final Entry RESIZED = new Entry(NULL_VALUE, NULL_VALUE, (byte) 2);

private static final Entry RESIZE_SENTINEL = new Entry(NULL_VALUE, NULL_VALUE, 3);
private static final Entry RESIZE_SENTINEL = new Entry(NULL_VALUE, NULL_VALUE, (byte) 3);

/**
* must be 2^n - 1
Expand All @@ -72,12 +78,10 @@ private static Object tableAt(Object[] array, int index) {
ENTRY_ARRAY_BASE);
}

private static boolean casTableAt(Object[] array, int index, Object expected, Object newValue) {
private static boolean casTableAt(Object[] array, int idx, Object e, Object v) {
return UNSAFE.compareAndSwapObject(
array,
((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE,
expected,
newValue);
((long) idx << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE, e, v);
}

private static void setTableAt(Object[] array, int index, Object newValue) {
Expand All @@ -95,12 +99,6 @@ private static int tableSizeFor(int c) {
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}

/**
* updated via atomic field updater
*/
@SuppressWarnings("UnusedDeclaration")
private volatile int size;

public IntMapByDynamicHash() {
this(DEFAULT_INITIAL_CAPACITY);
}
Expand Down Expand Up @@ -637,13 +635,13 @@ private void unconditionalCopy(Entry[] dest, Entry toCopyEntry) {
private static final class ResizeContainer extends Entry {
private static final int QUEUE_INCREMENT =
Math.min(1 << 10,
Integer.highestOneBit(Runtime.getRuntime().availableProcessors()) << 4);
Integer.highestOneBit(IntSet.CPUS) << 4);
private final AtomicInteger resizers = new AtomicInteger(1);
private final Entry[] nextArray;
private final AtomicInteger queuePosition;

private ResizeContainer(Entry[] nextArray, int oldSize) {
super(NULL_VALUE, NULL_VALUE, 4);
super(NULL_VALUE, NULL_VALUE, (byte) 4);
this.nextArray = nextArray;
this.queuePosition = new AtomicInteger(oldSize);
}
Expand Down Expand Up @@ -689,7 +687,7 @@ public void waitForAllResizers() {
try {
this.wait();
} catch (InterruptedException e) {
//ginore
// ignore
}
}
}
Expand All @@ -706,6 +704,7 @@ public void zeroOutQueuePosition() {
}

private static class Entry {
conghuhu marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect to serialize it into a chunk memory


final int key;
volatile int value;
volatile Entry next;
Expand All @@ -715,11 +714,11 @@ private static class Entry {
* 1 RESIZING
* 2 RESIZED
* 3 RESIZE_SENTINEL
* 4 ResizeContainer
* 4 RESIZE_CONTAINER
*/
final int state;
final byte state;

public Entry(int key, int value, int state) {
public Entry(int key, int value, byte state) {
this.key = key;
this.value = value;
this.state = state;
Expand Down Expand Up @@ -760,6 +759,7 @@ public String toString() {
/* ---------------- Iterator -------------- */

private static final class IteratorState {
conghuhu marked this conversation as resolved.
Show resolved Hide resolved

private Entry[] currentTable;
private int start;
private int end;
Expand All @@ -777,6 +777,7 @@ private IteratorState(Entry[] currentTable, int start, int end) {
}

private abstract class HashIterator implements IntIterator {
conghuhu marked this conversation as resolved.
Show resolved Hide resolved

private List<IteratorState> todo;
private IteratorState currentState;
private Entry next;
Expand Down