Skip to content

Avoid a slow exception catch in JdkMapAdapterStringMap constructor #2238

Closed

Description

@ppkarwasz

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>

Activity

  1. ppkarwasz commented on Jan 24, 2024

    @ppkarwasz
    MemberAuthor

    @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: ReadOnlyThreadContextMap can return a frozen StringMap without the need to convert it to and from a Java Map.

  2. jdgenerix commented on Jan 24, 2024

    @jdgenerix

    @ppkarwasz I'm sorry, i don't really see the optimzation you saw with ReadOnlyThreadContextMap.

  3. ppkarwasz commented on Jan 24, 2024

    @ppkarwasz
    MemberAuthor

    @jdgenerix,

    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:

                final ReadOnlyThreadContextMap map = ThreadContext.getThreadContextMap();
                if (map instanceof ReadOnlyStringMap) {
                    return (ReadOnlyStringMap) map;
                }

    is also wrong (a ReadOnlyThreadContextMap is a factory of ReadOnlyStringMap not a ReadOnlyStringMap itself) and should be:

                final ReadOnlyThreadContextMap map = ThreadContext.getThreadContextMap();
                if (map != null) {
                    return map.getReadOnlyContextData();
                }
  4. self-assigned this
    on Jan 29, 2024
  5. added this to the 2.23.0 milestone on Jan 29, 2024
  6. ppkarwasz commented on Feb 6, 2024

    @ppkarwasz
    MemberAuthor

    @jdgenerix,

    Can you check if the latest snapshot in the Maven repo https://repository.apache.org/snapshots solves your problem?

  7. jdgenerix commented on Feb 6, 2024

    @jdgenerix

    @ppkarwasz Thank you.
    I did a test and it looks fine to me.

  8. added a commit that references this issue on Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions