Add Integer awareness to RamUsageEstimator.sizeOf (#11715)

Additionally, update comments to reflect that we have not been VM cache-aware for a long time now.
This commit is contained in:
Mike Drob 2022-08-25 15:18:08 -05:00 committed by GitHub
parent 1d54299011
commit dbc7a9764a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -97,6 +97,8 @@ Improvements
--------------------- ---------------------
* LUCENE-10592: Build HNSW Graph on indexing. (Mayya Sharipova, Adrien Grand, Julie Tibshirani) * LUCENE-10592: Build HNSW Graph on indexing. (Mayya Sharipova, Adrien Grand, Julie Tibshirani)
* GITHUB#11715: Add Integer awareness to RamUsageEstimator.sizeOf (Mike Drob)
Optimizations Optimizations
--------------------- ---------------------

View File

@ -107,8 +107,7 @@ public final class RamUsageEstimator {
primitiveSizes = Collections.unmodifiableMap(primitiveSizesMap); primitiveSizes = Collections.unmodifiableMap(primitiveSizesMap);
} }
/** JVMs typically cache small longs. This tries to find out what the range is. */ static final int INTEGER_SIZE, LONG_SIZE, STRING_SIZE;
static final int LONG_SIZE, STRING_SIZE;
/** For testing only */ /** For testing only */
static final boolean JVM_IS_HOTSPOT_64BIT; static final boolean JVM_IS_HOTSPOT_64BIT;
@ -187,6 +186,7 @@ public final class RamUsageEstimator {
NUM_BYTES_ARRAY_HEADER = NUM_BYTES_OBJECT_HEADER + Integer.BYTES; NUM_BYTES_ARRAY_HEADER = NUM_BYTES_OBJECT_HEADER + Integer.BYTES;
} }
INTEGER_SIZE = (int) shallowSizeOfInstance(Integer.class);
LONG_SIZE = (int) shallowSizeOfInstance(Long.class); LONG_SIZE = (int) shallowSizeOfInstance(Long.class);
STRING_SIZE = (int) shallowSizeOfInstance(String.class); STRING_SIZE = (int) shallowSizeOfInstance(String.class);
} }
@ -208,10 +208,18 @@ public final class RamUsageEstimator {
} }
/** /**
* Return the size of the provided {@link Long} object, returning 0 if it is cached by the JVM and * Return the shallow size of the provided {@link Integer} object. Ignores the possibility that
* its shallow size otherwise. * this object is part of the VM IntegerCache
*/ */
public static long sizeOf(Long value) { public static long sizeOf(Integer ignored) {
return INTEGER_SIZE;
}
/**
* Return the shallow size of the provided {@link Long} object. Ignores the possibility that this
* object is part of the VM LongCache
*/
public static long sizeOf(Long ignored) {
return LONG_SIZE; return LONG_SIZE;
} }
@ -456,6 +464,8 @@ public final class RamUsageEstimator {
size = sizeOf((float[]) o); size = sizeOf((float[]) o);
} else if (o instanceof int[]) { } else if (o instanceof int[]) {
size = sizeOf((int[]) o); size = sizeOf((int[]) o);
} else if (o instanceof Integer) {
size = sizeOf((Integer) o);
} else if (o instanceof Long) { } else if (o instanceof Long) {
size = sizeOf((Long) o); size = sizeOf((Long) o);
} else if (o instanceof long[]) { } else if (o instanceof long[]) {