Skip to content

Commit 89199a6

Browse files
authored
Improve performance of Int3Hash#removeAndAdd (#114383) (#114469)
1 parent 57f083e commit 89199a6

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

server/src/main/java/org/elasticsearch/common/util/Int3Hash.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public int getKey3(long id) {
5858
* Get the id associated with <code>key</code> or -1 if the key is not contained in the hash.
5959
*/
6060
public long find(int key1, int key2, int key3) {
61-
long index = slot(hash(key1, key2, key3), mask);
62-
while (true) {
61+
final long slot = slot(hash(key1, key2, key3), mask);
62+
for (long index = slot;; index = nextSlot(index, mask)) {
6363
final long id = id(index);
6464
if (id == -1) {
6565
return id;
@@ -69,48 +69,49 @@ public long find(int key1, int key2, int key3) {
6969
return id;
7070
}
7171
}
72-
index = nextSlot(index, mask);
7372
}
7473
}
7574

7675
private long set(int key1, int key2, int key3, long id) {
7776
assert size < maxSize;
78-
long index = slot(hash(key1, key2, key3), mask);
79-
while (true) {
77+
long slot = slot(hash(key1, key2, key3), mask);
78+
for (long index = slot;; index = nextSlot(index, mask)) {
8079
final long curId = id(index);
8180
if (curId == -1) { // means unset
8281
setId(index, id);
8382
append(id, key1, key2, key3);
8483
++size;
8584
return id;
8685
} else {
87-
long keyOffset = 3 * curId;
86+
final long keyOffset = 3 * curId;
8887
if (keys.get(keyOffset) == key1 && keys.get(keyOffset + 1) == key2 && keys.get(keyOffset + 2) == key3) {
8988
return -1 - curId;
9089
}
9190
}
92-
index = nextSlot(index, mask);
9391
}
9492
}
9593

9694
private void append(long id, int key1, int key2, int key3) {
97-
long keyOffset = 3 * id;
95+
final long keyOffset = 3 * id;
9896
keys = bigArrays.grow(keys, keyOffset + 3);
9997
keys.set(keyOffset, key1);
10098
keys.set(keyOffset + 1, key2);
10199
keys.set(keyOffset + 2, key3);
102100
}
103101

104-
private void reset(int key1, int key2, int key3, long id) {
105-
long index = slot(hash(key1, key2, key3), mask);
106-
while (true) {
102+
private void reset(long id) {
103+
final IntArray keys = this.keys;
104+
final long keyOffset = id * 3;
105+
final int key1 = keys.get(keyOffset);
106+
final int key2 = keys.get(keyOffset + 1);
107+
final int key3 = keys.get(keyOffset + 2);
108+
final long slot = slot(hash(key1, key2, key3), mask);
109+
for (long index = slot;; index = nextSlot(index, mask)) {
107110
final long curId = id(index);
108111
if (curId == -1) { // means unset
109112
setId(index, id);
110-
append(id, key1, key2, key3);
111113
break;
112114
}
113-
index = nextSlot(index, mask);
114115
}
115116
}
116117

@@ -132,11 +133,7 @@ public long add(int key1, int key2, int key3) {
132133
protected void removeAndAdd(long index) {
133134
final long id = getAndSetId(index, -1);
134135
assert id >= 0;
135-
long keyOffset = id * 3;
136-
final int key1 = keys.getAndSet(keyOffset, 0);
137-
final int key2 = keys.getAndSet(keyOffset + 1, 0);
138-
final int key3 = keys.getAndSet(keyOffset + 2, 0);
139-
reset(key1, key2, key3, id);
136+
reset(id);
140137
}
141138

142139
@Override

0 commit comments

Comments
 (0)