Skip to content

Commit 9fd96a3

Browse files
committed
avoid buffer
1 parent b3b9dbb commit 9fd96a3

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

server/src/main/java/org/elasticsearch/index/codec/tsdb/es819/ES819TSDBDocValuesProducer.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,8 +1404,8 @@ public BlockLoader.Block tryRead(
14041404
boolean nullsFiltered,
14051405
BlockDocValuesReader.ToDouble toDouble
14061406
) throws IOException {
1407-
try (var longs = singletonLongs(factory, toDouble, docs.count() - offset)) {
1408-
return tryRead(longs, docs, offset);
1407+
try (var singletonLongBuilder = singletonLongBuilder(factory, toDouble, docs.count() - offset)) {
1408+
return tryRead(singletonLongBuilder, docs, offset);
14091409
}
14101410
}
14111411

@@ -1566,7 +1566,7 @@ public BlockLoader.Block tryRead(
15661566
assert disi.index() == firstIndex + i : "unexpected disi index " + (firstIndex + i) + "!=" + disi.index();
15671567
}
15681568
}
1569-
try (var longs = singletonLongs(factory, toDouble, valueCount)) {
1569+
try (var singletonLongBuilder = singletonLongBuilder(factory, toDouble, valueCount)) {
15701570
for (int i = 0; i < valueCount;) {
15711571
final int index = firstIndex + i;
15721572
final int blockIndex = index >>> ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SHIFT;
@@ -1580,10 +1580,10 @@ public BlockLoader.Block tryRead(
15801580
decoder.decode(valuesData, currentBlock);
15811581
}
15821582
final int count = Math.min(ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE - blockStartIndex, valueCount - i);
1583-
longs.appendLongs(currentBlock, blockStartIndex, count);
1583+
singletonLongBuilder.appendLongs(currentBlock, blockStartIndex, count);
15841584
i += count;
15851585
}
1586-
return longs.build();
1586+
return singletonLongBuilder.build();
15871587
}
15881588
}
15891589
};
@@ -1881,7 +1881,7 @@ public BlockLoader.Builder endPositionEntry() {
18811881
public void close() {}
18821882
}
18831883

1884-
static BlockLoader.SingletonLongBuilder singletonLongs(
1884+
static BlockLoader.SingletonLongBuilder singletonLongBuilder(
18851885
BlockLoader.BlockFactory factory,
18861886
BlockDocValuesReader.ToDouble toDouble,
18871887
int valueCount
@@ -1897,7 +1897,6 @@ static BlockLoader.SingletonLongBuilder singletonLongs(
18971897
static final class SingletonLongToDoubleDelegate implements BlockLoader.SingletonLongBuilder {
18981898
private final BlockLoader.SingletonDoubleBuilder doubleBuilder;
18991899
private final BlockDocValuesReader.ToDouble toDouble;
1900-
private final double[] buffer = new double[ES819TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE];
19011900

19021901
// The passed builder is used to store the converted double values and produce the final block containing them.
19031902
SingletonLongToDoubleDelegate(BlockLoader.SingletonDoubleBuilder doubleBuilder, BlockDocValuesReader.ToDouble toDouble) {
@@ -1912,11 +1911,7 @@ public BlockLoader.SingletonLongBuilder appendLong(long value) {
19121911

19131912
@Override
19141913
public BlockLoader.SingletonLongBuilder appendLongs(long[] values, int from, int length) {
1915-
assert length <= buffer.length : "length " + length + " > " + buffer.length;
1916-
for (int i = 0; i < length; i++) {
1917-
buffer[i] = toDouble.convert(values[from + i]);
1918-
}
1919-
doubleBuilder.appendDoubles(buffer, 0, length);
1914+
doubleBuilder.appendLongs(toDouble, values, from, length);
19201915
return this;
19211916
}
19221917

server/src/main/java/org/elasticsearch/index/mapper/BlockLoader.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,6 @@ interface BlockFactory {
407407
*/
408408
DoubleBuilder doubles(int expectedCount);
409409

410-
/**
411-
* Creates a block from an array of doubles
412-
*/
413-
Block doubles(double[] values, int expectedCount);
414-
415410
/**
416411
* Build a builder to load dense vectors without any loading constraints.
417412
*/
@@ -575,6 +570,8 @@ interface SingletonDoubleBuilder extends Builder {
575570
SingletonDoubleBuilder appendDouble(double value);
576571

577572
SingletonDoubleBuilder appendDoubles(double[] values, int from, int length);
573+
574+
SingletonDoubleBuilder appendLongs(BlockDocValuesReader.ToDouble toDouble, long[] values, int from, int length);
578575
}
579576

580577
interface LongBuilder extends Builder {

test/framework/src/main/java/org/elasticsearch/index/mapper/TestBlock.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,6 @@ public DoublesBuilder appendDouble(double value) {
120120
return new DoublesBuilder();
121121
}
122122

123-
@Override
124-
public BlockLoader.Block doubles(double[] values, int expectedCount) {
125-
try (BlockLoader.DoubleBuilder builder = doubles(expectedCount)) {
126-
for (double value : values) {
127-
builder.appendDouble(value);
128-
}
129-
return builder.build();
130-
}
131-
}
132-
133123
@Override
134124
public BlockLoader.FloatBuilder denseVectors(int expectedCount, int dimensions) {
135125
class FloatsBuilder extends TestBlock.Builder implements BlockLoader.FloatBuilder {
@@ -286,6 +276,20 @@ public BlockLoader.SingletonDoubleBuilder appendDouble(double value) {
286276
return this;
287277
}
288278

279+
@Override
280+
public BlockLoader.SingletonDoubleBuilder appendLongs(
281+
BlockDocValuesReader.ToDouble toDouble,
282+
long[] longValues,
283+
int from,
284+
int length
285+
) {
286+
for (int i = 0; i < length; i++) {
287+
values[count + i] = toDouble.convert(longValues[from + i]);
288+
}
289+
this.count += length;
290+
return this;
291+
}
292+
289293
@Override
290294
public BlockLoader.Builder appendNull() {
291295
throw new UnsupportedOperationException();

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/read/DelegatingBlockLoaderFactory.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ public BlockLoader.DoubleBuilder doubles(int expectedCount) {
7878
return factory.newDoubleBlockBuilder(expectedCount);
7979
}
8080

81-
@Override
82-
public BlockLoader.Block doubles(double[] values, int expectedCount) {
83-
return factory.newDoubleArrayVector(values, expectedCount).asBlock();
84-
}
85-
8681
@Override
8782
public BlockLoader.FloatBuilder denseVectors(int expectedVectorsCount, int dimensions) {
8883
return factory.newFloatBlockBuilder(expectedVectorsCount * dimensions);

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/read/SingletonDoubleBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.compute.data.Block;
1111
import org.elasticsearch.compute.data.BlockFactory;
1212
import org.elasticsearch.core.Releasable;
13+
import org.elasticsearch.index.mapper.BlockDocValuesReader;
1314
import org.elasticsearch.index.mapper.BlockLoader;
1415

1516
/**
@@ -84,6 +85,15 @@ public BlockLoader.SingletonDoubleBuilder appendDoubles(double[] values, int fro
8485
return this;
8586
}
8687

88+
@Override
89+
public BlockLoader.SingletonDoubleBuilder appendLongs(BlockDocValuesReader.ToDouble toDouble, long[] longValues, int from, int length) {
90+
for (int i = 0; i < length; i++) {
91+
values[count + i] = toDouble.convert(longValues[from + i]);
92+
}
93+
this.count += length;
94+
return this;
95+
}
96+
8797
@Override
8898
public void close() {
8999
blockFactory.adjustBreaker(-valuesSize(values.length));

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/lucene/read/SingletonDoubleBuilderTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ private void testRead(BlockFactory factory) throws IOException {
7272
double value = Double.longBitsToDouble(docValues.longValue());
7373
if (randomBoolean()) {
7474
builder.appendDoubles(new double[] { value }, 0, 1);
75+
} else if (randomBoolean()) {
76+
builder.appendLongs(Double::longBitsToDouble, new long[] { docValues.longValue() }, 0, 1);
7577
} else {
7678
builder.appendDouble(value);
7779
}

0 commit comments

Comments
 (0)