Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization: resizable workers array #3137

Merged
merged 6 commits into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions kotlinx-coroutines-core/jvm/src/internal/ResizableAtomicArray.kt
@@ -0,0 +1,41 @@
/*
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.internal

import kotlinx.coroutines.*
import java.util.concurrent.atomic.*

/**
* Atomic array with lock-free reads and synchronized modifications. It logically has an unbounded size,
* is implicitly filled with nulls, and is resized on updates as needed to grow.
*/
internal class ResizableAtomicArray<T>(initialLength: Int) {
elizarov marked this conversation as resolved.
Show resolved Hide resolved
@Volatile
private var array = AtomicReferenceArray<T>(initialLength)

// for debug output
public fun currentLength(): Int = array.length()

public operator fun get(index: Int): T? {
assert { index >= 0 }
elizarov marked this conversation as resolved.
Show resolved Hide resolved
val array = this.array // volatile read
return if (index < array.length()) array[index] else null
}

@Synchronized
elizarov marked this conversation as resolved.
Show resolved Hide resolved
operator fun set(index: Int, value: T?) {
assert { index >= 0 }
elizarov marked this conversation as resolved.
Show resolved Hide resolved
val curArray = this.array
val curLen = curArray.length()
if (index < curLen) {
curArray[index] = value
} else {
val newArray = AtomicReferenceArray<T>((index + 1).coerceAtLeast(2 * curLen))
for (i in 0 until curLen) newArray[i] = curArray[i]
newArray[index] = value
array = newArray // copy done
qwwdfsad marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Up @@ -9,7 +9,6 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import java.io.*
import java.util.concurrent.*
import java.util.concurrent.atomic.*
import java.util.concurrent.locks.*
import kotlin.math.*
import kotlin.random.*
Expand Down Expand Up @@ -261,7 +260,7 @@ internal class CoroutineScheduler(
* works properly
*/
@JvmField
val workers = AtomicReferenceArray<Worker?>(maxPoolSize + 1)
val workers = ResizableAtomicArray<Worker>(corePoolSize + 1)

/**
* Long describing state of workers in this pool.
Expand Down Expand Up @@ -525,7 +524,7 @@ internal class CoroutineScheduler(
var dormant = 0
var terminated = 0
val queueSizes = arrayListOf<String>()
for (index in 1 until workers.length()) {
for (index in 1 until workers.currentLength()) {
val worker = workers[index] ?: continue
val queueSize = worker.localQueue.size
when (worker.state) {
Expand Down