Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimization in CloseableThreadContext.closeMap() (#2292) #2296

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Changes from 1 commit
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();
Map<String, String> valuesToReplace = new HashMap<>(originalValues.size());
List<String> keysToRemove = new ArrayList<>(originalValues.size());
for (Map.Entry<String, String> entry : originalValues.entrySet()) {
vy marked this conversation as resolved.
Show resolved Hide resolved
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