Skip to content

Commit ee38ce6

Browse files
committed
update OnHeapHnswGraph ramBytesUsed method
1 parent b0b4164 commit ee38ce6

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphBuilder.java

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ private void addVectors(int maxOrd) throws IOException {
212212

213213
public void addGraphNode(int node, UpdateableRandomVectorScorer scorer) throws IOException {
214214
addGraphNodeInternal(node, scorer, null);
215+
if (node % 1000 == 0) {
216+
hnsw.updateRamBytesUsedEstimate();
217+
}
215218
}
216219

217220
private void addGraphNodeInternal(int node, UpdateableRandomVectorScorer scorer, IntHashSet eps0)
@@ -444,6 +447,7 @@ void finish() throws IOException {
444447
// see: https://github.com/apache/lucene/issues/14214
445448
// connectComponents();
446449
frozen = true;
450+
hnsw.updateRamBytesUsedEstimate();
447451
}
448452

449453
@SuppressWarnings("unused")

lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.Arrays;
2222
import org.apache.lucene.internal.hppc.FloatArrayList;
2323
import org.apache.lucene.internal.hppc.IntArrayList;
24+
import org.apache.lucene.util.Accountable;
25+
import org.apache.lucene.util.RamUsageEstimator;
2426

2527
/**
2628
* NeighborArray encodes the neighbors of a node and their mutual scores in the HNSW graph as a pair
@@ -30,7 +32,10 @@
3032
*
3133
* @lucene.internal
3234
*/
33-
public class NeighborArray {
35+
public class NeighborArray implements Accountable {
36+
private static final long BASE_RAM_BYTES_USED =
37+
RamUsageEstimator.shallowSizeOfInstance(NeighborArray.class);
38+
3439
private final boolean scoresDescOrder;
3540
private int size;
3641
private final int maxSize;
@@ -308,4 +313,9 @@ private boolean isWorstNonDiverse(
308313
public int maxSize() {
309314
return maxSize;
310315
}
316+
317+
@Override
318+
public long ramBytesUsed() {
319+
return BASE_RAM_BYTES_USED + nodes.ramBytesUsed() + scores.ramBytesUsed();
320+
}
311321
}

lucene/core/src/java/org/apache/lucene/util/hnsw/OnHeapHnswGraph.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public final class OnHeapHnswGraph extends HnswGraph implements Accountable {
6363
private int upto;
6464
private NeighborArray cur;
6565

66+
private volatile long graphRamBytesUsed;
67+
6668
/**
6769
* ctor
6870
*
@@ -292,23 +294,28 @@ private void generateLevelToNodes() {
292294
lastFreezeSize = size();
293295
}
294296

297+
public void updateRamBytesUsedEstimate() {
298+
long currentRamBytesUsedEstimate = RamUsageEstimator.NUM_BYTES_ARRAY_HEADER;
299+
for (int node = 0; node < graph.length; node++) {
300+
if (graph[node] == null) {
301+
continue;
302+
}
303+
304+
for (int i = 0; i < graph[node].length; i++) {
305+
if (graph[node][i] == null) {
306+
continue;
307+
}
308+
currentRamBytesUsedEstimate += graph[node][i].ramBytesUsed();
309+
}
310+
311+
currentRamBytesUsedEstimate += RamUsageEstimator.NUM_BYTES_OBJECT_HEADER;
312+
}
313+
graphRamBytesUsed = currentRamBytesUsedEstimate;
314+
}
315+
295316
@Override
296317
public long ramBytesUsed() {
297-
long neighborArrayBytes0 =
298-
(long) nsize0 * (Integer.BYTES + Float.BYTES)
299-
+ RamUsageEstimator.NUM_BYTES_ARRAY_HEADER * 2L
300-
+ RamUsageEstimator.NUM_BYTES_OBJECT_REF * 2L
301-
+ Integer.BYTES * 3;
302-
long neighborArrayBytes =
303-
(long) nsize * (Integer.BYTES + Float.BYTES)
304-
+ RamUsageEstimator.NUM_BYTES_ARRAY_HEADER * 2L
305-
+ RamUsageEstimator.NUM_BYTES_OBJECT_REF * 2L
306-
+ Integer.BYTES * 3;
307-
long total = 0;
308-
total +=
309-
size() * (neighborArrayBytes0 + RamUsageEstimator.NUM_BYTES_ARRAY_HEADER)
310-
+ RamUsageEstimator.NUM_BYTES_ARRAY_HEADER; // for graph and level 0;
311-
total += nonZeroLevelSize.get() * neighborArrayBytes; // for non-zero level
318+
long total = graphRamBytesUsed; // all NeighborArray
312319
total += 4 * Integer.BYTES; // all int fields
313320
total += 1; // field: noGrowth
314321
total +=

0 commit comments

Comments
 (0)