ppkarwasz opened a new issue, #2238:
URL: https://github.com/apache/logging-log4j2/issues/2238

   ### Discussed in https://github.com/apache/logging-log4j2/discussions/2235
   
   <div type='discussions-op-text'>
   
   <sup>Originally posted by **jdgenerix** January 24, 2024</sup>
   The `org.apache.logging.log4j.core.impl.JdkMapAdapterStringMap` constructor 
is called from `org.apache.logging.log4j.core.impl.ThreadContextDataInjector` 
with an UnmodiableMap (`copy`) and the constructor is trying to modify and 
catch an exception to detect if this is an immutable Map.
   This code is triggered a lot of time and could be more efficient.
   Adding a package protected constructor which take the immutable boolean flag 
as an argument and using it from `ThreadContextDataInjector` would avoid this 
slow try catch.
   
   ```java
           @Override
           public ReadOnlyStringMap rawContextData() {
               final ReadOnlyThreadContextMap map = 
ThreadContext.getThreadContextMap();
               if (map instanceof ReadOnlyStringMap) {
                   return (ReadOnlyStringMap) map;
               }
               // note: default ThreadContextMap is null
               final Map<String, String> copy = 
ThreadContext.getImmutableContext();
               return copy.isEmpty() ? 
ContextDataFactory.emptyFrozenContextData() : new JdkMapAdapterStringMap(copy);
           }
   
   
           public JdkMapAdapterStringMap(final Map<String, String> map) {
                this.map = Objects.requireNonNull(map, "map");
                try {
                        map.replace(Strings.EMPTY, Strings.EMPTY, 
Strings.EMPTY);
                } catch (final UnsupportedOperationException ignored) {
                        immutable = true;
                }
        }
   ```</div>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to