Skip to content

Commit 310426b

Browse files
committed
Start working on reducing the memory consumption
1 parent b34edb9 commit 310426b

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

Diff for: kotlinx-coroutines-core/common/src/internal/LockFreeLinkedList.common.kt

+11-15
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,18 @@ internal open class LockFreeLinkedListNode {
4646
}
4747

4848
/** @suppress **This is unstable API and it is subject to change.** */
49-
internal open class LockFreeLinkedListHead {
50-
private val head = LockFreeLinkedListSegment(
51-
id = 0,
52-
prev = null,
53-
pointers = 2,
54-
head = this,
55-
)
56-
private val tail = atomic(head)
49+
internal open class LockFreeLinkedListHead: LockFreeLinkedListSegment(
50+
id = 0,
51+
prev = null,
52+
pointers = 2,
53+
) {
54+
private val tail = atomic<LockFreeLinkedListSegment>(this)
5755
private val nextElement = atomic(0L)
5856

5957
/**
6058
* The list of bits that are forbidden from entering the list.
6159
*
62-
* TODO: we can store this in the extra bits in [head], there's enough space for that there, and it's never removed.
60+
* TODO: we can store this in `cleanedAndPointers`, there's enough space for that there.
6361
*/
6462
private val forbiddenBits: AtomicInt = atomic(0)
6563

@@ -122,17 +120,14 @@ internal open class LockFreeLinkedListHead {
122120
null
123121
}
124122
}
123+
124+
override val head: LockFreeLinkedListHead get() = this
125125
}
126126

127127
internal open class LockFreeLinkedListSegment(
128128
id: Long,
129129
prev: LockFreeLinkedListSegment?,
130130
pointers: Int,
131-
/** Used only during promoting of a single node to a list to ensure wait-freedom of the promotion operation.
132-
* Without this, promotion can't be implemented without a (possibly bounded) spin loop: once the node is committed
133-
* to be part of some list, the other threads can't do anything until that one thread sets the state to be the
134-
* head of the list. */
135-
@JvmField val head: LockFreeLinkedListHead,
136131
) : Segment<LockFreeLinkedListSegment>(id = id, prev = prev, pointers = pointers)
137132
{
138133
/** Each cell is a [LockFreeLinkedListNode], a [BrokenForSomeElements], or `null`. */
@@ -188,6 +183,8 @@ internal open class LockFreeLinkedListSegment(
188183
override fun onCancellation(index: Int, cause: Throwable?, context: CoroutineContext) {
189184
throw UnsupportedOperationException("Cancellation is not supported on LockFreeLinkedList")
190185
}
186+
187+
open val head: LockFreeLinkedListHead get() = prev!!.head
191188
}
192189

193190
internal class Address(@JvmField val segment: LockFreeLinkedListSegment, @JvmField val index: Int)
@@ -197,7 +194,6 @@ private fun createSegment(id: Long, prev: LockFreeLinkedListSegment): LockFreeLi
197194
id = id,
198195
prev = prev,
199196
pointers = 0,
200-
head = prev.head
201197
)
202198

203199
private const val SEGMENT_SIZE = 8

Diff for: kotlinx-coroutines-core/jvm/test/MemoryFootprintTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class MemoryFootprintTest : TestBase(true) {
1313

1414
@Test
1515
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
16+
assertTotalSize(jobWithChildren(1), 104) // original: 112
17+
assertTotalSize(jobWithChildren(2), 320) // original: 192
18+
assertTotalSize(jobWithChildren(3), 392) // original: 248
19+
assertTotalSize(jobWithChildren(4), 464) // original: 304
2020
}
2121

2222
private fun jobWithChildren(numberOfChildren: Int): Job {

0 commit comments

Comments
 (0)