You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2021/10/15 13:45:07 UTC

[lucene] branch main updated: LUCENE-10129: Add RamUsageEstimator.shallowSizeOf() for primitive arrays (#367)

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 560f71b  LUCENE-10129: Add RamUsageEstimator.shallowSizeOf() for primitive arrays (#367)
560f71b is described below

commit 560f71b47d3969a37b59a39cce85f6868f3c92be
Author: Stefan Vodita <41...@users.noreply.github.com>
AuthorDate: Fri Oct 15 16:45:04 2021 +0300

    LUCENE-10129: Add RamUsageEstimator.shallowSizeOf() for primitive arrays (#367)
    
    Co-authored-by: Stefan Vodita <vo...@amazon.com>
---
 lucene/CHANGES.txt                                 |  4 +++
 .../org/apache/lucene/util/RamUsageEstimator.java  | 40 ++++++++++++++++++++++
 .../apache/lucene/util/TestRamUsageEstimator.java  |  8 +++++
 3 files changed, 52 insertions(+)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 192959e..5b6c9ec 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -163,6 +163,10 @@ API Changes
 
 Improvements
 
+* LUCENE-10129: RamUsageEstimator overloads the shallowSizeOf method for primitive arrays
+  to avoid falling back on shallowSizeOf(Object), which could lead to performance traps.
+  (Robert Muir, Uwe Schindler, Stefan Vodita)
+
 * LUCENE-10139: ExternalRefSorter returns a covariant with a subtype of BytesRefIterator
   that is Closeable. (Dawid Weiss).
 
diff --git a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
index b1b0363..656bf75 100644
--- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
+++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
@@ -486,6 +486,46 @@ public final class RamUsageEstimator {
     return alignObjectSize(size);
   }
 
+  /** Returns the size in bytes of the byte[] object. */
+  public static long shallowSizeOf(byte[] arr) {
+    return sizeOf(arr);
+  }
+
+  /** Returns the size in bytes of the boolean[] object. */
+  public static long shallowSizeOf(boolean[] arr) {
+    return sizeOf(arr);
+  }
+
+  /** Returns the size in bytes of the char[] object. */
+  public static long shallowSizeOf(char[] arr) {
+    return sizeOf(arr);
+  }
+
+  /** Returns the size in bytes of the short[] object. */
+  public static long shallowSizeOf(short[] arr) {
+    return sizeOf(arr);
+  }
+
+  /** Returns the size in bytes of the int[] object. */
+  public static long shallowSizeOf(int[] arr) {
+    return sizeOf(arr);
+  }
+
+  /** Returns the size in bytes of the float[] object. */
+  public static long shallowSizeOf(float[] arr) {
+    return sizeOf(arr);
+  }
+
+  /** Returns the size in bytes of the long[] object. */
+  public static long shallowSizeOf(long[] arr) {
+    return sizeOf(arr);
+  }
+
+  /** Returns the size in bytes of the double[] object. */
+  public static long shallowSizeOf(double[] arr) {
+    return sizeOf(arr);
+  }
+
   /** Returns the shallow size in bytes of the Object[] object. */
   // Use this method instead of #shallowSizeOf(Object) to avoid costly reflection
   public static long shallowSizeOf(Object[] arr) {
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
index baa1ca9..13f892a 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestRamUsageEstimator.java
@@ -65,41 +65,49 @@ public class TestRamUsageEstimator extends LuceneTestCase {
     {
       byte[] array = new byte[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
 
     {
       boolean[] array = new boolean[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
 
     {
       char[] array = new char[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
 
     {
       short[] array = new short[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
 
     {
       int[] array = new int[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
 
     {
       float[] array = new float[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
 
     {
       long[] array = new long[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
 
     {
       double[] array = new double[rnd.nextInt(1024)];
       assertEquals(sizeOf(array), sizeOf((Object) array));
+      assertEquals(shallowSizeOf(array), sizeOf((Object) array));
     }
   }