-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Avoid resizing of fixed-size HashSet
/LinkedHashSet
variants
#32291
Conversation
aa09ee0
to
c663c5d
Compare
public static <E> LinkedHashSet<E> newLinkedHashSet(int expectedSize) { | ||
return new LinkedHashSet<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR); | ||
} | ||
|
||
private static int computeMapInitialCapacity(int expectedSize) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private static int computeMapInitialCapacity(int expectedSize) { | |
private static int computeInitialCapacity(int expectedSize) { |
Probably better to rename this method since it's now used for maps and sets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It sort of makes sense I guess since it ends up being backed by a Map.
True. I just thought the new name would require less cognitive overhead for anyone reading the code. Though, to be honest, it's an internal helper method. So it doesn't really matter.
In any case, thanks for making the change.
On a side note, I just learned that there's a new HashSet.newHashSet()
factory method in Java 19. So thanks for that. 👍
Though we still have a Java 17 baseline.
Add helpers to CollectionUtils for building HashSets and LinkedHashSets that can hold an expected number of elements without needing to resize / rehash.
c663c5d
to
8a90e3a
Compare
I also wanted to note that part of the reason for this PR is that I'd like to be able to rely on this helper in Spring projects that can't use the JDK19 helpers, and where we don't necessarily want to rely on Guava. |
* @param expectedSize the expected number of elements (with a corresponding | ||
* capacity to be derived so that no resize/rehash operations are needed) | ||
* @see #newLinkedHashSet(int) | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, please remember to add @since
tags to newly introduced non-private` methods and to update the copyright headers of all affected files.
Though no need to do that for this PR: I'll take care of that when merging.
HashSet
/LinkedHashSet
variants
Add helpers to CollectionUtils for building HashSets and LinkedHashSets that can hold an expected number of elements without needing to resize / rehash.
I also went ahead and updated places throughout the code base that appeared to be potentially sizing HashSet / LinkedHashSet incorrectly. This is by no means exhaustive.
See ff11467, where this was added for HashMap / LinkedHashMap.