Apache Lucene open-source search software
LUCENE-10129: Add RamUsageEstimator.shallowSizeOf() for primitive arrays (#367)
Stefan Vodita560f71b47d39
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 192959ee638..5b6c9ece1bb 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -163,6 +163,10 @@ API ChangesImprovements+* 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 BytesRefIteratorthat 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.javaindex b1b036353c0..656bf75ab99 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 reflectionpublic 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.javaindex baa1ca925a1..13f892a40a1 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));}}