You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2024/02/19 17:30:10 UTC
(logging-log4j2) branch 2.x updated: Improve performance of `CloseableThreadContext#closeMap()` (#2292, #2296)
This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new b4ef5dd122 Improve performance of `CloseableThreadContext#closeMap()` (#2292, #2296)
b4ef5dd122 is described below
commit b4ef5dd122f3c003be78973749377a85519db0cb
Author: jengebr <73...@users.noreply.github.com>
AuthorDate: Mon Feb 19 11:30:04 2024 -0600
Improve performance of `CloseableThreadContext#closeMap()` (#2292, #2296)
---
.../apache/logging/log4j/CloseableThreadContext.java | 20 ++++++++++++--------
.../improve_CloseableThreadContext_closeMap.xml | 8 ++++++++
2 files changed, 20 insertions(+), 8 deletions(-)
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 1ec849f001..c585edf546 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 class CloseableThreadContext {
}
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);
}
}
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 0000000000..958fe5a59d
--- /dev/null
+++ b/src/changelog/.2.x.x/improve_CloseableThreadContext_closeMap.xml
@@ -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>
\ No newline at end of file