@@ -64,9 +64,13 @@ public class HnswGraphBuilder implements HnswBuilder {
6464 @ SuppressWarnings ("NonFinalStaticField" )
6565 public static long randSeed = DEFAULT_RAND_SEED ;
6666
67+ private static final int MAX_BULK_SCORE_NODES = 8 ;
68+
6769 protected final int M ; // max number of connections on upper layers
6870 private final double ml ;
6971
72+ private final int [] bulkScoreNodes ; // for bulk scoring
73+ private final float [] bulkScores ; // for bulk scoring
7074 private final SplittableRandom random ;
7175 protected final UpdateableRandomVectorScorer scorer ;
7276 protected final HnswGraphSearcher graphSearcher ;
@@ -177,6 +181,10 @@ protected HnswGraphBuilder(
177181 this .hnsw = hnsw ;
178182 this .hnswLock = hnswLock ;
179183 this .graphSearcher = graphSearcher ;
184+ // pick a number that keeps us from scoring TOO much for diversity checking
185+ // but enough to take advantage of bulk scoring
186+ this .bulkScoreNodes = new int [MAX_BULK_SCORE_NODES ];
187+ this .bulkScores = new float [MAX_BULK_SCORE_NODES ];
180188 entryCandidates = new GraphBuilderKnnCollector (1 );
181189 beamCandidates = new GraphBuilderKnnCollector (beamWidth );
182190 beamCandidates0 = new GraphBuilderKnnCollector (Math .min (beamWidth / 2 , M * 3 ));
@@ -491,9 +499,11 @@ static void popToScratch(GraphBuilderKnnCollector candidates, NeighborArray scra
491499 */
492500 private boolean diversityCheck (float score , NeighborArray neighbors , RandomVectorScorer scorer )
493501 throws IOException {
494- for (int i = 0 ; i < neighbors .size (); i ++) {
495- float neighborSimilarity = scorer .score (neighbors .nodes ()[i ]);
496- if (neighborSimilarity >= score ) {
502+ final int bulkScoreChunk = Math .min ((neighbors .size () + 1 ) / 2 , bulkScoreNodes .length );
503+ for (int scored = 0 ; scored < neighbors .size (); scored += bulkScoreChunk ) {
504+ int chunkSize = Math .min (bulkScoreChunk , neighbors .size () - scored );
505+ System .arraycopy (neighbors .nodes (), scored , bulkScoreNodes , 0 , chunkSize );
506+ if (scorer .bulkScore (bulkScoreNodes , bulkScores , chunkSize ) >= score ) {
497507 return false ;
498508 }
499509 }
0 commit comments