Skip to content

Commit ba4b1b2

Browse files
Fix inefficient org.elasticsearch.index.mapper.Mapper#getTotalFieldsCount implementations (elastic#119555) (elastic#119557)
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.
1 parent 320fc75 commit ba4b1b2

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import java.util.function.Consumer;
5858
import java.util.function.Function;
5959
import java.util.function.Supplier;
60-
import java.util.stream.Stream;
6160

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

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

445444
@Override
446445
public int getTotalFieldsCount() {
447-
return 1 + Stream.of(builderParams.multiFields.mappers).mapToInt(FieldMapper::getTotalFieldsCount).sum();
446+
int sum = 1;
447+
for (FieldMapper mapper : builderParams.multiFields.mappers) {
448+
sum += mapper.getTotalFieldsCount();
449+
}
450+
return sum;
448451
}
449452

450453
public Map<String, NamedAnalyzer> indexAnalyzers() {

server/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ public ObjectMapper build(MapperBuilderContext context) {
263263

264264
@Override
265265
public int getTotalFieldsCount() {
266-
return 1 + mappers.values().stream().mapToInt(Mapper::getTotalFieldsCount).sum();
266+
int sum = 1;
267+
for (Mapper mapper : mappers.values()) {
268+
sum += mapper.getTotalFieldsCount();
269+
}
270+
return sum;
267271
}
268272

269273
public static class TypeParser implements Mapper.TypeParser {

server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,6 @@ private static boolean processField(
538538

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

0 commit comments

Comments
 (0)