Add missing lazy wrappers of KeySets for strict Maps #24767
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #24749.
The PR contains two parts, one tiny and the other more involved.
KeySetperformance issuesPreviously we use a
List[K]to hold keys inside the strict KeySet, in order tokeep iteration order stable. However this degrades performance of KeySet as a Set.
The fix is to simply use something that has both. Currently it means
mutable.LinkedHashSet,so we use it, even though we don't intend to mutate the set at all.
Missing
LazyKeySetimplementations for some strict MapsAs part of #23769 we categorised most data structures to be strict/pure (as in "not hiding any
computation as part of its structure"), which includes
Set.This however clashes with how
.keySetmethod was implemented: it returns aSet[K], butprovides no guarantee on whether it is a lazy view over the underlying data structure
(which can be lazy, e.g. when you call
.keySetfrom aMapView) or a strict set.With API compatibility in mind, we now create strict key sets (with a copy of the keys)
in the generic case, while trying to detect whether the
.keySetmethod was called from analready strict data-structure (which is the majority case) - returning a thin-wrapper
LazyKeySetin that case. As the LazyKeySet works like a view with no aggregation, applying it over a strict
data structure doesn't violate strictness.
This left a few holes however, which was overlooked: some data structures such as
mutable.HashSetoverrides
.keySetwith their own (protected!) classes, extending from the previously-ambiguousKeySet classes (that is now made strict), and accidentally ending up always using the strict version.
Unfortunately we cannot modify these extended classes (for TASTy compatibility), so similar versions
extending the thin wrapper are added instead, and the old classes deprecated.
To not shoot ourselves in the foot in the future, these classes are also made
private[collection]:We don't expect classes like
HashMapto be extended with a customKeySet, and in the cases thatit does, it is not a problem to reimplement the missing 4-5 methods in the most efficient way for your
extended data structure.