Skip to content

Commit

Permalink
Make MappedFieldType#meta type "more consistent" (elastic#100041)
Browse files Browse the repository at this point in the history
We use various types for this field depending on the code
path taken. This leads to a rather high cost for serializing
the meta map in field caps (from the virtual call overhead
as well as the fact that we have an unmodifiable wrapped `TreeMap`
here quiet often).
=> this PR makes a bit of an effort to use a consistent type as much as
possible to get more predictable performance.
  • Loading branch information
original-brownbear authored Sep 29, 2023
1 parent e23fd32 commit fd65b61
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static Map<String, IndexFieldCapabilities> retrieveFieldCaps(
false,
false,
null,
Collections.emptyMap()
Map.of()
);
responseMap.put(parentField, fieldCap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,6 @@ public Map<String, IndexFieldCapabilities> get() {
return responseMap;
}

/**
*
* Get the field capabilities for the provided {@code field}
*/
public IndexFieldCapabilities getField(String field) {
return responseMap.get(field);
}

TransportVersion getOriginVersion() {
return originVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static IndexFieldCapabilities readFrom(StreamInput in) throws IOException
isAggregatable,
isDimension,
metricType,
in.readMap(StreamInput::readString)
in.readImmutableMap(StreamInput::readString)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
Expand Down Expand Up @@ -1108,7 +1107,7 @@ public static Parameter<Map<String, String>> metaParam() {
return new Parameter<>(
"meta",
true,
Collections::emptyMap,
Map::of,
(n, c, o) -> TypeParsers.parseMeta(n, o),
m -> m.fieldType().meta(),
XContentBuilder::stringStringMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public MappedFieldType(
this.isStored = isStored;
this.docValues = hasDocValues;
this.textSearchInfo = Objects.requireNonNull(textSearchInfo);
this.meta = Objects.requireNonNull(meta);
// meta should be sorted but for the one item or empty case we can fall back to immutable maps to save some memory since order is
// irrelevant
this.meta = meta.size() <= 1 ? Map.copyOf(meta) : meta;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public static Map<String, String> parseMeta(String name, Object metaObject) {
}
@SuppressWarnings("unchecked")
Map<String, ?> meta = (Map<String, ?>) metaObject;
if (meta.isEmpty()) {
return Map.of();
}
if (meta.size() > 5) {
throw new MapperParsingException("[meta] can't have more than 5 entries, but got " + meta.size() + " on field [" + name + "]");
}
Expand Down Expand Up @@ -69,6 +72,12 @@ public static Map<String, String> parseMeta(String name, Object metaObject) {
);
}
}
var entrySet = meta.entrySet();
if (entrySet.size() == 1) {
// no need to sort for a single entry
var entry = entrySet.iterator().next();
return Map.of(entry.getKey(), (String) entry.getValue());
}
Map<String, String> sortedMeta = new TreeMap<>();
for (Map.Entry<String, ?> entry : meta.entrySet()) {
sortedMeta.put(entry.getKey(), (String) entry.getValue());
Expand Down

0 comments on commit fd65b61

Please sign in to comment.