Skip to content

Commit

Permalink
Add pre-computed hashes to speed up reinserts
Browse files Browse the repository at this point in the history
Signed-off-by: Ketan Verma <ketan9495@gmail.com>
  • Loading branch information
ketanv3 committed Aug 23, 2023
1 parent 67ee845 commit f9c874d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions server/src/main/java/org/opensearch/common/util/BytesRefHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public final class BytesRefHash implements Releasable {
*/
private ByteArray keys;

/**
* Pre-computed hashes of the stored keys.
* It is used to speed up reinserts when doubling the capacity.
*/
private LongArray hashes;

public BytesRefHash(final BigArrays bigArrays) {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_HASHER, bigArrays);
}
Expand Down Expand Up @@ -175,6 +181,7 @@ public BytesRefHash(final long initialCapacity, final float loadFactor, final Ha
offsets = bigArrays.newLongArray(initialCapacity + 1, false);
offsets.set(0, 0);
keys = bigArrays.newByteArray(initialCapacity * 3, false);
hashes = bigArrays.newLongArray(initialCapacity, false);
}

/**
Expand All @@ -193,7 +200,7 @@ public long add(final BytesRef key) {
} else {
table.set(idx, val);
}
return append(key);
return append(key, hash);
} else if (((value & MASK_FINGERPRINT) == fingerprint) && key.bytesEquals(get(ordinal = (value & MASK_ORDINAL), scratch))) {
return -1 - ordinal;
}
Expand Down Expand Up @@ -254,13 +261,15 @@ public long size() {
/**
* Appends the key in the keys' and offsets' tables.
*/
private long append(final BytesRef key) {
private long append(final BytesRef key, final long hash) {
final long start = offsets.get(size);
final long end = start + key.length;
offsets = bigArrays.grow(offsets, size + 2);
offsets.set(size + 1, end);
keys = bigArrays.grow(keys, end);
keys.set(start, key.bytes, key.offset, key.length);
hashes = bigArrays.grow(hashes, size + 1);
hashes.set(size, hash);
return size++;
}

Expand All @@ -282,7 +291,7 @@ private void growAndInsert(final long hash, final long value) {
table.set(hash & mask, value);

for (long ordinal = 0; ordinal < size; ordinal++) {
reinsert(ordinal, hasher.hash(get(ordinal, scratch)));
reinsert(ordinal, hashes.get(ordinal));
}
}

Expand All @@ -300,7 +309,7 @@ private void reinsert(final long ordinal, final long hash) {

@Override
public void close() {
Releasables.close(table, offsets, keys);
Releasables.close(table, offsets, keys, hashes);
}

/**
Expand Down

0 comments on commit f9c874d

Please sign in to comment.