Skip to content

Commit

Permalink
Provide a custom hash implementation in HnswLock (#13751)
Browse files Browse the repository at this point in the history
This commit is a micro optimisation to the HNSW Lock implementation, which avoids integer and vararg boxing when determining the hash of the node and level. Trivially we provide our own arity specialised hash, rather than the more generic java.util.Objects.hash(Object...).
  • Loading branch information
ChrisHegarty authored Sep 10, 2024
1 parent d3130e2 commit e4efae6
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.lucene.util.hnsw;

import java.io.Closeable;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

Expand All @@ -40,14 +39,14 @@ class HnswLock {
}

LockedRow read(int level, int node) {
int lockid = Objects.hash(level, node) % NUM_LOCKS;
int lockid = hash(level, node) % NUM_LOCKS;
Lock lock = locks[lockid].readLock();
lock.lock();
return new LockedRow(graph.getNeighbors(level, node), lock);
}

LockedRow write(int level, int node) {
int lockid = Objects.hash(level, node) % NUM_LOCKS;
int lockid = hash(level, node) % NUM_LOCKS;
Lock lock = locks[lockid].writeLock();
lock.lock();
return new LockedRow(graph.getNeighbors(level, node), lock);
Expand All @@ -67,4 +66,8 @@ public void close() {
lock.unlock();
}
}

static int hash(int v1, int v2) {
return v1 * 31 + v2;
}
}

0 comments on commit e4efae6

Please sign in to comment.