Apache Solr open-source search software
LUCENE-5773: Fix ram usage estimation on PositiveIntOutputs.
Adrien Grand79351129ef14
diff --git a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.javaindex 66fe7933d56..f15032dd0b3 100644--- a/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java+++ b/lucene/core/src/java/org/apache/lucene/util/RamUsageEstimator.java@@ -130,6 +130,12 @@ public final class RamUsageEstimator {*/private final static EnumSet<JvmFeature> supportedFeatures;+ /**+ * JVMs typically cache small longs. This tries to find out what the range is.+ */+ private static final long LONG_CACHE_MIN_VALUE, LONG_CACHE_MAX_VALUE;+ private static final int LONG_SIZE;+/*** Initialize constants and try to collect information about the JVM internals.*/@@ -232,6 +238,20 @@ public final class RamUsageEstimator {JVM_INFO_STRING = "[JVM: " +Constants.JVM_NAME + ", " + Constants.JVM_VERSION + ", " + Constants.JVM_VENDOR + ", " +Constants.JAVA_VENDOR + ", " + Constants.JAVA_VERSION + "]";++ long longCacheMinValue = 0;+ while (longCacheMinValue > Long.MIN_VALUE+ && Long.valueOf(longCacheMinValue - 1) == Long.valueOf(longCacheMinValue - 1)) {+ longCacheMinValue -= 1;+ }+ long longCacheMaxValue = -1;+ while (longCacheMaxValue < Long.MAX_VALUE+ && Long.valueOf(longCacheMaxValue + 1) == Long.valueOf(longCacheMaxValue + 1)) {+ longCacheMaxValue += 1;+ }+ LONG_CACHE_MIN_VALUE = longCacheMinValue;+ LONG_CACHE_MAX_VALUE = longCacheMaxValue;+ LONG_SIZE = (int) shallowSizeOfInstance(Long.class);}@@ -266,7 +286,18 @@ public final class RamUsageEstimator {size += (long) NUM_BYTES_OBJECT_ALIGNMENT - 1L;return size - (size % NUM_BYTES_OBJECT_ALIGNMENT);}-++ /**+ * Return the size of the provided {@link Long} object, returning 0 if it is+ * cached by the JVM and its shallow size otherwise.+ */+ public static long sizeOf(Long value) {+ if (value >= LONG_CACHE_MIN_VALUE && value <= LONG_CACHE_MAX_VALUE) {+ return 0;+ }+ return LONG_SIZE;+ }+/** Returns the size in bytes of the byte[] object. */public static long sizeOf(byte[] arr) {return alignObjectSize((long) NUM_BYTES_ARRAY_HEADER + arr.length);diff --git a/lucene/core/src/java/org/apache/lucene/util/fst/PositiveIntOutputs.java b/lucene/core/src/java/org/apache/lucene/util/fst/PositiveIntOutputs.javaindex d13648af877..83136b4b2fd 100644--- a/lucene/core/src/java/org/apache/lucene/util/fst/PositiveIntOutputs.java+++ b/lucene/core/src/java/org/apache/lucene/util/fst/PositiveIntOutputs.java@@ -21,6 +21,7 @@ import java.io.IOException;import org.apache.lucene.store.DataInput;import org.apache.lucene.store.DataOutput;+import org.apache.lucene.util.RamUsageEstimator;/*** An FST {@link Outputs} implementation where each output@@ -119,4 +120,9 @@ public final class PositiveIntOutputs extends Outputs<Long> {public String toString() {return "PositiveIntOutputs";}++ @Override+ public long ramBytesUsed(Long output) {+ return RamUsageEstimator.sizeOf(output);+ }}