-
- Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Milestone
Description
Discussed in #2235
Originally posted by jdgenerix January 24, 2024
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.
@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>