@@ -376,7 +376,9 @@ private static class MemorySegmentPostingsVisitor implements PostingVisitor {
376376 final float [] correctionsUpper = new float [BULK_SIZE ];
377377 final int [] correctionsSum = new int [BULK_SIZE ];
378378 final float [] correctionsAdd = new float [BULK_SIZE ];
379- final int [] docIdsScratch ;
379+ final int [] docIdsScratch = new int [BULK_SIZE ];
380+ byte docEncoding ;
381+ int docBase = 0 ;
380382
381383 int vectors ;
382384 boolean quantized = false ;
@@ -415,7 +417,6 @@ private static class MemorySegmentPostingsVisitor implements PostingVisitor {
415417 quantizedVectorByteSize = (discretizedDimensions / 8 );
416418 quantizer = new OptimizedScalarQuantizer (fieldInfo .getVectorSimilarityFunction (), DEFAULT_LAMBDA , 1 );
417419 osqVectorsScorer = ESVectorUtil .getES91OSQVectorsScorer (indexInput , fieldInfo .getVectorDimension ());
418- this .docIdsScratch = new int [maxPostingListSize ];
419420 }
420421
421422 @ Override
@@ -425,24 +426,17 @@ public int resetPostingsScorer(long offset) throws IOException {
425426 indexInput .readFloats (centroid , 0 , centroid .length );
426427 centroidDp = Float .intBitsToFloat (indexInput .readInt ());
427428 vectors = indexInput .readVInt ();
428- // read the doc ids
429- assert vectors <= docIdsScratch .length ;
430- idsWriter .readInts (indexInput , vectors , docIdsScratch );
431- // reconstitute from the deltas
432- int sum = 0 ;
433- for (int i = 0 ; i < vectors ; i ++) {
434- sum += docIdsScratch [i ];
435- docIdsScratch [i ] = sum ;
436- }
429+ docEncoding = indexInput .readByte ();
430+ docBase = 0 ;
437431 slicePos = indexInput .getFilePointer ();
438432 return vectors ;
439433 }
440434
441- private float scoreIndividually (int offset ) throws IOException {
435+ private float scoreIndividually () throws IOException {
442436 float maxScore = Float .NEGATIVE_INFINITY ;
443437 // score individually, first the quantized byte chunk
444438 for (int j = 0 ; j < BULK_SIZE ; j ++) {
445- int doc = docIdsScratch [j + offset ];
439+ int doc = docIdsScratch [j ];
446440 if (doc != -1 ) {
447441 float qcDist = osqVectorsScorer .quantizeScore (quantizedQueryScratch );
448442 scores [j ] = qcDist ;
@@ -459,7 +453,7 @@ private float scoreIndividually(int offset) throws IOException {
459453 indexInput .readFloats (correctionsAdd , 0 , BULK_SIZE );
460454 // Now apply corrections
461455 for (int j = 0 ; j < BULK_SIZE ; j ++) {
462- int doc = docIdsScratch [offset + j ];
456+ int doc = docIdsScratch [j ];
463457 if (doc != -1 ) {
464458 scores [j ] = osqVectorsScorer .score (
465459 queryCorrections .lowerInterval (),
@@ -482,45 +476,56 @@ private float scoreIndividually(int offset) throws IOException {
482476 return maxScore ;
483477 }
484478
485- private static int docToBulkScore (int [] docIds , int offset , Bits acceptDocs ) {
479+ private static int docToBulkScore (int [] docIds , Bits acceptDocs ) {
486480 assert acceptDocs != null : "acceptDocs must not be null" ;
487481 int docToScore = ES91OSQVectorsScorer .BULK_SIZE ;
488482 for (int i = 0 ; i < ES91OSQVectorsScorer .BULK_SIZE ; i ++) {
489- final int idx = offset + i ;
490- if (acceptDocs .get (docIds [idx ]) == false ) {
491- docIds [idx ] = -1 ;
483+ if (acceptDocs .get (docIds [i ]) == false ) {
484+ docIds [i ] = -1 ;
492485 docToScore --;
493486 }
494487 }
495488 return docToScore ;
496489 }
497490
498- private static void collectBulk (int [] docIds , int offset , KnnCollector knnCollector , float [] scores ) {
491+ private void collectBulk (KnnCollector knnCollector , float [] scores ) {
499492 for (int i = 0 ; i < ES91OSQVectorsScorer .BULK_SIZE ; i ++) {
500- final int doc = docIds [ offset + i ];
493+ final int doc = docIdsScratch [ i ];
501494 if (doc != -1 ) {
502495 knnCollector .collect (doc , scores [i ]);
503496 }
504497 }
505498 }
506499
500+ private void readDocIds (int count ) throws IOException {
501+ idsWriter .readInts (indexInput , count , docEncoding , docIdsScratch );
502+ // reconstitute from the deltas
503+ for (int j = 0 ; j < count ; j ++) {
504+ docBase += docIdsScratch [j ];
505+ docIdsScratch [j ] = docBase ;
506+ }
507+ }
508+
507509 @ Override
508510 public int visit (KnnCollector knnCollector ) throws IOException {
509511 indexInput .seek (slicePos );
510512 // block processing
511513 int scoredDocs = 0 ;
512514 int limit = vectors - BULK_SIZE + 1 ;
513515 int i = 0 ;
516+ // read Docs
514517 for (; i < limit ; i += BULK_SIZE ) {
515- final int docsToBulkScore = acceptDocs == null ? BULK_SIZE : docToBulkScore (docIdsScratch , i , acceptDocs );
518+ // read the doc ids
519+ readDocIds (BULK_SIZE );
520+ final int docsToBulkScore = acceptDocs == null ? BULK_SIZE : docToBulkScore (docIdsScratch , acceptDocs );
516521 if (docsToBulkScore == 0 ) {
517522 indexInput .skipBytes (quantizedByteLength * BULK_SIZE );
518523 continue ;
519524 }
520525 quantizeQueryIfNecessary ();
521526 final float maxScore ;
522527 if (docsToBulkScore < BULK_SIZE / 2 ) {
523- maxScore = scoreIndividually (i );
528+ maxScore = scoreIndividually ();
524529 } else {
525530 maxScore = osqVectorsScorer .scoreBulk (
526531 quantizedQueryScratch ,
@@ -534,13 +539,18 @@ public int visit(KnnCollector knnCollector) throws IOException {
534539 );
535540 }
536541 if (knnCollector .minCompetitiveSimilarity () < maxScore ) {
537- collectBulk (docIdsScratch , i , knnCollector , scores );
542+ collectBulk (knnCollector , scores );
538543 }
539544 scoredDocs += docsToBulkScore ;
540545 }
541546 // process tail
547+ // read the doc ids
548+ if (i < vectors ) {
549+ readDocIds (vectors - i );
550+ }
551+ int count = 0 ;
542552 for (; i < vectors ; i ++) {
543- int doc = docIdsScratch [i ];
553+ int doc = docIdsScratch [count ++ ];
544554 if (acceptDocs == null || acceptDocs .get (doc )) {
545555 quantizeQueryIfNecessary ();
546556 float qcDist = osqVectorsScorer .quantizeScore (quantizedQueryScratch );
0 commit comments