Skip to content

Commit c4731aa

Browse files
authored
Improve performance of LongHash#removeAndAdd (#114199)
Remove unnecessary remove and later add of the key.
1 parent 316b47d commit c4731aa

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,17 @@ public long id(long index) {
3232
return ids.get(index) - 1;
3333
}
3434

35-
protected final long id(long index, long id) {
35+
/**
36+
* Set the id provided key at <code>0 &lt;= index &lt;= capacity()</code> .
37+
*/
38+
protected final void setId(long index, long id) {
39+
ids.set(index, id + 1);
40+
}
41+
42+
/**
43+
* Set the id provided key at <code>0 &lt;= index &lt;= capacity()</code> and get the previous value or -1 if this slot is unused.
44+
*/
45+
protected final long getAndSetId(long index, long id) {
3646
return ids.getAndSet(index, id + 1) - 1;
3747
}
3848

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private long set(BytesRef key, int code, long id) {
169169
for (long index = slot;; index = nextSlot(index, mask)) {
170170
final long curId = id(index);
171171
if (curId == -1) { // means unset
172-
id(index, id);
172+
setId(index, id);
173173
append(id, key, code);
174174
++size;
175175
return id;
@@ -197,7 +197,7 @@ private void reset(int code, long id) {
197197
for (long index = slot;; index = nextSlot(index, mask)) {
198198
final long curId = id(index);
199199
if (curId == -1) { // means unset
200-
id(index, id);
200+
setId(index, id);
201201
break;
202202
}
203203
}
@@ -223,7 +223,7 @@ public long add(BytesRef key) {
223223

224224
@Override
225225
protected void removeAndAdd(long index) {
226-
final long id = id(index, -1);
226+
final long id = getAndSetId(index, -1);
227227
assert id >= 0;
228228
final int code = hashes.get(id);
229229
reset(code, id);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private long set(int key1, int key2, int key3, long id) {
7979
while (true) {
8080
final long curId = id(index);
8181
if (curId == -1) { // means unset
82-
id(index, id);
82+
setId(index, id);
8383
append(id, key1, key2, key3);
8484
++size;
8585
return id;
@@ -106,7 +106,7 @@ private void reset(int key1, int key2, int key3, long id) {
106106
while (true) {
107107
final long curId = id(index);
108108
if (curId == -1) { // means unset
109-
id(index, id);
109+
setId(index, id);
110110
append(id, key1, key2, key3);
111111
break;
112112
}
@@ -130,7 +130,7 @@ public long add(int key1, int key2, int key3) {
130130

131131
@Override
132132
protected void removeAndAdd(long index) {
133-
final long id = id(index, -1);
133+
final long id = getAndSetId(index, -1);
134134
assert id >= 0;
135135
long keyOffset = id * 3;
136136
final int key1 = keys.getAndSet(keyOffset, 0);

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private long set(long key, long id) {
6767
for (long index = slot;; index = nextSlot(index, mask)) {
6868
final long curId = id(index);
6969
if (curId == -1) { // means unset
70-
id(index, id);
70+
setId(index, id);
7171
append(id, key);
7272
++size;
7373
return id;
@@ -82,13 +82,13 @@ private void append(long id, long key) {
8282
keys.set(id, key);
8383
}
8484

85-
private void reset(long key, long id) {
85+
private void reset(long id) {
86+
final long key = keys.get(id);
8687
final long slot = slot(hash(key), mask);
8788
for (long index = slot;; index = nextSlot(index, mask)) {
8889
final long curId = id(index);
8990
if (curId == -1) { // means unset
90-
id(index, id);
91-
append(id, key);
91+
setId(index, id);
9292
break;
9393
}
9494
}
@@ -109,10 +109,9 @@ public long add(long key) {
109109

110110
@Override
111111
protected void removeAndAdd(long index) {
112-
final long id = id(index, -1);
112+
final long id = getAndSetId(index, -1);
113113
assert id >= 0;
114-
final long key = keys.getAndSet(id, 0);
115-
reset(key, id);
114+
reset(id);
116115
}
117116

118117
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private long set(long key1, long key2, long id) {
8484
for (long index = slot;; index = nextSlot(index, mask)) {
8585
final long curId = id(index);
8686
if (curId == -1) { // means unset
87-
id(index, id);
87+
setId(index, id);
8888
append(id, key1, key2);
8989
++size;
9090
return id;
@@ -109,7 +109,7 @@ private void reset(long key1, long key2, long id) {
109109
for (long index = slot;; index = nextSlot(index, mask)) {
110110
final long curId = id(index);
111111
if (curId == -1) { // means unset
112-
id(index, id);
112+
setId(index, id);
113113
append(id, key1, key2);
114114
break;
115115
}
@@ -132,7 +132,7 @@ public long add(long key1, long key2) {
132132

133133
@Override
134134
protected void removeAndAdd(long index) {
135-
final long id = id(index, -1);
135+
final long id = getAndSetId(index, -1);
136136
assert id >= 0;
137137
long keyOffset = id * 2;
138138
final long key1 = keys.getAndSet(keyOffset, 0);

0 commit comments

Comments
 (0)