Skip to content

Commit 7353c11

Browse files
authored
Fix increased memory consumption in JobSupport (#4110)
Fixup to 65ef6ea: that commit made `JobSupport` consume more memory by making the information about whether a callback should be invoked when the job enters the cancelling state be represented as a boolean flag and not part of the object class hierarchy. Now that flag is a virtual function instead.
1 parent 59ab6e8 commit 7353c11

File tree

7 files changed

+33
-12
lines changed

7 files changed

+33
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ private class AwaitAll<T>(private val deferreds: Array<out Deferred<T>>) {
115115
}
116116
}
117117

118-
override val onCancelling = false
118+
override val onCancelling get() = false
119119
}
120120
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,5 +675,5 @@ private class ChildContinuation(
675675
child.parentCancelled(child.getContinuationCancellationCause(job))
676676
}
677677

678-
override val onCancelling = true
678+
override val onCancelling get() = true
679679
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,5 +673,5 @@ private class DisposeOnCompletion(
673673
) : JobNode() {
674674
override fun invoke(cause: Throwable?) = handle.dispose()
675675

676-
override val onCancelling = false
676+
override val onCancelling get() = false
677677
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
577577
override fun invoke(cause: Throwable?) {
578578
select.trySelect(this@JobSupport, Unit)
579579
}
580-
override val onCancelling: Boolean = false
580+
override val onCancelling: Boolean get() = false
581581
}
582582

583583
/**
@@ -1410,14 +1410,14 @@ private class InvokeOnCompletion(
14101410
private val handler: CompletionHandler
14111411
) : JobNode() {
14121412
override fun invoke(cause: Throwable?) = handler.invoke(cause)
1413-
override val onCancelling = false
1413+
override val onCancelling get() = false
14141414
}
14151415

14161416
private class ResumeOnCompletion(
14171417
private val continuation: Continuation<Unit>
14181418
) : JobNode() {
14191419
override fun invoke(cause: Throwable?) = continuation.resume(Unit)
1420-
override val onCancelling = false
1420+
override val onCancelling get() = false
14211421
}
14221422

14231423
private class ResumeAwaitOnCompletion<T>(
@@ -1435,7 +1435,7 @@ private class ResumeAwaitOnCompletion<T>(
14351435
continuation.resume(state.unboxState() as T)
14361436
}
14371437
}
1438-
override val onCancelling = false
1438+
override val onCancelling get() = false
14391439
}
14401440

14411441
// -------- invokeOnCancellation nodes
@@ -1448,7 +1448,7 @@ private class InvokeOnCancelling(
14481448
override fun invoke(cause: Throwable?) {
14491449
if (_invoked.compareAndSet(expect = false, update = true)) handler.invoke(cause)
14501450
}
1451-
override val onCancelling = true
1451+
override val onCancelling get() = true
14521452
}
14531453

14541454
private class ChildHandleNode(
@@ -1457,5 +1457,5 @@ private class ChildHandleNode(
14571457
override val parent: Job get() = job
14581458
override fun invoke(cause: Throwable?) = childJob.parentCancelled(job)
14591459
override fun childCancelled(cause: Throwable): Boolean = job.childCancelled(cause)
1460-
override val onCancelling: Boolean = true
1460+
override val onCancelling: Boolean get() = true
14611461
}

kotlinx-coroutines-core/jvm/src/Future.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private class CancelFutureOnCompletion(
3737
if (cause != null) future.cancel(false)
3838
}
3939

40-
override val onCancelling = false
40+
override val onCancelling get() = false
4141
}
4242

4343
private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler {

kotlinx-coroutines-core/jvm/src/Interruptible.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private class ThreadState : JobNode() {
154154
}
155155
}
156156

157-
override val onCancelling = true
157+
override val onCancelling get() = true
158158

159159
private fun invalidState(state: Int): Nothing = error("Illegal state $state")
160160
}

kotlinx-coroutines-core/jvm/test/MemoryFootprintTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package kotlinx.coroutines
22

33
import kotlinx.coroutines.testing.*
44
import org.junit.Test
5-
import org.openjdk.jol.info.ClassLayout
5+
import org.openjdk.jol.info.*
66
import kotlin.test.*
77

88

@@ -11,6 +11,22 @@ class MemoryFootprintTest : TestBase(true) {
1111
@Test
1212
fun testJobLayout() = assertLayout(Job().javaClass, 24)
1313

14+
@Test
15+
fun testJobSize() {
16+
assertTotalSize(jobWithChildren(1), 112)
17+
assertTotalSize(jobWithChildren(2), 192) // + 80
18+
assertTotalSize(jobWithChildren(3), 248) // + 56
19+
assertTotalSize(jobWithChildren(4), 304) // + 56
20+
}
21+
22+
private fun jobWithChildren(numberOfChildren: Int): Job {
23+
val result = Job()
24+
repeat(numberOfChildren) {
25+
Job(result)
26+
}
27+
return result
28+
}
29+
1430
@Test
1531
fun testCancellableContinuationFootprint() = assertLayout(CancellableContinuationImpl::class.java, 48)
1632

@@ -19,4 +35,9 @@ class MemoryFootprintTest : TestBase(true) {
1935
// println(ClassLayout.parseClass(clz).toPrintable())
2036
assertEquals(expectedSize.toLong(), size)
2137
}
38+
39+
private fun assertTotalSize(instance: Job, expectedSize: Int) {
40+
val size = GraphLayout.parseInstance(instance).totalSize()
41+
assertEquals(expectedSize.toLong(), size)
42+
}
2243
}

0 commit comments

Comments
 (0)