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

Properly nest ThreadContextElement #2517

Merged
merged 2 commits into from
Feb 8, 2021
Merged

Properly nest ThreadContextElement #2517

merged 2 commits into from
Feb 8, 2021

Conversation

qwwdfsad
Copy link
Contributor

@qwwdfsad qwwdfsad commented Feb 2, 2021

* Restore the context in the reverse order of update, so they are properly nested into each other
* Also, do a minor cleanup

Fixes #2195

    * Restore the context in the reverse order of update, so they are properly nested into each other
    * Also, do a minor cleanup

Fixes #2195
@qwwdfsad
Copy link
Contributor Author

qwwdfsad commented Feb 2, 2021

I've also measured the potential overhead and it turned out that even tho we allocate slightly more, the average throughput of context restoration (ChannelSinkBenchmark.channelPipelineTwoThreadLocals) has been improved by 20%!

For single-shot coroutines without hot loops, the performance is roughly the same (few percents better, actually) with the cost of an additional small allocation.

As an observation of the second order, it seems like the CoroutineContext.fold is not as fast as we tend to think

Copy link
Contributor

@elizarov elizarov left a comment

Choose a reason for hiding this comment

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

Great improvement.

kotlinx-coroutines-core/jvm/src/internal/ThreadContext.kt Outdated Show resolved Hide resolved
@qwwdfsad qwwdfsad merged commit e21c455 into develop Feb 8, 2021
@qwwdfsad qwwdfsad deleted the restore-order branch February 8, 2021 09:05
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.

None yet

2 participants