You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, after orderValuesBy is called, it is known that the insertion order is irrelevant. So I suggest to optimize the builder by letting newMutableValueCollection return regular hash sets if valueComparator is not null.
As far as I see this is safe even if orderValuesBy is called only after some entries have been added. The builderMap will then contain a mixture of linked hash sets and regular hash sets, but this will not change anything.
Note that once a value comparator is set, it can never be unset (only replaced). Furthermore, the insertion order is not even relevant if duplicate (according to the comparator) values are added for one key, because when the ImmutableSortedSets for values are created during build these get deduplicated anyway.
Side note: When ImmutableSetMultimap.Builder.orderValuesBy is used, the values get deduplicated (per key) according to both equals and the comparator. This is different from both TreeMultimap and ImmutableListMultimap.Builder.oderValuesBy and could be unexpected. I suggest to add a note to orderValuesBy that requires that the comparator must be consistent with equals.
The same kind of optimization could be done with orderKeysBy and builderMap, the latter of which could be replaced by a regular hash map if it is still empty while orderKeysBy is called (for all kinds of ImmutableMultimaps). But the benefit is smaller here, of course.
The text was updated successfully, but these errors were encountered:
Currently
ImmutableSetMultimap.Builder
stores the values in linked hash sets in order to keep insertion order:guava/guava/src/com/google/common/collect/ImmutableSetMultimap.java
Lines 254 to 256 in ea4e950
However, after
orderValuesBy
is called, it is known that the insertion order is irrelevant. So I suggest to optimize the builder by lettingnewMutableValueCollection
return regular hash sets ifvalueComparator
is not null.As far as I see this is safe even if
orderValuesBy
is called only after some entries have been added. ThebuilderMap
will then contain a mixture of linked hash sets and regular hash sets, but this will not change anything.Note that once a value comparator is set, it can never be unset (only replaced). Furthermore, the insertion order is not even relevant if duplicate (according to the comparator) values are added for one key, because when the
ImmutableSortedSet
s for values are created duringbuild
these get deduplicated anyway.Side note: When
ImmutableSetMultimap.Builder.orderValuesBy
is used, the values get deduplicated (per key) according to bothequals
and the comparator. This is different from bothTreeMultimap
andImmutableListMultimap.Builder.oderValuesBy
and could be unexpected. I suggest to add a note toorderValuesBy
that requires that the comparator must be consistent withequals
.The same kind of optimization could be done with
orderKeysBy
andbuilderMap
, the latter of which could be replaced by a regular hash map if it is still empty whileorderKeysBy
is called (for all kinds ofImmutableMultimap
s). But the benefit is smaller here, of course.The text was updated successfully, but these errors were encountered: