Skip to content

Commit 57c7902

Browse files
committed
Optimize attaching the debug probes
I see a notable speed improvement even without any benchmarks, simply by running `DebugProbes.withDebugProbes { }` in a loop.
1 parent 80a5bc7 commit 57c7902

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

kotlinx-coroutines-debug/src/Attach.kt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,30 @@ internal class ByteBuddyDynamicAttach : Function1<Boolean, Unit> {
1616

1717
private fun attach() {
1818
ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.ForEmulatedAttachment.INSTANCE)
19-
val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt")
20-
val cl2 = Class.forName("kotlinx.coroutines.debug.internal.DebugProbesKt")
21-
22-
ByteBuddy()
23-
.redefine(cl2)
24-
.name(cl.name)
25-
.make()
26-
.load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent())
19+
dynamicTypeWithProbes.load(targetClassLoader, ClassReloadingStrategy.fromInstalledAgent())
2720
}
2821

2922
private fun detach() {
30-
val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt")
31-
val cl2 = Class.forName("kotlinx.coroutines.debug.NoOpProbesKt")
32-
ByteBuddy()
33-
.redefine(cl2)
34-
.name(cl.name)
35-
.make()
36-
.load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent())
23+
dynamicTypeWithoutProbes.load(targetClassLoader, ClassReloadingStrategy.fromInstalledAgent())
3724
}
3825
}
26+
27+
private val targetClassLoader = Class.forName(classNameToOverride).classLoader
28+
29+
private const val classNameToOverride = "kotlin.coroutines.jvm.internal.DebugProbesKt"
30+
31+
private val dynamicTypeWithProbes by lazy {
32+
val classWithProbes = Class.forName("kotlinx.coroutines.debug.internal.DebugProbesKt")
33+
ByteBuddy()
34+
.redefine(classWithProbes)
35+
.name(classNameToOverride)
36+
.make()
37+
}
38+
39+
private val dynamicTypeWithoutProbes by lazy {
40+
val classWithoutProbes = Class.forName("kotlinx.coroutines.debug.NoOpProbesKt")
41+
ByteBuddy()
42+
.redefine(classWithoutProbes)
43+
.name(classNameToOverride)
44+
.make()
45+
}

0 commit comments

Comments
 (0)