Skip to content

Commit b7e2d59

Browse files
Speed up DocumentParser.innerParseObject (elastic#88715)
Making this a little smaller so it inlines more often and more importantly avoid getting the current field name redundantly before entering the loop which is not free when the parser is a `DotExpandingXContentParser`. This change gives a significant boost to `BeatsMapperBenchmark.benchmarkParseKeywordFields`. Results below are pretty stable, running with a large number of iterations. before: ``` Result "org.elasticsearch.benchmark.index.mapper.BeatsMapperBenchmark.benchmarkParseKeywordFields": 9039.270 ±(99.9%) 28.001 ns/op [Average] (min, avg, max) = (9008.778, 9039.270, 9062.246), stdev = 18.521 CI (99.9%): [9011.269, 9067.271] (assumes normal distribution) ``` after: ``` Result "org.elasticsearch.benchmark.index.mapper.BeatsMapperBenchmark.benchmarkParseKeywordFields": 8645.649 ±(99.9%) 53.677 ns/op [Average] (min, avg, max) = (8568.319, 8645.649, 8688.210), stdev = 35.504 CI (99.9%): [8591.972, 8699.327] (assumes normal distribution) ```
1 parent e3dc098 commit b7e2d59

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,18 @@ private static void throwOnConcreteValue(ObjectMapper mapper, String currentFiel
294294

295295
private static void innerParseObject(DocumentParserContext context, ObjectMapper mapper) throws IOException {
296296

297-
XContentParser.Token token = context.parser().currentToken();
298-
String currentFieldName = context.parser().currentName();
297+
final XContentParser parser = context.parser();
298+
XContentParser.Token token = parser.currentToken();
299+
String currentFieldName = null;
299300
assert token == XContentParser.Token.FIELD_NAME || token == XContentParser.Token.END_OBJECT;
300301

301302
while (token != XContentParser.Token.END_OBJECT) {
302303
if (token == null) {
303-
throwEOF(mapper, currentFieldName);
304+
throwEOF(mapper, context);
304305
}
305306
switch (token) {
306307
case FIELD_NAME:
307-
currentFieldName = context.parser().currentName();
308+
currentFieldName = parser.currentName();
308309
if (currentFieldName.isBlank()) {
309310
throwFieldNameBlank(context, currentFieldName);
310311
}
@@ -324,7 +325,7 @@ private static void innerParseObject(DocumentParserContext context, ObjectMapper
324325
}
325326
break;
326327
}
327-
token = context.parser().nextToken();
328+
token = parser.nextToken();
328329
}
329330
}
330331

@@ -334,12 +335,12 @@ private static void throwFieldNameBlank(DocumentParserContext context, String cu
334335
);
335336
}
336337

337-
private static void throwEOF(ObjectMapper mapper, String currentFieldName) {
338+
private static void throwEOF(ObjectMapper mapper, DocumentParserContext context) throws IOException {
338339
throw new MapperParsingException(
339340
"object mapping for ["
340341
+ mapper.name()
341342
+ "] tried to parse field ["
342-
+ currentFieldName
343+
+ context.parser().currentName()
343344
+ "] as object, but got EOF, has a concrete value been provided to it?"
344345
);
345346
}

0 commit comments

Comments
 (0)