Skip to content

Commit 2b18de7

Browse files
committed
Change
1 parent 2226323 commit 2b18de7

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/eson/ESONDeserializationBenchmark.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.xcontent.XContentHelper;
1717
import org.elasticsearch.core.Tuple;
1818
import org.elasticsearch.ingest.ESONSource;
19+
import org.elasticsearch.ingest.ESONXContentSerializer;
1920
import org.elasticsearch.xcontent.ToXContent;
2021
import org.elasticsearch.xcontent.XContentBuilder;
2122
import org.elasticsearch.xcontent.XContentFactory;
@@ -126,6 +127,14 @@ public void writeJSONFromESON(Blackhole bh) throws IOException {
126127
bh.consume(bytes);
127128
}
128129

130+
@Benchmark
131+
public void writeJSONFromESONFlatten(Blackhole bh) throws IOException {
132+
XContentBuilder builder = XContentFactory.contentBuilder(JsonXContent.jsonXContent.type());
133+
ESONXContentSerializer.flattedToXContent(esonObject, builder, ToXContent.EMPTY_PARAMS);
134+
BytesReference bytes = BytesReference.bytes(builder);
135+
bh.consume(bytes);
136+
}
137+
129138
@Benchmark
130139
public void readMap(Blackhole bh) throws IOException {
131140
Tuple<XContentType, Map<String, Object>> tuple = XContentHelper.convertToMap(source, false, XContentType.JSON);
@@ -143,7 +152,7 @@ public void readESON(Blackhole bh) throws IOException {
143152
source.length()
144153
)
145154
) {
146-
ESONSource.ESONObject eson = new ESONSource.Builder(refRecycler).parse(parser);
155+
ESONSource.ESONObject eson = new ESONSource.Builder().parse(parser);
147156
bh.consume(eson);
148157
}
149158
}

server/src/main/java/org/elasticsearch/action/index/IndexRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public IndexRequest source(BytesReference source, XContentType xContentType) {
564564
this.source = Objects.requireNonNull(source);
565565
this.contentType = Objects.requireNonNull(xContentType);
566566
if (useStructuredSource) {
567-
ESONSource.Builder builder = new ESONSource.Builder();
567+
ESONSource.Builder builder = new ESONSource.Builder((int) (source.length() * 0.70));
568568
try {
569569
XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, xContentType);
570570
structuredSource = builder.parse(parser);

server/src/main/java/org/elasticsearch/ingest/ESONSource.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.xcontent.ToXContent;
1818
import org.elasticsearch.xcontent.XContentBuilder;
1919
import org.elasticsearch.xcontent.XContentParser;
20+
import org.elasticsearch.xcontent.XContentString;
2021

2122
import java.io.IOException;
2223
import java.util.AbstractCollection;
@@ -39,11 +40,15 @@ public static class Builder {
3940
private final List<KeyEntry> keyArray;
4041

4142
public Builder() {
42-
this(BytesRefRecycler.NON_RECYCLING_INSTANCE);
43+
this(0);
4344
}
4445

45-
public Builder(Recycler<BytesRef> refRecycler) {
46-
this.bytes = new BytesStreamOutput(128);
46+
public Builder(int expectedSize) {
47+
this(BytesRefRecycler.NON_RECYCLING_INSTANCE, expectedSize);
48+
}
49+
50+
public Builder(Recycler<BytesRef> refRecycler, int expectedSize) {
51+
this.bytes = new BytesStreamOutput(expectedSize);
4752
this.keyArray = new ArrayList<>();
4853
}
4954

@@ -115,9 +120,9 @@ private static Type parseSimpleValue(XContentParser parser, BytesStreamOutput by
115120

116121
return switch (token) {
117122
case VALUE_STRING -> {
118-
byte[] stringBytes = parser.text().getBytes(java.nio.charset.StandardCharsets.UTF_8);
119-
bytes.write(stringBytes);
120-
yield new VariableValue((int) position, stringBytes.length, ValueType.STRING);
123+
XContentString.UTF8Bytes stringBytes = parser.optimizedText().bytes();
124+
bytes.write(stringBytes.bytes(), stringBytes.offset(), stringBytes.length());
125+
yield new VariableValue((int) position, stringBytes.length(), ValueType.STRING);
121126
}
122127
case VALUE_NUMBER -> {
123128
XContentParser.NumberType numberType = parser.numberType();

server/src/main/java/org/elasticsearch/ingest/IngestService.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,14 +1341,9 @@ static String getProcessorName(Processor processor) {
13411341
* Builds a new ingest document from the passed-in index request.
13421342
*/
13431343
private static IngestDocument newIngestDocument(final IndexRequest request) {
1344-
ESONSource.Builder builder = new ESONSource.Builder();
1345-
try (
1346-
XContentParser parser = XContentHelper.createParser(
1347-
XContentParserConfiguration.EMPTY,
1348-
request.source(),
1349-
request.getContentType()
1350-
);
1351-
) {
1344+
BytesReference source = request.source();
1345+
ESONSource.Builder builder = new ESONSource.Builder((int) (source.length() * 0.70));
1346+
try (XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, source, request.getContentType());) {
13521347
ESONSource.ESONObject esonObject = builder.parse(parser);
13531348
return new IngestDocument(
13541349
request.index(),

0 commit comments

Comments
 (0)