-
Notifications
You must be signed in to change notification settings - Fork 465
/
CompactOffHeapLinearHashTable.java
195 lines (161 loc) · 6.76 KB
/
CompactOffHeapLinearHashTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.impl;
import net.openhft.lang.Maths;
import static net.openhft.lang.MemoryUnit.BITS;
import static net.openhft.lang.MemoryUnit.BYTES;
import static net.openhft.lang.io.NativeBytes.UNSAFE;
public class CompactOffHeapLinearHashTable {
// to fit 64 bits per slot.
public static final int MAX_SEGMENT_CHUNKS = 1 << 30;
public static final int MAX_SEGMENT_ENTRIES = 1 << 29;
public static int valueBits(long actualChunksPerSegment) {
return 64 - Long.numberOfLeadingZeros(actualChunksPerSegment - 1L);
}
public static int keyBits(long entriesPerSegment, int valueBits) {
// key hash cardinality is between 1.0 and 2.0 of key cardinality
int minKeyBits = 64 - Long.numberOfLeadingZeros(entriesPerSegment - 1L);
// This minimizes probability of hash collision => on search, additional cache line touch
// and key comparison, that is quite high cost.
// between 8.0 and 16.0
minKeyBits += 3;
int actualEntryBits = (int) BYTES.align((long) (minKeyBits + valueBits), BITS);
// devote the rest bits for key
return actualEntryBits - valueBits;
}
public static int entrySize(int keyBits, int valueBits) {
return (int) BYTES.alignAndConvert((long) (keyBits + valueBits), BITS);
}
public static long capacityFor(long entriesPerSegment) {
if (entriesPerSegment < 0L)
throw new IllegalArgumentException("entriesPerSegment should be positive");
long capacity = Maths.nextPower2(entriesPerSegment, 64L);
if (((double) entriesPerSegment) / (double) capacity > 2./3.) {
// hash lookup shouldn't be too dense
capacity <<= 1L;
}
return capacity;
}
public static long mask(int bits) {
return (1L << bits) - 1L;
}
public static final long UNSET_KEY = 0L;
public static final long UNSET_ENTRY = 0L;
private final long capacityMask;
private final int hashLookupEntrySize;
private final long capacityMask2;
private final int keyBits;
private final long keyMask;
private final long valueMask;
private final long entryMask;
CompactOffHeapLinearHashTable(long capacity, int entrySize, int keyBits, int valueBits) {
this.capacityMask = capacity - 1L;
this.hashLookupEntrySize = entrySize;
this.capacityMask2 = capacityMask * entrySize;
this.keyBits = keyBits;
this.keyMask = mask(keyBits);
this.valueMask = mask(valueBits);
this.entryMask = mask(keyBits + valueBits);
}
CompactOffHeapLinearHashTable(VanillaChronicleHash h) {
this(h.segmentHashLookupCapacity, h.segmentHashLookupEntrySize, h.segmentHashLookupKeyBits,
h.segmentHashLookupValueBits);
}
long indexToPos(long index) {
return index * hashLookupEntrySize;
}
public long maskUnsetKey(long key) {
return (key &= keyMask) != UNSET_KEY ? key : keyMask;
}
public void checkValueForPut(long value) {
assert (value & ~valueMask) == 0L : "Value out of range, was " + value;
}
public boolean empty(long entry) {
return (entry & entryMask) == UNSET_ENTRY;
}
public long key(long entry) {
return entry & keyMask;
}
public long value(long entry) {
return (entry >>> keyBits) & valueMask;
}
long entry(long key, long value) {
return key | (value << keyBits);
}
public long hlPos(long key) {
return indexToPos(key & capacityMask);
}
public long step(long pos) {
return (pos += hashLookupEntrySize) <= capacityMask2 ? pos : 0L;
}
public long stepBack(long pos) {
return (pos -= hashLookupEntrySize) >= 0 ? pos : capacityMask2;
}
public long readEntry(long addr, long pos) {
return UNSAFE.getLong(addr + pos);
}
public void writeEntryVolatile(long addr, long pos, long prevEntry, long key, long value) {
long entry = (prevEntry & ~entryMask) | entry(key, value);
UNSAFE.putLongVolatile(null, addr + pos, entry);
}
public void putValueVolatile(long addr, long pos, long value) {
checkValueForPut(value);
long currentEntry = readEntry(addr, pos);
writeEntryVolatile(addr, pos, currentEntry, key(currentEntry), value);
}
void writeEntry(long addr, long pos, long prevEntry, long anotherEntry) {
long entry = (prevEntry & ~entryMask) | (anotherEntry & entryMask);
UNSAFE.putLong(addr + pos, entry);
}
void clearEntry(long addr, long pos, long prevEntry) {
long entry = (prevEntry & ~entryMask);
UNSAFE.putLong(addr + pos, entry);
}
public void clearHashLookup(long addr) {
UNSAFE.setMemory(addr, capacityMask2 + hashLookupEntrySize, (byte) 0);
}
/**
* Returns "insert" position in terms of consequent putValue()
*/
public long remove(long addr, long posToRemove) {
long entryToRemove = readEntry(addr, posToRemove);
long posToShift = posToRemove;
while (true) {
posToShift = step(posToShift);
long entryToShift = readEntry(addr, posToShift);
if (empty(entryToShift))
break;
long insertPos = hlPos(key(entryToShift));
// the following condition essentially means circular permutations
// of three (r = posToRemove, s = posToShift, i = insertPos)
// positions are accepted:
// [...i..r...s.] or
// [...r..s...i.] or
// [...s..i...r.]
boolean cond1 = insertPos <= posToRemove;
boolean cond2 = posToRemove <= posToShift;
if ((cond1 && cond2) ||
// chain wrapped around capacity
(posToShift < insertPos && (cond1 || cond2))) {
writeEntry(addr, posToRemove, entryToRemove, entryToShift);
posToRemove = posToShift;
entryToRemove = entryToShift;
}
}
clearEntry(addr, posToRemove, entryToRemove);
return posToRemove;
}
}