Skip to content

Commit

Permalink
Improve performance of CloseableThreadContext#closeMap() (#2292, #2296
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jengebr authored Feb 19, 2024
1 parent 8edb133 commit b4ef5dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
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>

0 comments on commit b4ef5dd

Please sign in to comment.