@@ -335,7 +335,7 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
335335 boolean allEnabled = this .allEnabled ;
336336 Map <String , ObjectMapper > fullPathObjectMappers = this .fullPathObjectMappers ;
337337 FieldTypeLookup fieldTypes = this .fieldTypes ;
338- Set <String > parentTypes = new HashSet <>( this .parentTypes ) ;
338+ Set <String > parentTypes = this .parentTypes ;
339339 Map <String , DocumentMapper > mappers = new HashMap <>(this .mappers );
340340
341341 Map <String , DocumentMapper > results = new LinkedHashMap <>(documentMappers .size () + 1 );
@@ -394,6 +394,7 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
394394
395395 for (ObjectMapper objectMapper : objectMappers ) {
396396 if (fullPathObjectMappers == this .fullPathObjectMappers ) {
397+ // first time through the loops
397398 fullPathObjectMappers = new HashMap <>(this .fullPathObjectMappers );
398399 }
399400 fullPathObjectMappers .put (objectMapper .fullPath (), objectMapper );
@@ -413,6 +414,10 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
413414 }
414415
415416 if (oldMapper == null && newMapper .parentFieldMapper ().active ()) {
417+ if (parentTypes == this .parentTypes ) {
418+ // first time through the loop
419+ parentTypes = new HashSet <>(this .parentTypes );
420+ }
416421 parentTypes .add (mapper .parentFieldMapper ().type ());
417422 }
418423
@@ -453,8 +458,15 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
453458 // make structures immutable
454459 mappers = Collections .unmodifiableMap (mappers );
455460 results = Collections .unmodifiableMap (results );
456- parentTypes = Collections .unmodifiableSet (parentTypes );
457- fullPathObjectMappers = Collections .unmodifiableMap (fullPathObjectMappers );
461+
462+ // only need to immutably rewrap these if the previous reference was changed.
463+ // if not then they are already implicitly immutable.
464+ if (fullPathObjectMappers != this .fullPathObjectMappers ) {
465+ fullPathObjectMappers = Collections .unmodifiableMap (fullPathObjectMappers );
466+ }
467+ if (parentTypes != this .parentTypes ) {
468+ parentTypes = Collections .unmodifiableSet (parentTypes );
469+ }
458470
459471 // commit the change
460472 if (defaultMappingSource != null ) {
0 commit comments