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:
Chris Hegarty 2024-09-10 22:55:43 +01:00 committed by GitHub
parent d3130e2e9e
commit e4efae6ab9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 3 deletions

View File

@ -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;
}
}