Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -203,18 +203,22 @@ public void close() {
}

private void closeMap() {
for (final Iterator<Map.Entry<String, String>> it =
originalValues.entrySet().iterator();
it.hasNext(); ) {
final Map.Entry<String, String> entry = it.next();
final Map<String, String> valuesToReplace = new HashMap<>(originalValues.size());
final List<String> keysToRemove = new ArrayList<>(originalValues.size());
for (final Map.Entry<String, String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.3.xsd"
type="changed">
<issue id="2296" link="https://github.com/apache/logging-log4j2/pull/2296"/>
<description format="asciidoc">Improve performance of `CloseableThreadContext#closeMap()`</description>
</entry>