Skip to content

Conversation

@kilink
Copy link
Contributor

@kilink kilink commented Nov 11, 2025

When Comparator.naturalOrder() was explicitly supplied to a collection such as TreeSet, or passed into the sorted method of a stream, the sorted characteristic was not preserved, causing unnecessary buffering and duplicate sorting.

Example:

TreeSet<Integer> sortedSet = new TreeSet<>(Comparator.naturalOrder());
sortedSet.add(1);
sortedSet.add(2);
// SortedOps.OfRef.opWrapSink is not a no-op
sortedSet.stream().sorted().forEach(System.out::println);

or

TreeSet<Integer> sortedSet = new TreeSet<>();
sortedSet.add(1);
sortedSet.add(2);
// SortedOps.OfRef.opWrapSink is not a no-op
sortedSet.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println);

This PR updates SortedOps.makeRef and StreamOpFlag.fromCharacteristics to handle the above cases and avoid the unnecessary sort step.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8362958: Unnecessary copying / sorting in Streams using Comparator.naturalOrder() (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28226/head:pull/28226
$ git checkout pull/28226

Update a local copy of the PR:
$ git checkout pull/28226
$ git pull https://git.openjdk.org/jdk.git pull/28226/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28226

View PR using the GUI difftool:
$ git pr show -t 28226

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28226.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 11, 2025

👋 Welcome back kilink! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Nov 11, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Nov 11, 2025
@openjdk
Copy link

openjdk bot commented Nov 11, 2025

@kilink The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 11, 2025
@kilink kilink force-pushed the fix-stream-sort-natural-order-comparator branch from 634bd4d to c4fb5d0 Compare November 11, 2025 02:46
@openjdk
Copy link

openjdk bot commented Nov 11, 2025

@kilink Please do not rebase or force-push to an active PR as it invalidates existing review comments. Note for future reference, the bots always squash all changes into a single commit automatically as part of the integration. See OpenJDK Developers’ Guide for more information.

@kilink kilink changed the title 8362958: Optimize Stream sorting with Comparator.naturalOrder() 8362958: Unnecessary copying / sorting in Streams using Comparator.naturalOrder() Nov 11, 2025
@mlbridge
Copy link

mlbridge bot commented Nov 11, 2025

Webrevs

Simplify comparison

Co-authored-by: Viktor Klang <viktor.klang@oracle.com>
@forax
Copy link
Member

forax commented Nov 11, 2025

I wonder if it's not better to replace Comparator.naturalOrder() by null in the constructor of TreeSet, given that TreeSet does not provide a getter for it so the only way to get the comparator is using treeSet.spliterator().getComparator().

@kilink
Copy link
Contributor Author

kilink commented Nov 11, 2025

I wonder if it's not better to replace Comparator.naturalOrder() by null in the constructor of TreeSet, given that TreeSet does not provide a getter for it so the only way to get the comparator is using treeSet.spliterator().getComparator().

That wouldn't help with the stream().sorted(Comparator.naturalOrder()) case; the example I supplied is somewhat contrived, I originally ran into this in Guava (see the issue I opened there); the library is null-hostile and so always supplies a comparator to its sorted collections. Note that this won't directly fix the issue there until they move away from their custom natural order comparator to the one from the JDK.

@forax
Copy link
Member

forax commented Nov 11, 2025

That wouldn't help with the stream().sorted(Comparator.naturalOrder()) case; the example I supplied is somewhat contrived, I originally ran into this in Guava (see the issue I opened there); the library is null-hostile and so always supplies a comparator to its sorted collections.

If you switch from Comparator.naturalOrder()) to null, you have to do it in stream.sorted() too (and also List.sort(Comparator), Collections.sort(Comparator), Array.sort(Comparator), Collections.reverseOrder(Comparator) etc)

Note that this won't directly fix the issue there until they move away from their custom natural order comparator to the one from the JDK.

yes, comparing comparators with == is brittle anyway, but at least you can make it consistent for the JDK.

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

Labels

core-libs core-libs-dev@openjdk.org rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

4 participants