Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grow internal arrays when growing the capacity in AbstractHash implementations #114907

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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