Skip to content

Commit 236b955

Browse files
Speedup SearchResponse serialization (#123211)
No need to have a nested concat here. There's obviously lots and lots of room for optimization on this one, but just flattening out one obvious step here outright halves the number of method calls required when serializing a search response. Given that method calls can consume up to half the serialization cost this change might massively speed up some usecases.
1 parent 2a9de3f commit 236b955

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

server/src/main/java/org/elasticsearch/action/search/SearchResponse.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -382,23 +382,23 @@ public Clusters getClusters() {
382382
@Override
383383
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
384384
assert hasReferences();
385-
return Iterators.concat(
386-
ChunkedToXContentHelper.startObject(),
387-
this.innerToXContentChunked(params),
388-
ChunkedToXContentHelper.endObject()
389-
);
385+
return getToXContentIterator(true, params);
390386
}
391387

392388
public Iterator<? extends ToXContent> innerToXContentChunked(ToXContent.Params params) {
389+
return getToXContentIterator(false, params);
390+
}
391+
392+
private Iterator<ToXContent> getToXContentIterator(boolean wrapInObject, ToXContent.Params params) {
393393
return Iterators.concat(
394+
wrapInObject ? ChunkedToXContentHelper.startObject() : Collections.emptyIterator(),
394395
ChunkedToXContentHelper.chunk(SearchResponse.this::headerToXContent),
395396
Iterators.single(clusters),
396-
Iterators.concat(
397-
hits.toXContentChunked(params),
398-
aggregations == null ? Collections.emptyIterator() : ChunkedToXContentHelper.chunk(aggregations),
399-
suggest == null ? Collections.emptyIterator() : ChunkedToXContentHelper.chunk(suggest),
400-
profileResults == null ? Collections.emptyIterator() : ChunkedToXContentHelper.chunk(profileResults)
401-
)
397+
hits.toXContentChunked(params),
398+
aggregations == null ? Collections.emptyIterator() : ChunkedToXContentHelper.chunk(aggregations),
399+
suggest == null ? Collections.emptyIterator() : ChunkedToXContentHelper.chunk(suggest),
400+
profileResults == null ? Collections.emptyIterator() : ChunkedToXContentHelper.chunk(profileResults),
401+
wrapInObject ? ChunkedToXContentHelper.endObject() : Collections.emptyIterator()
402402
);
403403
}
404404

0 commit comments

Comments
 (0)