From 197e4cc1129edf8d176b55fcef9f4a14c5513399 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 29 Nov 2023 09:21:20 +0100 Subject: [PATCH] Polishing. Use for-loop instead of Java Stream API for concatenation of paths to reduce GC and CPU pressure. See #2992 --- .../DefaultPersistentPropertyPath.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java b/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java index 11ccbb485c..bdd2d9364d 100644 --- a/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java +++ b/src/main/java/org/springframework/data/mapping/context/DefaultPersistentPropertyPath.java @@ -19,7 +19,6 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.stream.Collectors; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mapping.PersistentProperty; @@ -28,7 +27,6 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; -import org.springframework.util.StringUtils; /** * Abstraction of a path of {@link PersistentProperty}s. @@ -111,10 +109,27 @@ public String toPath(String delimiter, Converter converter) { Assert.hasText(delimiter, "Delimiter must not be null or empty"); Assert.notNull(converter, "Converter must not be null"); - return properties.stream() // - .map(converter::convert) // - .filter(StringUtils::hasText) // - .collect(Collectors.joining(delimiter)); + StringBuilder builder = null; + for (P property : properties) { + + String converted = converter.convert(property); + + if (ObjectUtils.isEmpty(converted)) { + continue; + } + + if (builder == null) { + builder = new StringBuilder(); + } + + if (!builder.isEmpty()) { + builder.append(delimiter); + } + + builder.append(converted); + } + + return builder == null ? "" : builder.toString(); } @Override