Skip to content

Commit

Permalink
Introduce singleton for PackedInts.NullReader of size 256 (#13232)
Browse files Browse the repository at this point in the history
Size 256 is very common here throguh the monotonic long values default
page size. In ES we're seing many MB O(10M) of duplicate instances of this
size relatively quickly.
=> adding a singleton for it to save some heap
  • Loading branch information
original-brownbear committed Mar 29, 2024
1 parent 32d6920 commit c41eb22
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ Optimizations

* GITHUB#13224: Use singleton for all-zeros DirectMonotonicReader.Meta (Armin Braun)

* GITHUB#13232 : Introduce singleton for PackedInts.NullReader of size 256 (Armin Braun)

Bug Fixes
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,20 @@ public String toString() {
/** A {@link Reader} which has all its values equal to 0 (bitsPerValue = 0). */
public static final class NullReader extends Reader {

private static final NullReader DEFAULT_PACKED_LONG_VALUES_PAGE_SIZE =
new NullReader(PackedLongValues.DEFAULT_PAGE_SIZE);

private final int valueCount;

public static NullReader forCount(int valueCount) {
if (valueCount == PackedLongValues.DEFAULT_PAGE_SIZE) {
return DEFAULT_PACKED_LONG_VALUES_PAGE_SIZE;
}
return new NullReader(valueCount);
}

/** Sole constructor. */
public NullReader(int valueCount) {
private NullReader(int valueCount) {
this.valueCount = valueCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void pack(long[] values, int numValues, int block, float acceptableOverheadRatio

// build a new packed reader
if (minValue == 0 && maxValue == 0) {
this.values[block] = new PackedInts.NullReader(numValues);
this.values[block] = PackedInts.NullReader.forCount(numValues);
} else {
final int bitsRequired = minValue < 0 ? 64 : PackedInts.bitsRequired(maxValue);
final PackedInts.Mutable mutable =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public void testFill() {
public void testPackedIntsNull() {
// must be > 10 for the bulk reads below
int size = TestUtil.nextInt(random(), 11, 256);
Reader packedInts = new PackedInts.NullReader(size);
Reader packedInts = PackedInts.NullReader.forCount(size);
assertEquals(0, packedInts.get(TestUtil.nextInt(random(), 0, size - 1)));
long[] arr = new long[size + 10];
int r;
Expand Down

0 comments on commit c41eb22

Please sign in to comment.