Skip to content

Commit 78388d3

Browse files
committed
try to avoid allocating multiple memory segments
1 parent d012ebf commit 78388d3

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

lucene/core/src/java25/org/apache/lucene/internal/vectorization/Lucene104MemorySegmentScalarQuantizedVectorScorer.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,8 @@ OptimizedScalarQuantizer.QuantizationResult getCorrectiveTerms(int ord) throws I
167167
node.get(INT_UNALIGNED_LE, Integer.BYTES * 3));
168168
}
169169

170-
record Node(MemorySegment vector, MemorySegment correctiveTerms) {
171-
OptimizedScalarQuantizer.QuantizationResult getQuantizationResult() {
172-
return new OptimizedScalarQuantizer.QuantizationResult(
173-
Float.intBitsToFloat(correctiveTerms.get(INT_UNALIGNED_LE, 0)),
174-
Float.intBitsToFloat(correctiveTerms.get(INT_UNALIGNED_LE, Integer.BYTES)),
175-
Float.intBitsToFloat(correctiveTerms.get(INT_UNALIGNED_LE, Integer.BYTES * 2)),
176-
correctiveTerms.get(INT_UNALIGNED_LE, Integer.BYTES * 3));
177-
}
178-
}
170+
record Node(
171+
MemorySegment vector, OptimizedScalarQuantizer.QuantizationResult correctiveTerms) {}
179172

180173
@SuppressWarnings("restricted")
181174
Node getNode(int ord) throws IOException {
@@ -189,7 +182,13 @@ Node getNode(int ord) throws IOException {
189182
input.readBytes(byteOffset, scratch, 0, nodeSize);
190183
vector = MemorySegment.ofArray(scratch);
191184
}
192-
MemorySegment correctiveTerms = vector.asSlice(vectorByteSize, CORRECTIVE_TERMS_SIZE);
185+
var correctiveTerms =
186+
new OptimizedScalarQuantizer.QuantizationResult(
187+
Float.intBitsToFloat(vector.get(INT_UNALIGNED_LE, vectorByteSize)),
188+
Float.intBitsToFloat(vector.get(INT_UNALIGNED_LE, vectorByteSize + Integer.BYTES)),
189+
Float.intBitsToFloat(
190+
vector.get(INT_UNALIGNED_LE, vectorByteSize + Integer.BYTES * 2)),
191+
vector.get(INT_UNALIGNED_LE, vectorByteSize + Integer.BYTES * 3));
193192
return new Node(vector.reinterpret(vectorByteSize), correctiveTerms);
194193
}
195194

@@ -243,7 +242,7 @@ public float score(int node) throws IOException {
243242
// Call getCorrectiveTerms() after computing dot product since corrective terms
244243
// bytes appear after the vector bytes, so this sequence of calls is more cache
245244
// friendly.
246-
return getSimilarity().score(dotProduct, queryCorrectiveTerms, doc.getQuantizationResult());
245+
return getSimilarity().score(dotProduct, queryCorrectiveTerms, doc.correctiveTerms);
247246
}
248247
}
249248

0 commit comments

Comments
 (0)