You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid a slow exception catch in JdkMapAdapterStringMap constructor #2238
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.
Thank you for this report. Can you supply a PR to correct this?
Looking at the code you are pointing at, there is another optimization possible: ReadOnlyThreadContextMap can return a frozen StringMap without the need to convert it to and from a Java Map.
After looking better at the code the native storage of DefaultThreadContextMap is JDK's Map, so the only optimization possible is to create an additional constructor to JdkMapAdapterStringMap that takes an additional boolean frozen parameter.
The:
finalReadOnlyThreadContextMapmap = ThreadContext.getThreadContextMap();
if (mapinstanceofReadOnlyStringMap) {
return (ReadOnlyStringMap) map;
}
is also wrong (a ReadOnlyThreadContextMap is a factory of ReadOnlyStringMap not a ReadOnlyStringMap itself) and should be:
finalReadOnlyThreadContextMapmap = ThreadContext.getThreadContextMap();
if (map != null) {
returnmap.getReadOnlyContextData();
}
Activity
ppkarwasz commentedon Jan 24, 2024
@jdgenerix,
Thank you for this report. Can you supply a PR to correct this?
Looking at the code you are pointing at, there is another optimization possible:
ReadOnlyThreadContextMapcan return a frozenStringMapwithout the need to convert it to and from a JavaMap.jdgenerix commentedon Jan 24, 2024
@ppkarwasz I'm sorry, i don't really see the optimzation you saw with
ReadOnlyThreadContextMap.ppkarwasz commentedon Jan 24, 2024
@jdgenerix,
After looking better at the code the native storage of
DefaultThreadContextMapis JDK'sMap, so the only optimization possible is to create an additional constructor toJdkMapAdapterStringMapthat takes an additional booleanfrozenparameter.The:
is also wrong (a
ReadOnlyThreadContextMapis a factory ofReadOnlyStringMapnot aReadOnlyStringMapitself) and should be:ppkarwasz commentedon Feb 6, 2024
@jdgenerix,
Can you check if the latest snapshot in the Maven repo
https://repository.apache.org/snapshotssolves your problem?jdgenerix commentedon Feb 6, 2024
@ppkarwasz Thank you.
I did a test and it looks fine to me.
[SPARK-49541][BUILD] Upgrade log4j2 to 2.24.1
[SPARK-49541][BUILD] Upgrade log4j2 to 2.24.1
[SPARK-49541][BUILD] Upgrade log4j2 to 2.24.1
[SPARK-49541][BUILD] Upgrade log4j2 to 2.24.1
[SPARK-49541][BUILD] Upgrade log4j2 to 2.24.1