@@ -115,7 +115,7 @@ public enum MergeReason {
115115 private volatile Map <String , DocumentMapper > mappers = emptyMap ();
116116
117117 private volatile FieldTypeLookup fieldTypes ;
118- private volatile Map <String , ObjectMapper > fullPathObjectMappers = new HashMap <> ();
118+ private volatile Map <String , ObjectMapper > fullPathObjectMappers = emptyMap ();
119119 private boolean hasNested = false ; // updated dynamically to true when a nested object is added
120120 private boolean allEnabled = false ; // updated dynamically to true when _all is enabled
121121
@@ -399,6 +399,7 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
399399
400400 for (ObjectMapper objectMapper : objectMappers ) {
401401 if (fullPathObjectMappers == this .fullPathObjectMappers ) {
402+ // first time through the loops
402403 fullPathObjectMappers = new HashMap <>(this .fullPathObjectMappers );
403404 }
404405 fullPathObjectMappers .put (objectMapper .fullPath (), objectMapper );
@@ -419,6 +420,7 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
419420
420421 if (oldMapper == null && newMapper .parentFieldMapper ().active ()) {
421422 if (parentTypes == this .parentTypes ) {
423+ // first time through the loop
422424 parentTypes = new HashSet <>(this .parentTypes );
423425 }
424426 parentTypes .add (mapper .parentFieldMapper ().type ());
@@ -461,8 +463,15 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
461463 // make structures immutable
462464 mappers = Collections .unmodifiableMap (mappers );
463465 results = Collections .unmodifiableMap (results );
464- parentTypes = Collections .unmodifiableSet (parentTypes );
465- fullPathObjectMappers = Collections .unmodifiableMap (fullPathObjectMappers );
466+
467+ // only need to immutably rewrap these if the previous reference was changed.
468+ // if not then they are already implicitly immutable.
469+ if (fullPathObjectMappers != this .fullPathObjectMappers ) {
470+ fullPathObjectMappers = Collections .unmodifiableMap (fullPathObjectMappers );
471+ }
472+ if (parentTypes != this .parentTypes ) {
473+ parentTypes = Collections .unmodifiableSet (parentTypes );
474+ }
466475
467476 // commit the change
468477 if (defaultMappingSource != null ) {
0 commit comments