Skip to content

Commit ba201a2

Browse files
committed
Speed up stored field spec management
When you load stored fields from lucene you have get all of your ducks in a row first or it'll be really slow. You need to get a list of all of the fields you want so you can ignore the ones that you don't. We do this with the `StoredFieldsSpec` which is immutable and has a `merge` method. It's quite common to merge a few dozen of these specs together to prepare for the fetch phase or for a new segment when loading fields from ESQL. When I was loading thousands of fields in ESQL I noticed that the merge was slowing things down marginally. This skips a big chunk of the merge in the common case that we don't have to load any named stored fields.
1 parent 018b3ce commit ba201a2

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

server/src/main/java/org/elasticsearch/search/fetch/StoredFieldsSpec.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ public StoredFieldsSpec merge(StoredFieldsSpec other) {
4242
if (this == other) {
4343
return this;
4444
}
45-
Set<String> mergedFields = new HashSet<>(this.requiredStoredFields);
46-
mergedFields.addAll(other.requiredStoredFields);
45+
Set<String> mergedFields;
46+
if (other.requiredStoredFields.isEmpty()) {
47+
mergedFields = this.requiredStoredFields;
48+
} else {
49+
mergedFields = new HashSet<>(this.requiredStoredFields);
50+
mergedFields.addAll(other.requiredStoredFields);
51+
}
4752
return new StoredFieldsSpec(
4853
this.requiresSource || other.requiresSource,
4954
this.requiresMetadata || other.requiresMetadata,

0 commit comments

Comments
 (0)