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

Forbid easily misused HashSet and HashMap constructors #9165

Merged
merged 9 commits into from
Feb 7, 2020

Conversation

capistrant
Copy link
Contributor

@capistrant capistrant commented Jan 10, 2020

Fixes #8423

Description

This PR adds constructors for HashMap, HashSet, and LinkedHashSet that specify an initial size parameter to the forbiddenapi list. It replaces all usage of these constructors with Guava helper methods that take the users parameter for size and create a more optimally sized object for them compared to what they get from the native constructors.

HashMap, HashSet and LinkedHashSet objects are often created in an inefficient way when the creator specifies a size. Guava has created utilities to take the size desired by the creator and come up with a proper initial size for the underlying object for them. It is explained more clearly here. The same is also true for LinkedHashMap, but unfortunately we are still using Guava 16, and the Guava implementation for LinkedHashMaps did not come about until Guava 19.

I had to add an ignores item in the pom.xml file for SomeAvroDatum.class because it is generated at build time so it can't have a suppression annotation inline. If there is a better way to exclude that I'm not thinking of, please let me know.


This PR has:

  • been self-reviewed
  • been tested in a test Druid cluster.

Key changed/added classes in this PR

Many classes were modified, but the changes are all transparent. Just changing the way Map and Set objects are created in some specific cases.

@capistrant
Copy link
Contributor Author

capistrant commented Jan 14, 2020

Edit: Not sure what triggered things to run again, but build appears to be green now 🤷‍♂

The travis-ci logs for the failed build steps all appear to be a 5XX error pulling external resources. Any way to get the build restarted short of pushing a dummy commit?

@jon-wei
Copy link
Contributor

jon-wei commented Jan 14, 2020

@capistrant I restarted the failed runs earlier, was going to comment but got pulled away for a moment

java.util.HashSet#<init>(int) @ Use com.google.collect.Sets#newHashSetWithExpectedSize(int) instead
java.util.HashSet#<init>(int, float) @ Use com.google.collect.Sets#newHashSetWithExpectedSize(int) instead
java.util.LinkedHashSet#<init>(int) @ Use com.google.collect.Sets#newLinkedHashSatWithExpectedSize(int) instead
java.util.LinkedHashSet#<init>(int, float) @ Use com.google.collect.Sets#newLinkedHashSatWithExpectedSize(int) instead
Copy link
Member

Choose a reason for hiding this comment

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

LinkedHashMap should be prohibited, too, even though there is no method ready in Guava. A polyfill method could be added to CollectionUtils.

@@ -39,6 +41,8 @@

public final class CollectionUtils
{
public static final int MAX_EXPECTED_SIZE = (1 << 30);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: should better be private.

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 catching that

@capistrant
Copy link
Contributor Author

Looks like a non-related TravisCI failure due to external dependency pulls

@@ -1341,6 +1341,9 @@
<signaturesFile>${project.parent.basedir}/codestyle/joda-time-forbidden-apis.txt</signaturesFile>
<signaturesFile>${project.parent.basedir}/codestyle/druid-forbidden-apis.txt</signaturesFile>
</signaturesFiles>
<excludes>
<exclude>**/SomeAvroDatum.class</exclude>
Copy link
Member

Choose a reason for hiding this comment

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

I've created https://issues.apache.org/jira/browse/AVRO-2731 and #9331 to track this. FYI @Fokko.

@capistrant please try to create such issues yourself to close the loops and offload info from the heads of people.

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

Successfully merging this pull request may close these issues.

Prohibit HashMap(capacity), HashMap(capacity, loadFactor), HashSet, LinkedHashMap constructors
5 participants