mirror of https://github.com/apache/lucene.git
Provide a custom hash implementation in HnswLock (#13751)
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...).
This commit is contained in:
parent
d3130e2e9e
commit
e4efae6ab9
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -67,4 +66,8 @@ class HnswLock {
|
|||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
static int hash(int v1, int v2) {
|
||||
return v1 * 31 + v2;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue