diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/CloseableThreadContext.java b/log4j-api/src/main/java/org/apache/logging/log4j/CloseableThreadContext.java index 1ec849f0012..c585edf5460 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/CloseableThreadContext.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/CloseableThreadContext.java @@ -16,8 +16,8 @@ */ package org.apache.logging.log4j; +import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -203,18 +203,22 @@ public void close() { } private void closeMap() { - for (final Iterator> it = - originalValues.entrySet().iterator(); - it.hasNext(); ) { - final Map.Entry entry = it.next(); + final Map valuesToReplace = new HashMap<>(originalValues.size()); + final List keysToRemove = new ArrayList<>(originalValues.size()); + for (final Map.Entry entry : originalValues.entrySet()) { final String key = entry.getKey(); final String originalValue = entry.getValue(); if (null == originalValue) { - ThreadContext.remove(key); + keysToRemove.add(key); } else { - ThreadContext.put(key, originalValue); + valuesToReplace.put(key, originalValue); } - it.remove(); + } + if (!valuesToReplace.isEmpty()) { + ThreadContext.putAll(valuesToReplace); + } + if (!keysToRemove.isEmpty()) { + ThreadContext.removeAll(keysToRemove); } } diff --git a/src/changelog/.2.x.x/improve_CloseableThreadContext_closeMap.xml b/src/changelog/.2.x.x/improve_CloseableThreadContext_closeMap.xml new file mode 100644 index 00000000000..958fe5a59d6 --- /dev/null +++ b/src/changelog/.2.x.x/improve_CloseableThreadContext_closeMap.xml @@ -0,0 +1,8 @@ + + + + Improve performance of `CloseableThreadContext#closeMap()` + \ No newline at end of file