Skip to content

Performance Optimization: Replace String.join with Collectors.joining in NodeSelectionInvocationHandler.getNodeDescription #3261

@ori0o0p

Description

@ori0o0p

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

  1. Keep the current implementation, as the performance impact might be minimal in some use cases
  2. Use a StringBuilder directly, but this would be more verbose and less idiomatic with the Stream API
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: improvementAn improvement to the existing implementation

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions