-
-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use Random as a ThreadLocal<> * changelog * code review changes
- Loading branch information
Showing
7 changed files
with
107 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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) | ||
} | ||
} |