Skip to content

Commit 36094ac

Browse files
committed
Replace hashmap by a counter in TrackingPostingsInMemoryBytesCodec
1 parent 2864dd8 commit 36094ac

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

server/src/main/java/org/elasticsearch/index/codec/TrackingPostingsInMemoryBytesCodec.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
import org.apache.lucene.codecs.FilterCodec;
1616
import org.apache.lucene.codecs.NormsProducer;
1717
import org.apache.lucene.codecs.PostingsFormat;
18+
import org.apache.lucene.index.FieldInfo;
1819
import org.apache.lucene.index.FieldInfos;
1920
import org.apache.lucene.index.Fields;
2021
import org.apache.lucene.index.FilterLeafReader;
2122
import org.apache.lucene.index.SegmentReadState;
2223
import org.apache.lucene.index.SegmentWriteState;
2324
import org.apache.lucene.index.Terms;
2425
import org.apache.lucene.index.TermsEnum;
25-
import org.apache.lucene.internal.hppc.IntIntHashMap;
2626
import org.apache.lucene.util.BytesRef;
2727
import org.elasticsearch.common.util.FeatureFlag;
2828

@@ -63,22 +63,18 @@ public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException
6363
static final class TrackingLengthFieldsConsumer extends FieldsConsumer {
6464
final SegmentWriteState state;
6565
final FieldsConsumer in;
66-
final IntIntHashMap termsBytesPerField;
66+
final long[] totalBytes;
6767

6868
TrackingLengthFieldsConsumer(SegmentWriteState state, FieldsConsumer in) {
6969
this.state = state;
7070
this.in = in;
71-
this.termsBytesPerField = new IntIntHashMap(state.fieldInfos.size());
71+
this.totalBytes = new long[1];
7272
}
7373

7474
@Override
7575
public void write(Fields fields, NormsProducer norms) throws IOException {
76-
in.write(new TrackingLengthFields(fields, termsBytesPerField, state.fieldInfos), norms);
77-
long totalBytes = 0;
78-
for (int bytes : termsBytesPerField.values) {
79-
totalBytes += bytes;
80-
}
81-
state.segmentInfo.putAttribute(IN_MEMORY_POSTINGS_BYTES_KEY, Long.toString(totalBytes));
76+
in.write(new TrackingLengthFields(fields, totalBytes, state.fieldInfos), norms);
77+
state.segmentInfo.putAttribute(IN_MEMORY_POSTINGS_BYTES_KEY, Long.toString(totalBytes[0]));
8278
}
8379

8480
@Override
@@ -88,12 +84,18 @@ public void close() throws IOException {
8884
}
8985

9086
static final class TrackingLengthFields extends FilterLeafReader.FilterFields {
91-
final IntIntHashMap termsBytesPerField;
87+
final long[] totalBytes;
88+
final boolean[] seenFields;
9289
final FieldInfos fieldInfos;
9390

94-
TrackingLengthFields(Fields in, IntIntHashMap termsBytesPerField, FieldInfos fieldInfos) {
91+
TrackingLengthFields(Fields in, long[] totalBytes, FieldInfos fieldInfos) {
9592
super(in);
96-
this.termsBytesPerField = termsBytesPerField;
93+
this.totalBytes = totalBytes;
94+
int maxFieldNumber = 0;
95+
for (FieldInfo info : fieldInfos) {
96+
maxFieldNumber = Math.max(info.getFieldNumber(), maxFieldNumber);
97+
}
98+
this.seenFields = new boolean[maxFieldNumber + 1];
9799
this.fieldInfos = fieldInfos;
98100
}
99101

@@ -104,10 +106,12 @@ public Terms terms(String field) throws IOException {
104106
return null;
105107
}
106108
int fieldNum = fieldInfos.fieldInfo(field).number;
107-
return new TrackingLengthTerms(
108-
terms,
109-
bytes -> termsBytesPerField.put(fieldNum, Math.max(termsBytesPerField.getOrDefault(fieldNum, 0), bytes))
110-
);
109+
return new TrackingLengthTerms(terms, bytes -> {
110+
if (seenFields[fieldNum] == false) {
111+
totalBytes[0] += bytes;
112+
seenFields[fieldNum] = true;
113+
}
114+
});
111115
}
112116
}
113117

0 commit comments

Comments
 (0)