Apache Lucene open-source search software
Reduce memory usage of `NeighborArray` (#15597)
Viliam Durinafb69171d0a48
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 6905f56af34..9f34e397c10 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -104,6 +104,8 @@ Optimizations* GITHUB#15085, GITHUB#15092: Hunspell suggestions: Ensure candidate roots are not worse before updating. (Ilia Permiashkin)+* GITHUB#15597: Reduce memory usage of NeighborArray (Viliam Durina)+Bug Fixes---------------------* GITHUB#14049: Randomize KNN codec params in RandomCodec. Fixes scalar quantization div-by-zerodiff --git a/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java b/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.javaindex c43cd681440..e8ebef29d44 100644--- a/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java+++ b/lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java@@ -42,7 +42,6 @@ public class NeighborArray {private final MaxSizedFloatArrayList scores;private final MaxSizedIntArrayList nodes;private int sortedNodeSize;- private long ramBytesUsed = BASE_RAM_BYTES_USED;private final LongConsumer onHeapMemoryUsageListener;public NeighborArray(int maxSize, boolean descOrder) {@@ -53,11 +52,11 @@ public class NeighborArray {this.maxSize = maxSize;nodes = new MaxSizedIntArrayList(maxSize, maxSize / 8);scores = new MaxSizedFloatArrayList(maxSize, maxSize / 8);- this.ramBytesUsed += nodes.ramBytesUsed() + scores.ramBytesUsed();this.scoresDescOrder = descOrder;this.onHeapMemoryUsageListener = onHeapMemoryUsageListener;if (onHeapMemoryUsageListener != null) {- onHeapMemoryUsageListener.accept(ramBytesUsed);+ onHeapMemoryUsageListener.accept(+ BASE_RAM_BYTES_USED + nodes.ramBytesUsed() + scores.ramBytesUsed());}}@@ -145,7 +144,7 @@ public class NeighborArray {int[] uncheckedIndexes = new int[size - sortedNodeSize];int count = 0;while (sortedNodeSize != size) {- // TODO: Instead of do an array copy on every insertion, I think we can do better here:+ // TODO: Instead of doing an array copy on every insertion, I think we can do better here:// Remember the insertion point of each unsorted node and insert them altogether// We can save several array copy by doing thatuncheckedIndexes[count] = insertSortedInternal(scorer); // sortedNodeSize is increased inside