-
-
Notifications
You must be signed in to change notification settings - Fork 461
Use Random through ThreadLocal<Random>
#3835
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package io.sentry.util; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * This SentryRandom is a compromise used for improving performance of the SDK. | ||
| * | ||
| * <p>We did some testing where using Random from multiple threads degrades performance | ||
| * significantly. We opted for this approach as it wasn't easily possible to vendor | ||
| * ThreadLocalRandom since it's using advanced features that can cause java.lang.IllegalAccessError. | ||
| */ | ||
| public final class SentryRandom { | ||
|
|
||
| private static final @NotNull SentryRandomThreadLocal instance = new SentryRandomThreadLocal(); | ||
|
|
||
| /** | ||
| * Returns the current threads instance of {@link Random}. An instance of {@link Random} will be | ||
| * created the first time this is invoked on each thread. | ||
| * | ||
| * <p>NOTE: Avoid holding a reference to the returned {@link Random} instance as sharing a | ||
| * reference across threads (while being thread-safe) will likely degrade performance | ||
| * significantly. | ||
| * | ||
| * @return random | ||
| */ | ||
| public static @NotNull Random current() { | ||
| return instance.get(); | ||
| } | ||
|
|
||
| private static class SentryRandomThreadLocal extends ThreadLocal<Random> { | ||
|
|
||
| @Override | ||
| protected Random initialValue() { | ||
| return new Random(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package io.sentry.util | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertNotSame | ||
| import kotlin.test.assertSame | ||
|
|
||
| class SentryRandomTest { | ||
|
|
||
| @Test | ||
| fun `thread local creates a new instance per thread but keeps re-using it for the same thread`() { | ||
| val mainThreadRandom1 = SentryRandom.current() | ||
| val mainThreadRandom2 = SentryRandom.current() | ||
| assertSame(mainThreadRandom1, mainThreadRandom2) | ||
|
|
||
| var thread1Random1: Random? = null | ||
| var thread1Random2: Random? = null | ||
|
|
||
| val thread1 = Thread() { | ||
| thread1Random1 = SentryRandom.current() | ||
| thread1Random2 = SentryRandom.current() | ||
| } | ||
|
|
||
| var thread2Random1: Random? = null | ||
| var thread2Random2: Random? = null | ||
|
|
||
| val thread2 = Thread() { | ||
| thread2Random1 = SentryRandom.current() | ||
| thread2Random2 = SentryRandom.current() | ||
| } | ||
|
|
||
| thread1.start() | ||
| thread2.start() | ||
| thread1.join() | ||
| thread2.join() | ||
|
|
||
| assertSame(thread1Random1, thread1Random2) | ||
| assertNotSame(mainThreadRandom1, thread1Random1) | ||
|
|
||
| assertSame(thread2Random1, thread2Random2) | ||
| assertNotSame(mainThreadRandom1, thread2Random1) | ||
|
|
||
| val mainThreadRandom3 = SentryRandom.current() | ||
| assertSame(mainThreadRandom1, mainThreadRandom3) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.