-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Feature Request
Is your feature request related to a problem? Please describe
The current implementation of getNodeDescription()
method creates an unnecessary intermediate collection by using String.join()
with Collectors.toList()
. This can lead to decreased performance and increased memory usage, especially in high-throughput scenarios where this method is frequently called.
Describe the solution you'd like
Replace the current implementation:
https://github.com/redis/lettuce/blob/main/src/main/java/io/lettuce/core/cluster/NodeSelectionInvocationHandler.java
private String getNodeDescription(List<RedisClusterNode> notFinished) {
return String.join(", ", notFinished.stream().map(this::getDescriptor).collect(Collectors.toList()));
}
With a more efficient approach using Collectors.joining()
:
private String getNodeDescription(List<RedisClusterNode> notFinished) {
return notFinished.stream().map(this::getDescriptor).collect(Collectors.joining(", "));
}
This optimization eliminates the temporary list creation while maintaining identical functionality, resulting in better performance and reduced memory overhead.
Describe alternatives you've considered
- Keep the current implementation, as the performance impact might be minimal in some use cases
- Use a StringBuilder directly, but this would be more verbose and less idiomatic with the Stream API
- Create a custom collector, but this would be overly complex for this simple case
Teachability, Documentation, Adoption, Migration Strategy
This change is a straightforward optimization that maintains the same behavior and API. No documentation updates are needed as this is an internal implementation detail.
For developers working on the codebase, this change serves as a good example of using Stream collectors efficiently. Similar patterns could be applied to other string-joining operations throughout the codebase.