Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ImmutableSetMultimap.Builder with sorted values #3963

Open
PhilippWendler opened this issue Jul 14, 2020 · 2 comments
Open

Optimize ImmutableSetMultimap.Builder with sorted values #3963

PhilippWendler opened this issue Jul 14, 2020 · 2 comments
Labels

Comments

@PhilippWendler
Copy link

Currently ImmutableSetMultimap.Builder stores the values in linked hash sets in order to keep insertion order:

Collection<V> newMutableValueCollection() {
return Platform.preservesInsertionOrderOnAddsSet();
}

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.

@souradeepmajumdar05
Copy link

i would like to work on this @netdpb

@netdpb
Copy link
Member

netdpb commented Sep 15, 2020

I don't think we're ready for any implementation work. Do you have a sense of how much time or space this optimization might save?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants