Skip to content

Commit 33f2010

Browse files
authored
Speed up block serialization (#124394) (#124840)
Currently, we use NamedWriteable for serializing blocks. While convenient, it incurs a noticeable performance penalty when pages contain thousands of blocks. Since block types are small and already centered in ElementType, we can safely switch from NamedWriteable to typed code. For example, the NamedWriteable alone of a small page with 10K fields would be 180KB, whereas the new method reduces it to 10KB. Below are the serialization improvements with FROM idx | LIMIT 10000 where the target index has 10K fields: - write_exchange_response executed 173 times took: 73.2ms -> 26.7ms - read_exchange_response executed 173 times took: 49.4ms -> 25.8ms (cherry picked from commit 79a1626)
1 parent b03753d commit 33f2010

File tree

31 files changed

+137
-229
lines changed

31 files changed

+137
-229
lines changed

docs/changelog/124394.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 124394
2+
summary: Avoid `NamedWritable` in block serialization
3+
area: ES|QL
4+
type: enhancement
5+
issues: []

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ static TransportVersion def(int id) {
194194
public static final TransportVersion RETRY_ILM_ASYNC_ACTION_REQUIRE_ERROR_8_19 = def(8_841_0_07);
195195
public static final TransportVersion INFERENCE_CONTEXT_8_X = def(8_841_0_08);
196196
public static final TransportVersion ML_INFERENCE_DEEPSEEK_8_19 = def(8_841_0_09);
197+
public static final TransportVersion ESQL_SERIALIZE_BLOCK_TYPE_CODE = def(8_841_0_10);
197198

198199
/*
199200
* STOP! READ THIS FIRST! No, really,

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BooleanBlock.java

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/BytesRefBlock.java

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/DoubleBlock.java

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/FloatBlock.java

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/IntBlock.java

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/data/LongBlock.java

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/Block.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99

1010
import org.apache.lucene.util.Accountable;
1111
import org.apache.lucene.util.RamUsageEstimator;
12-
import org.elasticsearch.common.io.stream.NamedWriteable;
12+
import org.elasticsearch.common.io.stream.StreamOutput;
13+
import org.elasticsearch.common.io.stream.Writeable;
1314
import org.elasticsearch.common.unit.ByteSizeValue;
1415
import org.elasticsearch.core.RefCounted;
1516
import org.elasticsearch.core.Releasable;
1617
import org.elasticsearch.core.ReleasableIterator;
1718
import org.elasticsearch.core.Releasables;
1819
import org.elasticsearch.index.mapper.BlockLoader;
1920

21+
import java.io.IOException;
22+
2023
/**
2124
* A Block is a columnar representation of homogenous data. It has a position (row) count, and
2225
* various data retrieval methods for accessing the underlying data that is stored at a given
@@ -35,7 +38,7 @@
3538
* <p> Block are immutable and can be passed between threads as long as no two threads hold a reference to
3639
* the same block at the same time.
3740
*/
38-
public interface Block extends Accountable, BlockLoader.Block, NamedWriteable, RefCounted, Releasable {
41+
public interface Block extends Accountable, BlockLoader.Block, Writeable, RefCounted, Releasable {
3942
/**
4043
* The maximum number of values that can be added to one position via lookup.
4144
* TODO maybe make this everywhere?
@@ -329,6 +332,30 @@ static Block[] buildAll(Block.Builder... builders) {
329332
}
330333
}
331334

335+
/**
336+
* Writes only the data of the block to a stream output.
337+
* This method should be used when the type of the block is known during reading.
338+
*/
339+
void writeTo(StreamOutput out) throws IOException;
340+
341+
/**
342+
* Writes the type of the block followed by the block data to a stream output.
343+
* This should be paired with {@link #readTypedBlock(BlockStreamInput)}
344+
*/
345+
static void writeTypedBlock(Block block, StreamOutput out) throws IOException {
346+
block.elementType().writeTo(out);
347+
block.writeTo(out);
348+
}
349+
350+
/**
351+
* Reads the block type and then the block data from a stream input
352+
* This should be paired with {@link #writeTypedBlock(Block, StreamOutput)}
353+
*/
354+
static Block readTypedBlock(BlockStreamInput in) throws IOException {
355+
ElementType elementType = ElementType.readFrom(in);
356+
return elementType.reader.readBlock(in);
357+
}
358+
332359
/**
333360
* Serialization type for blocks: 0 and 1 replace false/true used in pre-8.14
334361
*/

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockWritables.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)