Skip to content

[BEAM-408] - Fixes ProxyInvocationHandler uses inefficient Math.random() for random int#588

Closed
lucasamorimca wants to merge 3 commits intoapache:masterfrom
lucasamorimca:beam-408
Closed

[BEAM-408] - Fixes ProxyInvocationHandler uses inefficient Math.random() for random int#588
lucasamorimca wants to merge 3 commits intoapache:masterfrom
lucasamorimca:beam-408

Conversation

@lucasamorimca
Copy link
Contributor

* between 0 and {@link Integer#MAX_VALUE}.
*/
private final int hashCode = (int) (Math.random() * Integer.MAX_VALUE);
private final int hashCode = (RANDOM.nextInt() * Integer.MAX_VALUE);
Copy link
Member

Choose a reason for hiding this comment

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

Please swap to use ThreadLocalRandom.current().nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE) instead of the static RANDOM

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadLocalRandom.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably just .next(32) will work?

@lukecwik
Copy link
Member

lukecwik commented Jul 6, 2016

R: @lukecwik

* between 0 and {@link Integer#MAX_VALUE}.
*/
private final int hashCode = (int) (Math.random() * Integer.MAX_VALUE);
private final int hashCode = ThreadLocalRandom.current().nextInt(
Copy link
Contributor

Choose a reason for hiding this comment

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

please drop the extra

Copy link
Contributor

Choose a reason for hiding this comment

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

in = ThreadLocalRandom...

* between 0 and {@link Integer#MAX_VALUE}.
*/
private final int hashCode = (int) (Math.random() * Integer.MAX_VALUE);
private final int hashCode = ThreadLocalRandom.current().nextInt(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! cc @dhalperi

Copy link
Member

@lukecwik lukecwik Jul 7, 2016

Choose a reason for hiding this comment

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

It turns out you ran into an undocumented limit of nextInt, it can't generate such a large range since it overflows internally (when subtracting min from max):
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/concurrent/ThreadLocalRandom.java?av=f#143

I'll merge and update to use nextInt()

@lukecwik
Copy link
Member

lukecwik commented Jul 7, 2016

LGTM

@asfgit asfgit closed this in 66d726a Jul 7, 2016
@lucasamorimca lucasamorimca deleted the beam-408 branch July 8, 2016 23:13
Amar3tto added a commit to akvelon/beam that referenced this pull request Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants