Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Use for-loop instead of Java Stream API for concatenation of paths to reduce GC and CPU pressure.

See #2992
  • Loading branch information
mp911de committed Nov 29, 2023
1 parent ee452d5 commit 197e4cc
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -111,10 +109,27 @@ public String toPath(String delimiter, Converter<? super P, String> 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
Expand Down

0 comments on commit 197e4cc

Please sign in to comment.