LUCENE-5773: Fix ram usage estimation on PositiveIntOutputs.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1603782 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2014-06-19 09:31:49 +00:00
parent ec2eaaafad
commit 79351129ef
2 changed files with 38 additions and 1 deletions

View File

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

View File

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