From 0a5e7ec1356cf679c6b7afc7aef63bb8790075f3 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 8 Feb 2026 15:09:41 -0800 Subject: [PATCH] Polish "Optimize ContextPairs nested name splitting" See gh-48999 --- .../boot/logging/structured/ContextPairs.java | 52 +++++++------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ContextPairs.java b/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ContextPairs.java index 69fd6628e37..d5aebb1c10f 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ContextPairs.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/logging/structured/ContextPairs.java @@ -193,47 +193,35 @@ public class ContextPairs { LinkedHashMap result = new LinkedHashMap<>(); this.addedPairs.forEach((addedPair) -> { addedPair.accept(item, joining((name, value) -> { + StringBuilder part = new StringBuilder(name.length()); + int length = (!name.endsWith(".")) ? name.length() : name.length() - 1; Map destination = result; - - int end = trimTrailingDelimiters(name); - if (end == 0) { - return; - } - - int start = 0; - while (true) { - int dot = name.indexOf('.', start); - if (dot == -1 || dot >= end) { - break; + for (int i = 0; i < length; i++) { + char ch = name.charAt(i); + if (i == length - 1) { + part.append(ch); + Object previous = destination.put(part.toString(), value); + assertNotDuplicateNestedPairs(previous == null, name, length); } - - String part = name.substring(start, dot); - - Object existing = destination.computeIfAbsent(part, (key) -> new LinkedHashMap<>()); - if (!(existing instanceof Map)) { - String common = name.substring(0, dot); - throw new IllegalStateException( - "Duplicate nested pairs added under '%s'".formatted(common)); + else if (ch == '.') { + Object current = destination.computeIfAbsent(part.toString(), + (key) -> new LinkedHashMap<>()); + assertNotDuplicateNestedPairs(current instanceof Map, name, i); + destination = (Map) current; + part.setLength(0); + } + else { + part.append(ch); } - - destination = (Map) existing; - start = dot + 1; } - - String leaf = name.substring(start, end); - Object previous = destination.put(leaf, value); - Assert.state(previous == null, () -> "Duplicate nested pairs added under '%s'".formatted(name)); })); }); result.forEach(pairs); } - private int trimTrailingDelimiters(String name) { - int end = name.length(); - while (end > 0 && name.charAt(end - 1) == '.') { - end--; - } - return end; + private void assertNotDuplicateNestedPairs(boolean expression, String name, int index) { + Assert.state(expression, + () -> "Duplicate nested pairs added under '%s'".formatted(name.substring(0, index))); } private BiConsumer joining(BiConsumer pairs) {