Skip to content

Commit ddfdd40

Browse files
Reduce footprint of codec instances further (#114072)
Lots of effectively singleton objects here and fields that can be made static, saves a little more on the per-index overhead and might reveal further simplifications.
1 parent 38a0711 commit ddfdd40

File tree

13 files changed

+34
-34
lines changed

13 files changed

+34
-34
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/index/codec/tsdb/internal/DecodeBenchmark.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.store.ByteArrayDataInput;
1313
import org.apache.lucene.store.ByteArrayDataOutput;
1414
import org.apache.lucene.store.DataOutput;
15+
import org.elasticsearch.index.codec.tsdb.DocValuesForUtil;
1516
import org.openjdk.jmh.infra.Blackhole;
1617

1718
import java.io.IOException;
@@ -43,7 +44,7 @@ public void setupInvocation(int bitsPerValue) {
4344

4445
@Override
4546
public void benchmark(int bitsPerValue, Blackhole bh) throws IOException {
46-
forUtil.decode(bitsPerValue, this.dataInput, this.output);
47+
DocValuesForUtil.decode(bitsPerValue, this.dataInput, this.output);
4748
bh.consume(this.output);
4849
}
4950
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Elasticsearch814Codec() {
6767
*/
6868
public Elasticsearch814Codec(Zstd814StoredFieldsFormat.Mode mode) {
6969
super("Elasticsearch814", lucene99Codec);
70-
this.storedFieldsFormat = new Zstd814StoredFieldsFormat(mode);
70+
this.storedFieldsFormat = mode.getFormat();
7171
}
7272

7373
@Override

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,27 @@
2828
*/
2929
public class Elasticsearch816Codec extends CodecService.DeduplicateFieldInfosCodec {
3030

31+
private static final Lucene912Codec LUCENE_912_CODEC = new Lucene912Codec();
32+
private static final PostingsFormat defaultPostingsFormat = new Lucene912PostingsFormat();
33+
private static final DocValuesFormat defaultDVFormat = new Lucene90DocValuesFormat();
34+
private static final KnnVectorsFormat defaultKnnVectorsFormat = new Lucene99HnswVectorsFormat();
35+
3136
private final StoredFieldsFormat storedFieldsFormat;
3237

33-
private final PostingsFormat defaultPostingsFormat;
3438
private final PostingsFormat postingsFormat = new PerFieldPostingsFormat() {
3539
@Override
3640
public PostingsFormat getPostingsFormatForField(String field) {
3741
return Elasticsearch816Codec.this.getPostingsFormatForField(field);
3842
}
3943
};
4044

41-
private final DocValuesFormat defaultDVFormat;
4245
private final DocValuesFormat docValuesFormat = new PerFieldDocValuesFormat() {
4346
@Override
4447
public DocValuesFormat getDocValuesFormatForField(String field) {
4548
return Elasticsearch816Codec.this.getDocValuesFormatForField(field);
4649
}
4750
};
4851

49-
private final KnnVectorsFormat defaultKnnVectorsFormat;
5052
private final KnnVectorsFormat knnVectorsFormat = new PerFieldKnnVectorsFormat() {
5153
@Override
5254
public KnnVectorsFormat getKnnVectorsFormatForField(String field) {
@@ -64,11 +66,8 @@ public Elasticsearch816Codec() {
6466
* worse space-efficiency or vice-versa.
6567
*/
6668
public Elasticsearch816Codec(Zstd814StoredFieldsFormat.Mode mode) {
67-
super("Elasticsearch816", new Lucene912Codec());
68-
this.storedFieldsFormat = new Zstd814StoredFieldsFormat(mode);
69-
this.defaultPostingsFormat = new Lucene912PostingsFormat();
70-
this.defaultDVFormat = new Lucene90DocValuesFormat();
71-
this.defaultKnnVectorsFormat = new Lucene99HnswVectorsFormat();
69+
super("Elasticsearch816", LUCENE_912_CODEC);
70+
this.storedFieldsFormat = mode.getFormat();
7271
}
7372

7473
@Override

server/src/main/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtil.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,10 @@ public class DocValuesForUtil {
2121
private static final int BITS_IN_FIVE_BYTES = 5 * Byte.SIZE;
2222
private static final int BITS_IN_SIX_BYTES = 6 * Byte.SIZE;
2323
private static final int BITS_IN_SEVEN_BYTES = 7 * Byte.SIZE;
24-
private final int blockSize;
25-
private final byte[] encoded;
24+
private static final int blockSize = ES87TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE;
25+
private final byte[] encoded = new byte[1024];
2626

27-
public DocValuesForUtil() {
28-
this(ES87TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE);
29-
}
30-
31-
private DocValuesForUtil(int blockSize) {
32-
this.blockSize = blockSize;
33-
this.encoded = new byte[1024];
34-
}
27+
public DocValuesForUtil() {}
3528

3629
public static int roundBits(int bitsPerValue) {
3730
if (bitsPerValue > 24 && bitsPerValue <= 32) {
@@ -74,7 +67,7 @@ private void encodeFiveSixOrSevenBytesPerValue(long[] in, int bitsPerValue, fina
7467
out.writeBytes(this.encoded, bytesPerValue * in.length);
7568
}
7669

77-
public void decode(int bitsPerValue, final DataInput in, long[] out) throws IOException {
70+
public static void decode(int bitsPerValue, final DataInput in, long[] out) throws IOException {
7871
if (bitsPerValue <= 24) {
7972
ForUtil.decode(bitsPerValue, in, out);
8073
} else if (bitsPerValue <= 32) {
@@ -88,7 +81,7 @@ public void decode(int bitsPerValue, final DataInput in, long[] out) throws IOEx
8881
}
8982
}
9083

91-
private void decodeFiveSixOrSevenBytesPerValue(int bitsPerValue, final DataInput in, long[] out) throws IOException {
84+
private static void decodeFiveSixOrSevenBytesPerValue(int bitsPerValue, final DataInput in, long[] out) throws IOException {
9285
// NOTE: we expect multibyte values to be written "least significant byte" first
9386
int bytesPerValue = bitsPerValue / Byte.SIZE;
9487
long mask = (1L << bitsPerValue) - 1;

server/src/main/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void decodeOrdinals(DataInput in, long[] out, int bitsPerOrd) throws IOException
275275
Arrays.fill(out, runLen, out.length, v2);
276276
} else if (encoding == 2) {
277277
// bit-packed
278-
forUtil.decode(bitsPerOrd, in, out);
278+
DocValuesForUtil.decode(bitsPerOrd, in, out);
279279
} else if (encoding == 3) {
280280
// cycle encoding
281281
int cycleLength = (int) v1;
@@ -299,7 +299,7 @@ void decode(DataInput in, long[] out) throws IOException {
299299
final int bitsPerValue = token >>> 3;
300300

301301
if (bitsPerValue != 0) {
302-
forUtil.decode(bitsPerValue, in, out);
302+
DocValuesForUtil.decode(bitsPerValue, in, out);
303303
} else {
304304
Arrays.fill(out, 0L);
305305
}

server/src/main/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesProducer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public TermsEnum termsEnum() throws IOException {
355355
}
356356
}
357357

358-
private abstract class BaseSortedSetDocValues extends SortedSetDocValues {
358+
private abstract static class BaseSortedSetDocValues extends SortedSetDocValues {
359359

360360
final SortedSetEntry entry;
361361
final IndexInput data;

server/src/main/java/org/elasticsearch/index/codec/vectors/ES813FlatVectorFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class ES813FlatVectorFormat extends KnnVectorsFormat {
3636

3737
static final String NAME = "ES813FlatVectorFormat";
3838

39-
private final FlatVectorsFormat format = new Lucene99FlatVectorsFormat(DefaultFlatVectorScorer.INSTANCE);
39+
private static final FlatVectorsFormat format = new Lucene99FlatVectorsFormat(DefaultFlatVectorScorer.INSTANCE);
4040

4141
/**
4242
* Sole constructor

server/src/main/java/org/elasticsearch/index/codec/vectors/ES814ScalarQuantizedVectorsFormat.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public class ES814ScalarQuantizedVectorsFormat extends FlatVectorsFormat {
4949

5050
private static final FlatVectorsFormat rawVectorFormat = new Lucene99FlatVectorsFormat(DefaultFlatVectorScorer.INSTANCE);
5151

52+
static final FlatVectorsScorer flatVectorScorer = new ESFlatVectorsScorer(
53+
new ScalarQuantizedVectorScorer(DefaultFlatVectorScorer.INSTANCE)
54+
);
55+
5256
/** The minimum confidence interval */
5357
private static final float MINIMUM_CONFIDENCE_INTERVAL = 0.9f;
5458

@@ -60,7 +64,6 @@ public class ES814ScalarQuantizedVectorsFormat extends FlatVectorsFormat {
6064
* calculated as `1-1/(vector_dimensions + 1)`
6165
*/
6266
public final Float confidenceInterval;
63-
final FlatVectorsScorer flatVectorScorer;
6467

6568
private final byte bits;
6669
private final boolean compress;
@@ -83,7 +86,6 @@ public ES814ScalarQuantizedVectorsFormat(Float confidenceInterval, int bits, boo
8386
throw new IllegalArgumentException("bits must be one of: 4, 7, 8; bits=" + bits);
8487
}
8588
this.confidenceInterval = confidenceInterval;
86-
this.flatVectorScorer = new ESFlatVectorsScorer(new ScalarQuantizedVectorScorer(DefaultFlatVectorScorer.INSTANCE));
8789
this.bits = (byte) bits;
8890
this.compress = compress;
8991
}

server/src/main/java/org/elasticsearch/index/codec/vectors/ES815BitFlatVectorFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ES815BitFlatVectorFormat extends KnnVectorsFormat {
2222

2323
static final String NAME = "ES815BitFlatVectorFormat";
2424

25-
private final FlatVectorsFormat format = new ES815BitFlatVectorsFormat();
25+
private static final FlatVectorsFormat format = new ES815BitFlatVectorsFormat();
2626

2727
/**
2828
* Sole constructor

server/src/main/java/org/elasticsearch/index/codec/vectors/ES815BitFlatVectorsFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class ES815BitFlatVectorsFormat extends FlatVectorsFormat {
2929

30-
private final FlatVectorsFormat delegate = new Lucene99FlatVectorsFormat(FlatBitVectorScorer.INSTANCE);
30+
private static final FlatVectorsFormat delegate = new Lucene99FlatVectorsFormat(FlatBitVectorScorer.INSTANCE);
3131

3232
protected ES815BitFlatVectorsFormat() {
3333
super("ES815BitFlatVectorsFormat");

0 commit comments

Comments
 (0)