kotlinx-coroutines-core/jvm/src/internal/ThreadContext.kt [15:39]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - countOrElement is Int -> { // slow path for multiple active ThreadContextElements, allocates ThreadState for multiple old values context.fold(ThreadState(context, countOrElement), updateState) } else -> { // fast path for one ThreadContextElement (no allocations, no additional context scan) @Suppress("UNCHECKED_CAST") val element = countOrElement as ThreadContextElement element.updateThreadContext(context) } } } internal fun restoreThreadContext(context: CoroutineContext, oldState: Any?) { when { oldState === NO_THREAD_ELEMENTS -> return // very fast path when there are no ThreadContextElements oldState is ThreadState -> { // slow path with multiple stored ThreadContextElements oldState.restore(context) } else -> { // fast path for one ThreadContextElement, but need to find it @Suppress("UNCHECKED_CAST") val element = context.fold(null, findOne) as ThreadContextElement element.restoreThreadContext(context, oldState) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - kotlinx-coroutines-core/native/src/internal/ThreadContext.kt [14:38]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - countOrElement is Int -> { // slow path for multiple active ThreadContextElements, allocates ThreadState for multiple old values context.fold(ThreadState(context, countOrElement), updateState) } else -> { // fast path for one ThreadContextElement (no allocations, no additional context scan) @Suppress("UNCHECKED_CAST") val element = countOrElement as ThreadContextElement element.updateThreadContext(context) } } } internal fun restoreThreadContext(context: CoroutineContext, oldState: Any?) { when { oldState === NO_THREAD_ELEMENTS -> return // very fast path when there are no ThreadContextElements oldState is ThreadState -> { // slow path with multiple stored ThreadContextElements oldState.restore(context) } else -> { // fast path for one ThreadContextElement, but need to find it @Suppress("UNCHECKED_CAST") val element = context.fold(null, findOne) as ThreadContextElement element.restoreThreadContext(context, oldState) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -