Skip to content

Commit f7c3e6b

Browse files
committed
~: code improvements
1 parent b8295d1 commit f7c3e6b

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

common/kotlinx-coroutines-core-common/src/Dispatched.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,6 @@ internal fun <T> DispatchedTask<T>.resume(delegate: Continuation<T>, useMode: In
280280

281281

282282
@Suppress("NOTHING_TO_INLINE")
283-
internal inline fun Continuation<*>.resumeWithStackTrace(exception: Throwable) = resumeWith(Result.failure(recoverStackTrace(exception, this)))
283+
internal inline fun Continuation<*>.resumeWithStackTrace(exception: Throwable) {
284+
resumeWith(Result.failure(recoverStackTrace(exception, this)))
285+
}

common/kotlinx-coroutines-core-common/src/Timeout.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.coroutines
66

7+
import kotlinx.coroutines.internal.*
78
import kotlinx.coroutines.intrinsics.*
89
import kotlinx.coroutines.selects.*
910
import kotlin.coroutines.*
@@ -80,9 +81,10 @@ private fun <U, T: U> setupTimeout(
8081
private open class TimeoutCoroutine<U, in T: U>(
8182
@JvmField val time: Long,
8283
@JvmField val uCont: Continuation<U> // unintercepted continuation
83-
) : AbstractCoroutine<T>(uCont.context, active = true), Runnable, Continuation<T> {
84+
) : AbstractCoroutine<T>(uCont.context, active = true), Runnable, Continuation<T>, CoroutineStackFrame {
8485
override val defaultResumeMode: Int get() = MODE_DIRECT
85-
86+
override val callerFrame: CoroutineStackFrame? get() = (uCont as? CoroutineStackFrame)?.callerFrame
87+
override fun getStackTraceElement(): StackTraceElement? = (uCont as? CoroutineStackFrame)?.getStackTraceElement()
8688
@Suppress("LeakingThis", "Deprecation")
8789
override fun run() {
8890
cancel(TimeoutCancellationException(time, this))

core/kotlinx-coroutines-core/src/internal/Exceptions.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,10 @@ internal actual fun <E : Throwable> recoverStackTrace(exception: E): E {
1919
}
2020

2121
private fun <E : Throwable> E.sanitizeStackTrace(): E {
22+
val stackTrace = stackTrace
2223
val size = stackTrace.size
2324

24-
var lastIntrinsic = -1
25-
for (i in 0 until size) {
26-
val name = stackTrace[i].className
27-
if ("kotlinx.coroutines.internal.ExceptionsKt" == name) {
28-
lastIntrinsic = i
29-
}
30-
}
31-
25+
val lastIntrinsic = stackTrace.indexOfFirst { "kotlinx.coroutines.internal.ExceptionsKt" == it.className }
3226
val startIndex = lastIntrinsic + 1
3327
val trace = Array(size - lastIntrinsic) {
3428
if (it == 0) {
@@ -38,7 +32,7 @@ private fun <E : Throwable> E.sanitizeStackTrace(): E {
3832
}
3933
}
4034

41-
stackTrace = trace
35+
setStackTrace(trace)
4236
return this
4337
}
4438

@@ -74,6 +68,12 @@ internal actual fun <E : Throwable> unwrap(exception: E): E {
7468
return exception
7569
}
7670

71+
val cause = exception.cause
72+
// Fast-path to avoid array cloning
73+
if (cause == null || cause.javaClass != exception.javaClass) {
74+
return exception
75+
}
76+
7777
val element = exception.stackTrace.firstOrNull() ?: return exception
7878
if (element.isArtificial()) {
7979
@Suppress("UNCHECKED_CAST")
@@ -102,7 +102,7 @@ internal fun sanitize(element: StackTraceElement): StackTraceElement {
102102
if (!element.className.contains('/')) {
103103
return element
104104
}
105-
// STE generated with debug metadata contains '/' as separators in FQN, while Java contains dots
105+
// KT-28237: STE generated with debug metadata contains '/' as separators in FQN, while Java contains dots
106106
return StackTraceElement(element.className.replace('/', '.'), element.methodName, element.fileName, element.lineNumber)
107107
}
108108
internal fun artificialFrame(message: String) = java.lang.StackTraceElement("\b\b\b($message", "\b", "\b", -1)

0 commit comments

Comments
 (0)