-
Notifications
You must be signed in to change notification settings - Fork 328
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
Improvements in ThreadLocalStorage #553
Merged
Merged
Conversation
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
All credits to FastThreadLocal by @trask |
👍 |
codefromthecrypt
pushed a commit
to openzipkin/brave
that referenced
this pull request
Sep 26, 2018
the classloader thing happens even if you clear every time.. it is a weird
one! we have a kindof horrible framework to show this problem
Ex this test will fail even if you clear the ref.. the test below will fail
until it is a weak ref, but weak ref isn't cheap either..
https://github.com/openzipkin/brave/blob/master/brave-tests/src/main/java/brave/test/propagation/CurrentTraceContextTest.java#L265
https://github.com/openzipkin/brave/blob/master/brave-tests/src/main/java/brave/test/util/ClassLoaders.java
…On Wed, Sep 26, 2018 at 7:27 PM Diego Parra ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In kamon-core/src/main/scala/kamon/context/Storage.scala
<#553 (comment)>:
> class ThreadLocal extends Storage {
- private val tls = new java.lang.ThreadLocal[Context]() {
- override def initialValue(): Context = Context.Empty
+ private val tls = new java.lang.ThreadLocal[ContextHolder]() {
+ override def initialValue() = new ContextHolder(Context.Empty)
@adriancole <https://github.com/adriancole> thanks for pointing that, yes
you are right, here we can have a classloader leak if the context is not
correctly closed, what do you think about something like this:
final class ContextHolder(var value:Context) extends WeakReference(value)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#553 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD611CZHJERduiTNsyq8EWcNLKOczBXks5ufA1GgaJpZM4WkcRy>
.
|
cheap being relative to a plain ref or array access I mean.. not saying
weakref isn't cheap enough
…On Wed, Sep 26, 2018 at 7:34 PM Adrian Cole ***@***.***> wrote:
the classloader thing happens even if you clear every time.. it is a weird
one! we have a kindof horrible framework to show this problem
Ex this test will fail even if you clear the ref.. the test below will
fail until it is a weak ref, but weak ref isn't cheap either..
https://github.com/openzipkin/brave/blob/master/brave-tests/src/main/java/brave/test/propagation/CurrentTraceContextTest.java#L265
https://github.com/openzipkin/brave/blob/master/brave-tests/src/main/java/brave/test/util/ClassLoaders.java
On Wed, Sep 26, 2018 at 7:27 PM Diego Parra ***@***.***>
wrote:
> ***@***.**** commented on this pull request.
> ------------------------------
>
> In kamon-core/src/main/scala/kamon/context/Storage.scala
> <#553 (comment)>:
>
> > class ThreadLocal extends Storage {
> - private val tls = new java.lang.ThreadLocal[Context]() {
> - override def initialValue(): Context = Context.Empty
> + private val tls = new java.lang.ThreadLocal[ContextHolder]() {
> + override def initialValue() = new ContextHolder(Context.Empty)
>
> @adriancole <https://github.com/adriancole> thanks for pointing that,
> yes you are right, here we can have a classloader leak if the context is
> not correctly closed, what do you think about something like this:
>
> final class ContextHolder(var value:Context) extends WeakReference(value)
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#553 (comment)>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAD611CZHJERduiTNsyq8EWcNLKOczBXks5ufA1GgaJpZM4WkcRy>
> .
>
|
ivantopo
requested changes
Sep 28, 2018
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.
Couple stupid comments, but in general looks good to me! Thanks @adriancole for the support and @trask for the original awesomeness :D
kamon-core-bench/src/main/scala/kamon/bench/ThreadLocalStorageBenchmark.scala
Outdated
Show resolved
Hide resolved
* Remove unnecessary resolver
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Wrapper that implements optimized
ThreadLocal
access pattern ideal for heavily used ThreadLocals.It is faster to use a mutable holder object and always perform
ThreadLocal.get()
and never useThreadLocal.set()
, because the value is more likely to be found in theThreadLocalMap
direct hashslot and avoid the slow path
ThreadLocalMap.getEntryAfterMiss()
.With a twist proposed by @adriancole where we don't use an
Kamon-defined
holder object as that would prevent class unloading.JMH Results:
taskset -c 0-2 sbt 'coreBench/jmh:run -i 10 -wi 5 -t2 .*Benchmark.*'