@@ -376,7 +376,9 @@ private static class MemorySegmentPostingsVisitor implements PostingVisitor {
376
376
final float [] correctionsUpper = new float [BULK_SIZE ];
377
377
final int [] correctionsSum = new int [BULK_SIZE ];
378
378
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 ;
380
382
381
383
int vectors ;
382
384
boolean quantized = false ;
@@ -415,7 +417,6 @@ private static class MemorySegmentPostingsVisitor implements PostingVisitor {
415
417
quantizedVectorByteSize = (discretizedDimensions / 8 );
416
418
quantizer = new OptimizedScalarQuantizer (fieldInfo .getVectorSimilarityFunction (), DEFAULT_LAMBDA , 1 );
417
419
osqVectorsScorer = ESVectorUtil .getES91OSQVectorsScorer (indexInput , fieldInfo .getVectorDimension ());
418
- this .docIdsScratch = new int [maxPostingListSize ];
419
420
}
420
421
421
422
@ Override
@@ -425,24 +426,17 @@ public int resetPostingsScorer(long offset) throws IOException {
425
426
indexInput .readFloats (centroid , 0 , centroid .length );
426
427
centroidDp = Float .intBitsToFloat (indexInput .readInt ());
427
428
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 ;
437
431
slicePos = indexInput .getFilePointer ();
438
432
return vectors ;
439
433
}
440
434
441
- private float scoreIndividually (int offset ) throws IOException {
435
+ private float scoreIndividually () throws IOException {
442
436
float maxScore = Float .NEGATIVE_INFINITY ;
443
437
// score individually, first the quantized byte chunk
444
438
for (int j = 0 ; j < BULK_SIZE ; j ++) {
445
- int doc = docIdsScratch [j + offset ];
439
+ int doc = docIdsScratch [j ];
446
440
if (doc != -1 ) {
447
441
float qcDist = osqVectorsScorer .quantizeScore (quantizedQueryScratch );
448
442
scores [j ] = qcDist ;
@@ -459,7 +453,7 @@ private float scoreIndividually(int offset) throws IOException {
459
453
indexInput .readFloats (correctionsAdd , 0 , BULK_SIZE );
460
454
// Now apply corrections
461
455
for (int j = 0 ; j < BULK_SIZE ; j ++) {
462
- int doc = docIdsScratch [offset + j ];
456
+ int doc = docIdsScratch [j ];
463
457
if (doc != -1 ) {
464
458
scores [j ] = osqVectorsScorer .score (
465
459
queryCorrections .lowerInterval (),
@@ -482,45 +476,56 @@ private float scoreIndividually(int offset) throws IOException {
482
476
return maxScore ;
483
477
}
484
478
485
- private static int docToBulkScore (int [] docIds , int offset , Bits acceptDocs ) {
479
+ private static int docToBulkScore (int [] docIds , Bits acceptDocs ) {
486
480
assert acceptDocs != null : "acceptDocs must not be null" ;
487
481
int docToScore = ES91OSQVectorsScorer .BULK_SIZE ;
488
482
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 ;
492
485
docToScore --;
493
486
}
494
487
}
495
488
return docToScore ;
496
489
}
497
490
498
- private static void collectBulk (int [] docIds , int offset , KnnCollector knnCollector , float [] scores ) {
491
+ private void collectBulk (KnnCollector knnCollector , float [] scores ) {
499
492
for (int i = 0 ; i < ES91OSQVectorsScorer .BULK_SIZE ; i ++) {
500
- final int doc = docIds [ offset + i ];
493
+ final int doc = docIdsScratch [ i ];
501
494
if (doc != -1 ) {
502
495
knnCollector .collect (doc , scores [i ]);
503
496
}
504
497
}
505
498
}
506
499
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
+
507
509
@ Override
508
510
public int visit (KnnCollector knnCollector ) throws IOException {
509
511
indexInput .seek (slicePos );
510
512
// block processing
511
513
int scoredDocs = 0 ;
512
514
int limit = vectors - BULK_SIZE + 1 ;
513
515
int i = 0 ;
516
+ // read Docs
514
517
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 );
516
521
if (docsToBulkScore == 0 ) {
517
522
indexInput .skipBytes (quantizedByteLength * BULK_SIZE );
518
523
continue ;
519
524
}
520
525
quantizeQueryIfNecessary ();
521
526
final float maxScore ;
522
527
if (docsToBulkScore < BULK_SIZE / 2 ) {
523
- maxScore = scoreIndividually (i );
528
+ maxScore = scoreIndividually ();
524
529
} else {
525
530
maxScore = osqVectorsScorer .scoreBulk (
526
531
quantizedQueryScratch ,
@@ -534,13 +539,18 @@ public int visit(KnnCollector knnCollector) throws IOException {
534
539
);
535
540
}
536
541
if (knnCollector .minCompetitiveSimilarity () < maxScore ) {
537
- collectBulk (docIdsScratch , i , knnCollector , scores );
542
+ collectBulk (knnCollector , scores );
538
543
}
539
544
scoredDocs += docsToBulkScore ;
540
545
}
541
546
// process tail
547
+ // read the doc ids
548
+ if (i < vectors ) {
549
+ readDocIds (vectors - i );
550
+ }
551
+ int count = 0 ;
542
552
for (; i < vectors ; i ++) {
543
- int doc = docIdsScratch [i ];
553
+ int doc = docIdsScratch [count ++ ];
544
554
if (acceptDocs == null || acceptDocs .get (doc )) {
545
555
quantizeQueryIfNecessary ();
546
556
float qcDist = osqVectorsScorer .quantizeScore (quantizedQueryScratch );
0 commit comments