Skip to content

Commit

Permalink
Fix inefficient org.elasticsearch.index.mapper.Mapper#getTotalFieldsC…
Browse files Browse the repository at this point in the history
…ount implementations (elastic#119555)

This is eating up a lot of CPU when creating indices and visibly slowing
down many-shards benchmarks. Looking the profiling the cost of these
implementations is almost exclusively the cost of the stream abstraction
overhead (because we needlessly create the stream for every field mapper).
Keeping it simple and using iterators almost completely removes the cost
of this thing from profiling.
  • Loading branch information
original-brownbear authored Jan 4, 2025
1 parent cd40f99 commit 7d95699
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static org.elasticsearch.core.Strings.format;

Expand Down Expand Up @@ -444,7 +443,11 @@ protected void doXContentBody(XContentBuilder builder, Params params) throws IOE

@Override
public int getTotalFieldsCount() {
return 1 + Stream.of(builderParams.multiFields.mappers).mapToInt(FieldMapper::getTotalFieldsCount).sum();
int sum = 1;
for (FieldMapper mapper : builderParams.multiFields.mappers) {
sum += mapper.getTotalFieldsCount();
}
return sum;
}

public Map<String, NamedAnalyzer> indexAnalyzers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ public ObjectMapper build(MapperBuilderContext context) {

@Override
public int getTotalFieldsCount() {
return 1 + mappers.values().stream().mapToInt(Mapper::getTotalFieldsCount).sum();
int sum = 1;
for (Mapper mapper : mappers.values()) {
sum += mapper.getTotalFieldsCount();
}
return sum;
}

public static class TypeParser implements Mapper.TypeParser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,6 @@ private static boolean processField(

@Override
public int getTotalFieldsCount() {
return mappers.values().stream().mapToInt(Mapper::getTotalFieldsCount).sum() + runtimeFields.size();
return super.getTotalFieldsCount() - 1 + runtimeFields.size();
}
}

0 comments on commit 7d95699

Please sign in to comment.