Skip to content

Conversation

Copy link

Copilot AI commented Nov 2, 2025

Sorting data that will be largely discarded by a subsequent distinct operation wastes cycles. Applying distinct first reduces the element count before the expensive sort.

Changes

StreamRules.java

  • StreamDistinctSorted: stream.sorted().distinct()stream.distinct().sorted()
  • StreamDistinctSortedWithComparator: stream.sorted(comparator).distinct()stream.distinct().sorted(comparator)

ReactorRules.java

  • FluxDistinctSort: flux.sort().distinct()flux.distinct().sort()
  • FluxDistinctSortWithComparator: flux.sort(comparator).distinct()flux.distinct().sort(comparator)

Test coverage

Added test cases in StreamRulesTestInput/Output.java and ReactorRulesTestInput/Output.java

Example

// Before
Stream.of(1, 4, 3, 2, 4, 1)
    .sorted()      // Sorts 6 elements
    .distinct();   // Returns 4 elements

// After
Stream.of(1, 4, 3, 2, 4, 1)
    .distinct()    // Returns 4 elements
    .sorted();     // Sorts 4 elements

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkstyle.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.23.3/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.23.3/x64/codeql/xml/tools/xml-extractor.jar --fileList=/home/REDACTED/work/error-prone-support/.codeql-scratch/dbs/java/working/files-to-index3342103831160680863.list --sourceArchiveDir=/home/REDACTED/work/error-prone-support/.codeql-scratch/dbs/java/src --outputDir=/home/REDACTED/work/error-prone-support/.codeql-scratch/dbs/java/trap/java (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Introduce sorting and distinct operation improvements</issue_title>
<issue_description>### Problem

Performing an expensive sort operation on data that will largely be discarded by the subsequent distinct operation should be avoided.

Description of the proposed new feature

  • Support a stylistic preference.
  • Avoid a common gotcha, or potential problem.
  • Improve performance.

I would like to rewrite the following code:

stream.sorted().distinct()
// Rewrite to:
stream.distinct().sorted()
flux.sort().distinct()
// Rewrite to:
flux.distinct().sort()

Considerations

These rewrite rules can be implemented using Refaster by adding new rules to StreamRules and ReactorRules respectively.

Consider the overloads of the sort, sorted, and distinct APIs.

Participation

  • I am willing to submit a pull request to implement this improvement.</issue_description>

<agent_instructions>- Introduce the requested Refaster rules in StreamRules.java and ReactorRules.java.

  • Make sure to add test cases in the associated *TestInput.java and *TestOutput.java files.
  • Carefully follow the existing programming style, with a focus on Javadoc style, use of generics, and test format and test data.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mohamedsamehsalah Will assign to @Carlos-CBS. @mohamedsamehsalah Closed; Will assign internally. @mohamedsamehsalah Assigned to @ali-khaled-ali !

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Introduce improvements for sorting and distinct operations Add Refaster rules to apply distinct before sort operations Nov 2, 2025
Copilot AI requested a review from Stephan202 November 2, 2025 20:01
Copilot finished work on behalf of Stephan202 November 2, 2025 20:01
@Stephan202 Stephan202 added this to the 0.27.0 milestone Nov 2, 2025
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one commit with some tweaks. Thanks @copilot!

Suggested commit message:

Introduce `{Flux,Stream}DistinctSort{,ed}{,WithComparator}` Refaster rules (#1950)

While there, simplify some unrelated test cases.

Resolves #1828.

@Stephan202 Stephan202 marked this pull request as ready for review November 2, 2025 20:17
@Stephan202 Stephan202 changed the title Add Refaster rules to apply distinct before sort operations Introduce {Flux,String}DistinctSort{,ed}{,WithComparator} Refaster rules Nov 2, 2025
@Stephan202 Stephan202 changed the title Introduce {Flux,String}DistinctSort{,ed}{,WithComparator} Refaster rules Introduce {Flux,Stream}DistinctSort{,ed}{,WithComparator} Refaster rules Nov 2, 2025
@github-actions
Copy link

github-actions bot commented Nov 2, 2025

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202
Copy link
Member

To be merged after #1942.

Copy link
Contributor

@mohamedsamehsalah mohamedsamehsalah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Stephan202 and @copilot! 🚀

}

Flux<Integer> testFluxFilterSort() {
return Flux.just(1, 4, 3, 2).sort().filter(i -> i % 2 == 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 : I'd say "A for for effort" 👏

Copilot AI and others added 2 commits November 11, 2025 22:19
- Add StreamDistinctSorted rule to rewrite stream.sorted().distinct() → stream.distinct().sorted()
- Add StreamDistinctSortedWithComparator rule for comparator overload
- Add FluxDistinctSort rule to rewrite flux.sort().distinct() → flux.distinct().sort()
- Add FluxDistinctSortWithComparator rule for comparator overload
- Add test cases for all new rules in TestInput and TestOutput files

Co-authored-by: Stephan202 <[email protected]>
@Stephan202 Stephan202 force-pushed the copilot/improve-sorting-performance branch from 63f6131 to 23c7e50 Compare November 11, 2025 21:19
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Introduce sorting and distinct operation improvements

3 participants