Skip to content

8372946 - TreeMap sub-map entry spliterator is expensive#28608

Open
olivergillespie wants to merge 2 commits intoopenjdk:masterfrom
olivergillespie:8372946
Open

8372946 - TreeMap sub-map entry spliterator is expensive#28608
olivergillespie wants to merge 2 commits intoopenjdk:masterfrom
olivergillespie:8372946

Conversation

@olivergillespie
Copy link
Contributor

@olivergillespie olivergillespie commented Dec 2, 2025

TreeMap sub-maps use the default IteratorSpliterator implementation for TreeMap$EntrySetView which is slow for some operations, because EntrySetView.size() iterates all elements. This is most trivially shown by something like largeTreeMap.tailMap(0L, false).entrySet().limit(1).count() taking a long time. This showed up in my application, where it was trivial to mitigate by switching to a for loop, but I think the fix is easy enough.

keySet() does not have the same problem, as it provides a custom Spliterator implementation which is not Spliterator.SIZED, and returns Long.MAX_VALUE for estimateSize() (which is the recommended approach when the size is expensive to compute). I'm assuming this optimization was simply missed for the EntryIterator in the original implementation, but I don't know for sure.

This patch fixes the issue by providing a custom spliterator for EntrySetView, which is not SIZED. The implementation is copied almost exactly from the equivalent KeyIterator classes in this file (SubMapKeyIterator, DescendingSubMapKeyIterator). The only difference is in SubMapEntryIterator.getComparator, for which I copied the implementation from TreeMap$EntrySpliterator.

Basic performance test: map.tailMap(0L, false).entrySet().stream().limit(1).count() for a TreeMap with 10_000_000 entries.

Before (keySet is fast using SubMapKeyIterator, entrySet is slow using IteratorSpliterator):

class java.util.TreeMap$KeySet
    .stream().limit(1).count() took 0.046ms
    spliterator = SubMapKeyIterator, estimateSize() = 9223372036854775807
class java.util.TreeMap$AscendingSubMap$AscendingEntrySetView
    .stream().limit(1).count() took 218ms
    spliterator = IteratorSpliterator, estimateSize() = 9999999 

After (entrySet is now fast, using SubMapEntryIterator):

class java.util.TreeMap$KeySet
	.stream().limit(1).count() took 0.017ms
	spliterator = SubMapKeyIterator, estimateSize() = 9223372036854775807
class java.util.TreeMap$AscendingSubMap$AscendingEntrySetView
	.stream().limit(1).count() took 0.013ms
	spliterator = SubMapEntryIterator, estimateSize() = 9223372036854775807

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-8372946: TreeMap sub-map entry spliterator is expensive (Enhancement - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28608

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 2, 2025

👋 Welcome back ogillespie! 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 Dec 2, 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 Dec 2, 2025
@openjdk
Copy link

openjdk bot commented Dec 2, 2025

@olivergillespie 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 Dec 2, 2025
@mlbridge
Copy link

mlbridge bot commented Dec 2, 2025

Webrevs

@olivergillespie
Copy link
Contributor Author

Tier2 test failure. I will investigate.

 java.util.ConcurrentModificationException
	at java.base/java.util.TreeMap$NavigableSubMap$SubMapIterator.prevEntry(TreeMap.java:2068)
	at java.base/java.util.TreeMap$NavigableSubMap$DescendingSubMapEntryIterator.next(TreeMap.java:2156)
	at java.base/java.util.TreeMap$NavigableSubMap$DescendingSubMapEntryIterator.forEachRemaining(TreeMap.java:2166)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:570)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:560)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:635)
	at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:291)
	at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:652)
	at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:658)
	at org.openjdk.tests.java.util.stream.CollectionAndMapModifyStreamTest.testEntrySetSizeRemove(CollectionAndMapModifyStreamTest.java:164)
	at org.openjdk.tests.java.util.stream.CollectionAndMapModifyStreamTest.testMapEntriesSizeRemove(CollectionAndMapModifyStreamTest.java:155)

@olivergillespie
Copy link
Contributor Author

Fixed the test failure by skipping that case. The test intentionally modifies the backing map while holding an iterator, which is not safe in general. It got away with it before, but the new implementation reasonably throws CME.
The test only runs on EntrySets, but the equivalent test would already fail on the current map.tailMap(...).keySet() implementation, so I think it's expected and reasonable that it now fails for descendingMap.entrySet(). Appreciate any second opinions, though.

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 31, 2025

@olivergillespie This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@olivergillespie
Copy link
Contributor Author

/keepalive

@openjdk
Copy link

openjdk bot commented Jan 21, 2026

@olivergillespie The pull request is being re-evaluated and the inactivity timeout has been reset.

return false;
}

public abstract Spliterator<Map.Entry<K,V>> spliterator();
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need this huge a patch. I think you should just do:

Suggested change
public abstract Spliterator<Map.Entry<K,V>> spliterator();
public Spliterator<Map.Entry<K,V>> spliterator() {
return Spliterators.spliterator(iterator(), Spliterator.DISTINCT);
}

Your patch is introducing spliterator behavioral changes unrelated to the performance regression fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for looking.

I suppose you mean Spliterators.spliteratorUnknownSize?

Hmm - I made the change this way to be consistent with the existing SubMapKeyIterator and DescendingSubMapKeyIterator, simply adding the same functionality for the Entry versions. Do you think those are overcomplicated too, or there's a reason they're like that that doesn't apply to the Entry versions? I don't know why they were originally added, to be honest, I didn't find much useful context in the history.

I don't know Spliterator well enough to spot any subtle behavioural differences, that's one reason I chose to follow the existing patterns.

DescendingSubMapEntryIterator is SORTED but SubMapEntryIterator is not, so I'd have to account for that too.

Copy link
Member

@liach liach Jan 27, 2026

Choose a reason for hiding this comment

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

I suppose you mean Spliterators.spliteratorUnknownSize?

Yes. Thanks for corecting me.

Hmm - I made the change this way to be consistent with the existing SubMapKeyIterator and DescendingSubMapKeyIterator, simply adding the same functionality for the Entry versions.

I made the recommendation given the starting point is to address a performance regression, instead of to enhance the sub-map entry spliterator to be on par with the DescendingSubMapKeyIterator.

From this starting point, I believe we can easily identify EntrySetView inherits Set::spliterator which is slow because the spliterator calls size() frequently. This root problem is easily fixed with using Spliterators.spliteratorUnknownSize, which also has the minimal behavioral impact.

In contrast, functional enhancement to spliterators is really a can of worms where you can never find an end - sometimes you add more flags, sometimes other splitting strategies. And in your example, you already have a test case failing due to the functional enhancements while you did make new tests to verify them.

So let's keep it simple, fix the bug, and leave the functional enhancements for another time. This also makes backporting the fix much easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay sounds good to me! Thanks for the suggestion, I'll update later this week.

Copy link
Contributor Author

@olivergillespie olivergillespie Jan 29, 2026

Choose a reason for hiding this comment

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

I created #29485 to add test cases before making this change - that required a slight functional tweak so I didn't want to include it in this change.

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.

2 participants