Skip to content

Apache Lucene open-source search software

SegmentTermsEnumFrame: avoid unnecessary array allocations that waste memory (#15618)

Misha Dmitriev0a699fa4d2db
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 5b64315c514..fdfc63fd795 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -326,6 +326,8 @@ Optimizations
* GITHUB#15592: Optimize DocIdSetIteratorAcceptDocs#cost. (Shubham Chaudhary)
+* GITHUB#15618: SegmentTermsEnumFrame: avoid unnecessary array allocations that waste memory (Misha Dmitriev)
+
Bug Fixes
---------------------
* GITHUB#14161: PointInSetQuery's constructor now throws IllegalArgumentException
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java
index 1f3fcfcd05c..b55d9396886 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene103/blocktree/SegmentTermsEnumFrame.java
@@ -45,13 +45,13 @@ final class SegmentTermsEnumFrame {
long fpEnd;
long totalSuffixBytes; // for stats
- byte[] suffixBytes = new byte[128];
+ byte[] suffixBytes;
final ByteArrayDataInput suffixesReader = new ByteArrayDataInput();
byte[] suffixLengthBytes;
final ByteArrayDataInput suffixLengthsReader;
- byte[] statBytes = new byte[64];
+ byte[] statBytes;
int statsSingletonRunLength = 0;
final ByteArrayDataInput statsReader = new ByteArrayDataInput();
@@ -95,7 +95,7 @@ final class SegmentTermsEnumFrame {
final BlockTermState state;
// metadata buffer
- byte[] bytes = new byte[32];
+ byte[] bytes;
final ByteArrayDataInput bytesReader = new ByteArrayDataInput();
private final SegmentTermsEnum ste;
@@ -105,7 +105,6 @@ final class SegmentTermsEnumFrame {
this.ord = ord;
this.state = ste.fr.parent.postingsReader.newTermState();
this.state.totalTermFreq = -1;
- suffixLengthBytes = new byte[32];
suffixLengthsReader = new ByteArrayDataInput();
}
@@ -192,7 +191,7 @@ final class SegmentTermsEnumFrame {
final long codeL = ste.in.readVLong();
isLeafBlock = (codeL & 0x04) != 0;
final int numSuffixBytes = (int) (codeL >>> 3);
- if (suffixBytes.length < numSuffixBytes) {
+ if (suffixBytes == null || suffixBytes.length < numSuffixBytes) {
suffixBytes = new byte[ArrayUtil.oversize(numSuffixBytes, 1)];
}
try {
@@ -206,7 +205,7 @@ final class SegmentTermsEnumFrame {
int numSuffixLengthBytes = ste.in.readVInt();
allEqual = (numSuffixLengthBytes & 0x01) != 0;
numSuffixLengthBytes >>>= 1;
- if (suffixLengthBytes.length < numSuffixLengthBytes) {
+ if (suffixLengthBytes == null || suffixLengthBytes.length < numSuffixLengthBytes) {
suffixLengthBytes = new byte[ArrayUtil.oversize(numSuffixLengthBytes, 1)];
}
if (allEqual) {
@@ -227,7 +226,7 @@ final class SegmentTermsEnumFrame {
// stats
int numBytes = ste.in.readVInt();
- if (statBytes.length < numBytes) {
+ if (statBytes == null || statBytes.length < numBytes) {
statBytes = new byte[ArrayUtil.oversize(numBytes, 1)];
}
ste.in.readBytes(statBytes, 0, numBytes);
@@ -243,7 +242,7 @@ final class SegmentTermsEnumFrame {
// that's rare so won't help much
// metadata
numBytes = ste.in.readVInt();
- if (bytes.length < numBytes) {
+ if (bytes == null || bytes.length < numBytes) {
bytes = new byte[ArrayUtil.oversize(numBytes, 1)];
}
ste.in.readBytes(bytes, 0, numBytes);