Skip to content

Commit d3bab57

Browse files
Reduce footprint of DynamicTemplate
A lot of these lists are empty most of the time, we can save memory here by moving to immutable lists. Found in a heap dump where this saves about 10M of heap.
1 parent 1a8238c commit d3bab57

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

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

+10-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
20+
import java.util.Collection;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Locale;
@@ -351,15 +352,9 @@ && matchPatternsAreDefined(match, pathMatch, unmatchMappingType))
351352
.toArray(XContentFieldType[]::new);
352353

353354
final MatchType matchType = MatchType.fromString(matchPattern);
354-
List<String> allPatterns = Stream.of(match.stream(), unmatch.stream(), pathMatch.stream(), pathUnmatch.stream())
355-
.flatMap(s -> s)
356-
.toList();
357-
for (String pattern : allPatterns) {
358-
// no need to check return value - the method impls either have side effects (set header warnings)
359-
// or throw an exception that should be sent back to the user
360-
matchType.validate(pattern, name);
361-
}
362-
355+
// no need to check return value - the method impls either have side effects (set header warnings)
356+
// or throw an exception that should be sent back to the user
357+
Stream.of(match, unmatch, pathMatch, pathUnmatch).flatMap(Collection::stream).forEach(pattern -> matchType.validate(pattern, name));
363358
return new DynamicTemplate(
364359
name,
365360
pathMatch,
@@ -427,13 +422,13 @@ private DynamicTemplate(
427422
boolean runtimeMapping
428423
) {
429424
this.name = name;
430-
this.pathMatch = pathMatch;
431-
this.pathUnmatch = pathUnmatch;
432-
this.match = match;
433-
this.unmatch = unmatch;
425+
this.pathMatch = List.copyOf(pathMatch);
426+
this.pathUnmatch = List.copyOf(pathUnmatch);
427+
this.match = List.copyOf(match);
428+
this.unmatch = List.copyOf(unmatch);
434429
this.matchType = matchType;
435-
this.matchMappingType = matchMappingType;
436-
this.unmatchMappingType = unmatchMappingType;
430+
this.matchMappingType = List.copyOf(matchMappingType);
431+
this.unmatchMappingType = List.copyOf(unmatchMappingType);
437432
this.xContentFieldTypes = xContentFieldTypes;
438433
this.mapping = mapping;
439434
this.runtimeMapping = runtimeMapping;

0 commit comments

Comments
 (0)