From 736328f43c678a4f8132f8642ff275ae93983ff2 Mon Sep 17 00:00:00 2001 From: Yossef Hisham <93604359+yossev@users.noreply.github.com> Date: Fri, 1 Aug 2025 21:52:44 +0300 Subject: [PATCH] Optimize prefix sum computation in Lucene99HnswVectorsReader Replaced the two-step prefix sum loop in `Lucene99HnswVectorsReader` with a single-loop variant that avoids redundant memory access and improves performance. Previous approach: - Read first value separately. - Then used previous buffer element + readVInt(). New approach: - Accumulates sum in a single pass and assigns directly. This change follows the suggestion from issue #14979 and has the same functional behavior with slightly better efficiency. --- .../lucene/codecs/lucene99/Lucene99HnswVectorsReader.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java index faa885f7b2ea..a16e3dc8b06e 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java @@ -552,9 +552,11 @@ public void seek(int level, int targetOrd) throws IOException { arcCount = dataIn.readVInt(); assert arcCount <= currentNeighborsBuffer.length : "too many neighbors: " + arcCount; if (arcCount > 0) { - currentNeighborsBuffer[0] = dataIn.readVInt(); - for (int i = 1; i < arcCount; i++) { - currentNeighborsBuffer[i] = currentNeighborsBuffer[i - 1] + dataIn.readVInt(); + // Faster prefix sum computation (see #14979) + int sum = 0; + for (int i = 0; i < arcCount; i++) { + sum += dataIn.readVInt(); + currentNeighborsBuffer[i] = sum; } } arc = -1;