Skip to content

Commit 2203e4c

Browse files
authored
Grow internal arrays when growing the capacity in AbstractHash implementations (#114907) (#115289)
This commit resizes those arrays when incrementing the capacity of the hashes to the maxSize.
1 parent b69d23d commit 2203e4c

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public BytesRefHash(long capacity, float maxLoadFactor, BigArrays bigArrays) {
4848
boolean success = false;
4949
try {
5050
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
51-
this.hashes = bigArrays.newIntArray(capacity, false);
51+
this.hashes = bigArrays.newIntArray(maxSize, false);
5252
this.bytesRefs = new BytesRefArray(capacity, bigArrays);
5353
success = true;
5454
} finally {
@@ -98,7 +98,7 @@ public BytesRefHash(BytesRefArray bytesRefs, float maxLoadFactor, BigArrays bigA
9898
boolean success = false;
9999
try {
100100
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
101-
this.hashes = bigArrays.newIntArray(bytesRefs.size() + 1, false);
101+
this.hashes = bigArrays.newIntArray(maxSize, false);
102102
this.bytesRefs = BytesRefArray.takeOwnershipOf(bytesRefs);
103103
success = true;
104104
} finally {
@@ -182,7 +182,6 @@ private long set(BytesRef key, int code, long id) {
182182
private void append(long id, BytesRef key, int code) {
183183
assert size == id;
184184
bytesRefs.append(key);
185-
hashes = bigArrays.grow(hashes, id + 1);
186185
hashes.set(id, code);
187186
}
188187

@@ -211,6 +210,7 @@ public long add(BytesRef key, int code) {
211210
if (size >= maxSize) {
212211
assert size == maxSize;
213212
grow();
213+
hashes = bigArrays.resize(hashes, maxSize);
214214
}
215215
assert size < maxSize;
216216
return set(key, rehash(code), size);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public LongHash(long capacity, float maxLoadFactor, BigArrays bigArrays) {
3333
super(capacity, maxLoadFactor, bigArrays);
3434
try {
3535
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
36-
keys = bigArrays.newLongArray(capacity, false);
36+
keys = bigArrays.newLongArray(maxSize, false);
3737
} finally {
3838
if (keys == null) {
3939
close();
@@ -78,7 +78,6 @@ private long set(long key, long id) {
7878
}
7979

8080
private void append(long id, long key) {
81-
keys = bigArrays.grow(keys, id + 1);
8281
keys.set(id, key);
8382
}
8483

@@ -102,6 +101,7 @@ public long add(long key) {
102101
if (size >= maxSize) {
103102
assert size == maxSize;
104103
grow();
104+
keys = bigArrays.resize(keys, maxSize);
105105
}
106106
assert size < maxSize;
107107
return set(key, size);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public LongLongHash(long capacity, float maxLoadFactor, BigArrays bigArrays) {
4040
super(capacity, maxLoadFactor, bigArrays);
4141
try {
4242
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
43-
keys = bigArrays.newLongArray(2 * capacity, false);
43+
keys = bigArrays.newLongArray(2 * maxSize, false);
4444
} finally {
4545
if (keys == null) {
4646
close();
@@ -99,7 +99,6 @@ private long set(long key1, long key2, long id) {
9999

100100
private void append(long id, long key1, long key2) {
101101
long keyOffset = 2 * id;
102-
keys = bigArrays.grow(keys, keyOffset + 2);
103102
keys.set(keyOffset, key1);
104103
keys.set(keyOffset + 1, key2);
105104
}
@@ -128,6 +127,7 @@ public long add(long key1, long key2) {
128127
if (size >= maxSize) {
129128
assert size == maxSize;
130129
grow();
130+
keys = bigArrays.resize(keys, maxSize * 2);
131131
}
132132
assert size < maxSize;
133133
return set(key1, key2, size);

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/aggregation/blockhash/BlockHashTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ public void testLongBytesRefHashWithMultiValuedFields() {
11471147
} else {
11481148
assertThat(
11491149
ordsAndKeys.description,
1150-
equalTo("BytesRefLongBlockHash{keys=[BytesRefKey[channel=1], LongKey[channel=0]], entries=9, size=491b}")
1150+
equalTo("BytesRefLongBlockHash{keys=[BytesRefKey[channel=1], LongKey[channel=0]], entries=9, size=483b}")
11511151
);
11521152
assertOrds(
11531153
ordsAndKeys.ords,

0 commit comments

Comments
 (0)