Skip to content

Commit ffc7134

Browse files
author
Sergey Mashkov
committed
IO: minor performance improvement
1 parent f742bc8 commit ffc7134

File tree

1 file changed

+24
-2
lines changed
  • core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/internal

1 file changed

+24
-2
lines changed

core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/internal/RingBufferCapacity.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal class RingBufferCapacity(private val totalCapacity: Int) {
2424
}
2525

2626
fun tryReadExact(n: Int): Boolean {
27+
val AvailableForRead = AvailableForRead
2728
while (true) {
2829
val remaining = availableForRead
2930
if (remaining < n) return false
@@ -32,6 +33,7 @@ internal class RingBufferCapacity(private val totalCapacity: Int) {
3233
}
3334

3435
fun tryReadAtMost(n: Int): Int {
36+
val AvailableForRead = AvailableForRead
3537
while (true) {
3638
val remaining = availableForRead
3739
val delta = minOf(n, remaining)
@@ -41,6 +43,7 @@ internal class RingBufferCapacity(private val totalCapacity: Int) {
4143
}
4244

4345
fun tryWriteAtLeast(n: Int): Int {
46+
val AvailableForWrite = AvailableForWrite
4447
while (true) {
4548
val remaining = availableForWrite
4649
if (remaining < n) return 0
@@ -49,6 +52,7 @@ internal class RingBufferCapacity(private val totalCapacity: Int) {
4952
}
5053

5154
fun tryWriteExact(n: Int): Boolean {
55+
val AvailableForWrite = AvailableForWrite
5256
while (true) {
5357
val remaining = availableForWrite
5458
if (remaining < n) return false
@@ -57,6 +61,8 @@ internal class RingBufferCapacity(private val totalCapacity: Int) {
5761
}
5862

5963
fun tryWriteAtMost(n: Int): Int {
64+
val AvailableForWrite = AvailableForWrite
65+
6066
while (true) {
6167
val remaining = availableForWrite
6268
val delta = minOf(n, remaining)
@@ -66,27 +72,42 @@ internal class RingBufferCapacity(private val totalCapacity: Int) {
6672
}
6773

6874
fun completeRead(n: Int) {
75+
val totalCapacity = totalCapacity
76+
val AvailableForWrite = AvailableForWrite
77+
6978
while (true) {
7079
val remaining = availableForWrite
7180
val update = remaining + n
72-
require(update <= totalCapacity) { "Completed read overflow: $remaining + $n = $update > $totalCapacity" }
81+
if (update > totalCapacity) completeReadOverflow(remaining, update, n)
7382
if (AvailableForWrite.compareAndSet(this, remaining, update)) break
7483
}
7584
}
7685

86+
private fun completeReadOverflow(remaining: Int, update: Int, n: Int): Nothing {
87+
throw IllegalArgumentException("Completed read overflow: $remaining + $n = $update > $totalCapacity")
88+
}
89+
7790
fun completeWrite(n: Int) {
91+
val totalCapacity = totalCapacity
92+
val PendingToFlush = PendingToFlush
93+
7894
while (true) {
7995
val pending = pendingToFlush
8096
val update = pending + n
81-
require(update <= totalCapacity) { "Complete write overflow: $pending + $n > $totalCapacity" }
97+
if (update > totalCapacity) completeReadOverflow(pending, n)
8298
if (PendingToFlush.compareAndSet(this, pending, update)) break
8399
}
84100
}
85101

102+
private fun completeReadOverflow(pending: Int, n: Int): Nothing {
103+
throw IllegalArgumentException("Complete write overflow: $pending + $n > $totalCapacity")
104+
}
105+
86106
/**
87107
* @return true if there are bytes available for read after flush
88108
*/
89109
fun flush(): Boolean {
110+
val AvailableForRead = AvailableForRead
90111
val pending = PendingToFlush.getAndSet(this, 0)
91112
while (true) {
92113
val remaining = availableForRead
@@ -98,6 +119,7 @@ internal class RingBufferCapacity(private val totalCapacity: Int) {
98119
}
99120

100121
fun tryLockForRelease(): Boolean {
122+
val AvailableForWrite = AvailableForWrite
101123
while (true) {
102124
val remaining = availableForWrite
103125
if (pendingToFlush > 0 || availableForRead > 0 || remaining != totalCapacity) return false

0 commit comments

Comments
 (0)