Make HnswLock and LockedRow final (#13776)

Trivial commit that makes HnswLock final and LockedRow a record. This is general clean up and helps the JIT a little when reasoning about these types - which show quite a bit in indexing and search profiles.
This commit is contained in:
Chris Hegarty 2024-09-12 20:39:57 +01:00 committed by GitHub
parent 2f7da75b7d
commit 3a8f75ccfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 4 additions and 12 deletions

View File

@ -222,7 +222,7 @@ public class HnswConcurrentMergeBuilder implements HnswBuilder {
@Override @Override
void graphSeek(HnswGraph graph, int level, int targetNode) { void graphSeek(HnswGraph graph, int level, int targetNode) {
try (HnswLock.LockedRow rowLock = hnswLock.read(level, targetNode)) { try (HnswLock.LockedRow rowLock = hnswLock.read(level, targetNode)) {
NeighborArray neighborArray = rowLock.row; NeighborArray neighborArray = rowLock.row();
if (nodeBuffer == null || nodeBuffer.length < neighborArray.size()) { if (nodeBuffer == null || nodeBuffer.length < neighborArray.size()) {
nodeBuffer = new int[neighborArray.size()]; nodeBuffer = new int[neighborArray.size()];
} }

View File

@ -339,7 +339,7 @@ public class HnswGraphBuilder implements HnswBuilder {
int nbr = candidates.nodes()[i]; int nbr = candidates.nodes()[i];
if (hnswLock != null) { if (hnswLock != null) {
try (HnswLock.LockedRow rowLock = hnswLock.write(level, nbr)) { try (HnswLock.LockedRow rowLock = hnswLock.write(level, nbr)) {
NeighborArray nbrsOfNbr = rowLock.row; NeighborArray nbrsOfNbr = rowLock.row();
nbrsOfNbr.addAndEnsureDiversity(node, candidates.scores()[i], nbr, scorerSupplier); nbrsOfNbr.addAndEnsureDiversity(node, candidates.scores()[i], nbr, scorerSupplier);
} }
} else { } else {

View File

@ -25,7 +25,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
* Provide (read-and-write) locked access to rows of an OnHeapHnswGraph. For use by * Provide (read-and-write) locked access to rows of an OnHeapHnswGraph. For use by
* HnswConcurrentMerger and its HnswGraphBuilders. * HnswConcurrentMerger and its HnswGraphBuilders.
*/ */
class HnswLock { final class HnswLock {
private static final int NUM_LOCKS = 512; private static final int NUM_LOCKS = 512;
private final ReentrantReadWriteLock[] locks; private final ReentrantReadWriteLock[] locks;
private final OnHeapHnswGraph graph; private final OnHeapHnswGraph graph;
@ -52,15 +52,7 @@ class HnswLock {
return new LockedRow(graph.getNeighbors(level, node), lock); return new LockedRow(graph.getNeighbors(level, node), lock);
} }
static class LockedRow implements Closeable { record LockedRow(NeighborArray row, Lock lock) implements Closeable {
final Lock lock;
final NeighborArray row;
LockedRow(NeighborArray row, Lock lock) {
this.lock = lock;
this.row = row;
}
@Override @Override
public void close() { public void close() {
lock.unlock(); lock.unlock();