Skip to content

Commit

Permalink
Fix ram estimate and its test for PackedInts.NullReader singleton (#1…
Browse files Browse the repository at this point in the history
…3250)

Correct estimate for singleton to return `0` and use custom accumulator in tests to
fix assertions. Tried not doing this in #13232 but turns out we need a little complexity here
since the singleton is recognized by the `RamUsageTester` in so far that is only counted
once if it shows up repeatedly in an array or so.

fixes #13249
  • Loading branch information
original-brownbear authored and benwtrent committed Apr 1, 2024
1 parent 34f1cce commit dd9827f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,10 @@ public int size() {

@Override
public long ramBytesUsed() {
return RamUsageEstimator.alignObjectSize(
RamUsageEstimator.NUM_BYTES_OBJECT_HEADER + Integer.BYTES);
return valueCount == PackedLongValues.DEFAULT_PAGE_SIZE
? 0
: RamUsageEstimator.alignObjectSize(
RamUsageEstimator.NUM_BYTES_OBJECT_HEADER + Integer.BYTES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import org.apache.lucene.store.ByteArrayDataInput;
import org.apache.lucene.store.DataInput;
Expand Down Expand Up @@ -966,6 +969,18 @@ public void testPackedLongValuesOnZeros() {
.ramBytesUsed());
}

private static final class IgnoreNullReaderSingletonAccumulator
extends RamUsageTester.Accumulator {
@Override
public long accumulateObject(
Object o, long shallowSize, Map<Field, Object> fieldValues, Collection<Object> queue) {
if (o == PackedInts.NullReader.forCount(PackedLongValues.DEFAULT_PAGE_SIZE)) {
return 0;
}
return super.accumulateObject(o, shallowSize, fieldValues, queue);
}
}

public void testPackedLongValues() {
final long[] arr =
new long[RandomNumbers.randomIntBetween(random(), 1, TEST_NIGHTLY ? 1000000 : 10000)];
Expand Down Expand Up @@ -1017,7 +1032,8 @@ public void testPackedLongValues() {
for (int i = 0; i < arr.length; ++i) {
buf.add(arr[i]);
if (rarely() && !TEST_NIGHTLY) {
final long expectedBytesUsed = RamUsageTester.ramUsed(buf);
final long expectedBytesUsed =
RamUsageTester.ramUsed(buf, new IgnoreNullReaderSingletonAccumulator());
final long computedBytesUsed = buf.ramBytesUsed();
assertEquals(expectedBytesUsed, computedBytesUsed);
}
Expand All @@ -1044,7 +1060,8 @@ public void testPackedLongValues() {
}
assertFalse(it.hasNext());

final long expectedBytesUsed = RamUsageTester.ramUsed(values);
final long expectedBytesUsed =
RamUsageTester.ramUsed(values, new IgnoreNullReaderSingletonAccumulator());
final long computedBytesUsed = values.ramBytesUsed();
assertEquals(expectedBytesUsed, computedBytesUsed);
}
Expand Down

0 comments on commit dd9827f

Please sign in to comment.