Skip to content

Commit

Permalink
Grow internal arrays when growing the capacity in AbstractHash implem…
Browse files Browse the repository at this point in the history
…entations (elastic#114907)

This commit resizes those arrays when incrementing the capacity of the hashes to the maxSize.
  • Loading branch information
iverase authored Oct 22, 2024
1 parent aff4edd commit 013760c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BytesRefHash(long capacity, float maxLoadFactor, BigArrays bigArrays) {
boolean success = false;
try {
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
this.hashes = bigArrays.newIntArray(capacity, false);
this.hashes = bigArrays.newIntArray(maxSize, false);
this.bytesRefs = new BytesRefArray(capacity, bigArrays);
success = true;
} finally {
Expand Down Expand Up @@ -98,7 +98,7 @@ public BytesRefHash(BytesRefArray bytesRefs, float maxLoadFactor, BigArrays bigA
boolean success = false;
try {
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
this.hashes = bigArrays.newIntArray(bytesRefs.size() + 1, false);
this.hashes = bigArrays.newIntArray(maxSize, false);
this.bytesRefs = BytesRefArray.takeOwnershipOf(bytesRefs);
success = true;
} finally {
Expand Down Expand Up @@ -182,7 +182,6 @@ private long set(BytesRef key, int code, long id) {
private void append(long id, BytesRef key, int code) {
assert size == id;
bytesRefs.append(key);
hashes = bigArrays.grow(hashes, id + 1);
hashes.set(id, code);
}

Expand Down Expand Up @@ -211,6 +210,7 @@ public long add(BytesRef key, int code) {
if (size >= maxSize) {
assert size == maxSize;
grow();
hashes = bigArrays.resize(hashes, maxSize);
}
assert size < maxSize;
return set(key, rehash(code), size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public LongHash(long capacity, float maxLoadFactor, BigArrays bigArrays) {
super(capacity, maxLoadFactor, bigArrays);
try {
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
keys = bigArrays.newLongArray(capacity, false);
keys = bigArrays.newLongArray(maxSize, false);
} finally {
if (keys == null) {
close();
Expand Down Expand Up @@ -78,7 +78,6 @@ private long set(long key, long id) {
}

private void append(long id, long key) {
keys = bigArrays.grow(keys, id + 1);
keys.set(id, key);
}

Expand All @@ -102,6 +101,7 @@ public long add(long key) {
if (size >= maxSize) {
assert size == maxSize;
grow();
keys = bigArrays.resize(keys, maxSize);
}
assert size < maxSize;
return set(key, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public LongLongHash(long capacity, float maxLoadFactor, BigArrays bigArrays) {
super(capacity, maxLoadFactor, bigArrays);
try {
// `super` allocates a big array so we have to `close` if we fail here or we'll leak it.
keys = bigArrays.newLongArray(2 * capacity, false);
keys = bigArrays.newLongArray(2 * maxSize, false);
} finally {
if (keys == null) {
close();
Expand Down Expand Up @@ -99,7 +99,6 @@ private long set(long key1, long key2, long id) {

private void append(long id, long key1, long key2) {
long keyOffset = 2 * id;
keys = bigArrays.grow(keys, keyOffset + 2);
keys.set(keyOffset, key1);
keys.set(keyOffset + 1, key2);
}
Expand Down Expand Up @@ -128,6 +127,7 @@ public long add(long key1, long key2) {
if (size >= maxSize) {
assert size == maxSize;
grow();
keys = bigArrays.resize(keys, maxSize * 2);
}
assert size < maxSize;
return set(key1, key2, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ public void testLongBytesRefHashWithMultiValuedFields() {
} else {
assertThat(
ordsAndKeys.description,
equalTo("BytesRefLongBlockHash{keys=[BytesRefKey[channel=1], LongKey[channel=0]], entries=9, size=491b}")
equalTo("BytesRefLongBlockHash{keys=[BytesRefKey[channel=1], LongKey[channel=0]], entries=9, size=483b}")
);
assertOrds(
ordsAndKeys.ords,
Expand Down

0 comments on commit 013760c

Please sign in to comment.