From a4d232521b250e6df883d45dda8629859d38f858 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Sat, 4 Jan 2025 14:41:22 +0100 Subject: [PATCH] Fix inefficient org.elasticsearch.index.mapper.Mapper#getTotalFieldsCount implementations 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. --- .../java/org/elasticsearch/index/mapper/FieldMapper.java | 7 +++++-- .../java/org/elasticsearch/index/mapper/ObjectMapper.java | 6 +++++- .../org/elasticsearch/index/mapper/RootObjectMapper.java | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index ffd60efc772f9..5bbecbf117dba 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -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; @@ -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 indexAnalyzers() { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java index 6a107dbaa9e63..f4084b3ede24f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java @@ -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 { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java index ce983e8a327c9..2fe82b4eacfc5 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java @@ -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(); } }