Skip to content

Apache Lucene open-source search software

Add Integer awareness to RamUsageEstimator.sizeOf (#11715)

Mike Drobdbc7a9764a84
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 8b2935f9986..943315f6190 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -97,6 +97,8 @@ Improvements
---------------------
* LUCENE-10592: Build HNSW Graph on indexing. (Mayya Sharipova, Adrien Grand, Julie Tibshirani)
+* GITHUB#11715: Add Integer awareness to RamUsageEstimator.sizeOf (Mike Drob)
+
Optimizations
---------------------
@@ -18768,4 +18770,4 @@ First open source release.
The code has been re-organized into a new package and directory
structure for this release. It builds OK, but has not been tested
-beyond that since the re-organization.
\ No newline at end of file
+beyond that since the re-organization.
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 e28e2a7f73c..8a1127e6c5b 100644
--- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
+++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java
@@ -107,8 +107,7 @@ public final class RamUsageEstimator {
primitiveSizes = Collections.unmodifiableMap(primitiveSizesMap);
}
- /** JVMs typically cache small longs. This tries to find out what the range is. */
- static final int LONG_SIZE, STRING_SIZE;
+ static final int INTEGER_SIZE, LONG_SIZE, STRING_SIZE;
/** For testing only */
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;
}
+ INTEGER_SIZE = (int) shallowSizeOfInstance(Integer.class);
LONG_SIZE = (int) shallowSizeOfInstance(Long.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
- * its shallow size otherwise.
+ * Return the shallow size of the provided {@link Integer} object. Ignores the possibility that
+ * 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;
}
@@ -456,6 +464,8 @@ public final class RamUsageEstimator {
size = sizeOf((float[]) o);
} else if (o instanceof int[]) {
size = sizeOf((int[]) o);
+ } else if (o instanceof Integer) {
+ size = sizeOf((Integer) o);
} else if (o instanceof Long) {
size = sizeOf((Long) o);
} else if (o instanceof long[]) {