LUCENE-10129: Add RamUsageEstimator.shallowSizeOf() for primitive arrays (#367)

Co-authored-by: Stefan Vodita <voditas@amazon.com>
This commit is contained in:
Stefan Vodita 2021-10-15 16:45:04 +03:00 committed by GitHub
parent c9e56d27a3
commit 560f71b47d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 0 deletions

View File

@ -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).

View File

@ -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) {

View File

@ -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));
}
}