jpountz commented on code in PR #12844:
URL: https://github.com/apache/lucene/pull/12844#discussion_r1407925571


##########
lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java:
##########
@@ -330,6 +330,29 @@ public static int[] growExact(int[] array, int newLength) {
     return copy;
   }
 
+  /**
+   * Returns an array whose size is at least {@code minLength}, generally 
over-allocating
+   * exponentially, but never allocating more than {@code maxLength} elements.
+   */
+  public static int[] growInRange(int[] array, int minLength, int maxLength) {
+    if (array.length >= minLength) {
+      return array;
+    }
+    if (minLength > maxLength) {
+      throw new IllegalArgumentException(
+          "requested minimum array length "
+              + minLength
+              + " is larger than requested maximum array length "
+              + maxLength);
+    }

Review Comment:
   Should we validate this first, and only then check if the array is already 
large enough?



##########
lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java:
##########
@@ -31,18 +33,21 @@
  *
  * @lucene.internal
  */
-public class NeighborArray {
+public class NeighborArray implements Accountable {
+  private static final int INITIAL_CAPACITY = 10;

Review Comment:
   @msokolov @benwtrent Can you double check it's ok to grow this array on 
demand vs. pre-sizing like today? It looks like it could save significant 
amounts of memory?



##########
lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java:
##########
@@ -206,4 +211,12 @@ private int descSortFindRightMostInsertionPoint(float 
newScore, int bound) {
     }
     return start;
   }
+
+  @Override
+  public long ramBytesUsed() {
+    return (long) node.length * (Integer.BYTES + Float.BYTES)
+        + RamUsageEstimator.NUM_BYTES_ARRAY_HEADER * 2L
+        + RamUsageEstimator.NUM_BYTES_OBJECT_REF * 2L
+        + Integer.BYTES * 5;

Review Comment:
   Let's use the RamUsageEstimator utility functions instead of estimating RAM 
usage manually? (`shallowSizeOfInstance` and `sizeOf` specifically)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to