Skip to content

Commit f75ec15

Browse files
author
Sergey Mashkov
committed
IO: eliminate state machines, workaround ACC_FINAL missing flag
1 parent b94ce42 commit f75ec15

File tree

4 files changed

+94
-65
lines changed

4 files changed

+94
-65
lines changed

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

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,15 @@ internal class ByteBufferChannel(
395395
else consumed + consumed0
396396
}
397397

398-
suspend override fun readFully(dst: ByteArray, offset: Int, length: Int) {
398+
final suspend override fun readFully(dst: ByteArray, offset: Int, length: Int) {
399399
val consumed = readAsMuchAsPossible(dst, offset, length)
400400

401401
if (consumed < length) {
402-
readFullySuspend(dst, offset + consumed, length - consumed)
402+
return readFullySuspend(dst, offset + consumed, length - consumed)
403403
}
404404
}
405405

406-
suspend override fun readFully(dst: ByteBuffer): Int {
406+
final suspend override fun readFully(dst: ByteBuffer): Int {
407407
val rc = readAsMuchAsPossible(dst)
408408
if (!dst.hasRemaining()) return rc
409409

@@ -491,7 +491,7 @@ internal class ByteBufferChannel(
491491
return readAvailable(dst)
492492
}
493493

494-
suspend override fun readPacket(size: Int, headerSizeHint: Int): ByteReadPacket {
494+
final suspend override fun readPacket(size: Int, headerSizeHint: Int): ByteReadPacket {
495495
closed?.cause?.let { throw it }
496496

497497
if (size == 0) return ByteReadPacket.Empty
@@ -525,7 +525,7 @@ internal class ByteBufferChannel(
525525
BufferPool.recycle(buffer)
526526
builder.build()
527527
} else {
528-
readPacketSuspend(remaining, builder, buffer)
528+
return readPacketSuspend(remaining, builder, buffer)
529529
}
530530
}
531531

@@ -557,7 +557,7 @@ internal class ByteBufferChannel(
557557
}
558558
}
559559

560-
suspend override fun readByte(): Byte {
560+
final suspend override fun readByte(): Byte {
561561
var b: Byte = 0
562562

563563
val rc = reading {
@@ -568,10 +568,10 @@ internal class ByteBufferChannel(
568568
} else false
569569
}
570570

571-
return if (rc) {
572-
b
571+
if (rc) {
572+
return b
573573
} else {
574-
readByteSuspend()
574+
return readByteSuspend()
575575
}
576576
}
577577

@@ -580,7 +580,7 @@ internal class ByteBufferChannel(
580580
return readByte()
581581
}
582582

583-
suspend override fun readBoolean(): Boolean {
583+
final suspend override fun readBoolean(): Boolean {
584584
var b = false
585585

586586
val rc = reading {
@@ -591,10 +591,10 @@ internal class ByteBufferChannel(
591591
} else false
592592
}
593593

594-
return if (rc) {
595-
b
594+
if (rc) {
595+
return b
596596
} else {
597-
readBooleanSuspend()
597+
return readBooleanSuspend()
598598
}
599599
}
600600

@@ -603,7 +603,7 @@ internal class ByteBufferChannel(
603603
return readBoolean()
604604
}
605605

606-
suspend override fun readShort(): Short {
606+
final suspend override fun readShort(): Short {
607607
var sh: Short = 0
608608

609609
val rc = reading {
@@ -615,10 +615,10 @@ internal class ByteBufferChannel(
615615
} else false
616616
}
617617

618-
return if (rc) {
619-
sh
618+
if (rc) {
619+
return sh
620620
} else {
621-
readShortSuspend()
621+
return readShortSuspend()
622622
}
623623
}
624624

@@ -627,7 +627,7 @@ internal class ByteBufferChannel(
627627
return readShort()
628628
}
629629

630-
suspend override fun readInt(): Int {
630+
final suspend override fun readInt(): Int {
631631
var i = 0
632632

633633
val rc = reading {
@@ -639,10 +639,10 @@ internal class ByteBufferChannel(
639639
} else false
640640
}
641641

642-
return if (rc) {
643-
i
642+
if (rc) {
643+
return i
644644
} else {
645-
readIntSuspend()
645+
return readIntSuspend()
646646
}
647647
}
648648

@@ -651,7 +651,7 @@ internal class ByteBufferChannel(
651651
return readInt()
652652
}
653653

654-
suspend override fun readLong(): Long {
654+
final suspend override fun readLong(): Long {
655655
var i = 0L
656656

657657
val rc = reading {
@@ -663,10 +663,10 @@ internal class ByteBufferChannel(
663663
} else false
664664
}
665665

666-
return if (rc) {
667-
i
666+
if (rc) {
667+
return i
668668
} else {
669-
readLongSuspend()
669+
return readLongSuspend()
670670
}
671671
}
672672

@@ -675,7 +675,7 @@ internal class ByteBufferChannel(
675675
return readLong()
676676
}
677677

678-
suspend override fun readDouble(): Double {
678+
final suspend override fun readDouble(): Double {
679679
var d = 0.0
680680

681681
val rc = reading {
@@ -687,10 +687,10 @@ internal class ByteBufferChannel(
687687
} else false
688688
}
689689

690-
return if (rc) {
691-
d
690+
if (rc) {
691+
return d
692692
} else {
693-
readDoubleSuspend()
693+
return readDoubleSuspend()
694694
}
695695
}
696696

@@ -699,7 +699,7 @@ internal class ByteBufferChannel(
699699
return readDouble()
700700
}
701701

702-
suspend override fun readFloat(): Float {
702+
final suspend override fun readFloat(): Float {
703703
var f = 0.0f
704704

705705
val rc = reading {
@@ -711,10 +711,10 @@ internal class ByteBufferChannel(
711711
} else false
712712
}
713713

714-
return if (rc) {
715-
f
714+
if (rc) {
715+
return f
716716
} else {
717-
readFloatSuspend()
717+
return readFloatSuspend()
718718
}
719719
}
720720

@@ -902,15 +902,18 @@ internal class ByteBufferChannel(
902902
}
903903

904904
suspend override fun writeInt(i: Int) {
905-
val delegated = resolveDelegation(this)
906-
if (delegated !== this) return delegated.writeInt(i)
907-
908-
val buffer = setupStateForWrite() ?: return delegateInt(i)
905+
val buffer = setupStateForWrite()
906+
if (buffer == null) {
907+
val delegation = resolveDelegation(this)
908+
if (delegation !== this) return delegation.writeInt(i)
909+
else return delegateSuspend(joining!!, { writeInt(i) })
910+
}
909911
val c = state.capacity
910-
911-
if (!buffer.tryWriteInt(i, c)) {
912-
return buffer.writeIntSuspend(i, c)
912+
//
913+
if (buffer.tryWriteInt(i, c)) {
914+
return
913915
}
916+
return buffer.writeIntSuspend(i, c)
914917
}
915918

916919
private tailrec suspend fun ByteBuffer.writeIntSuspend(i: Int, c: RingBufferCapacity) {
@@ -992,11 +995,11 @@ internal class ByteBufferChannel(
992995
}
993996

994997
suspend override fun writeDouble(d: Double) {
995-
writeLong(java.lang.Double.doubleToRawLongBits(d))
998+
return writeLong(java.lang.Double.doubleToRawLongBits(d))
996999
}
9971000

9981001
suspend override fun writeFloat(f: Float) {
999-
writeInt(java.lang.Float.floatToRawIntBits(f))
1002+
return writeInt(java.lang.Float.floatToRawIntBits(f))
10001003
}
10011004

10021005
suspend override fun writeAvailable(src: ByteBuffer): Int {
@@ -1078,7 +1081,7 @@ internal class ByteBufferChannel(
10781081
val joined = joining
10791082

10801083
if (joined != null) {
1081-
joined.awaitClose()
1084+
return joined.awaitClose()
10821085
} else if (closed == null) {
10831086
error("Only works for joined")
10841087
}
@@ -1091,6 +1094,10 @@ internal class ByteBufferChannel(
10911094
}
10921095
closed?.let { closed -> throw closed.sendException }
10931096

1097+
return joinFromSuspend(src, delegateClose)
1098+
}
1099+
1100+
private suspend fun joinFromSuspend(src: ByteBufferChannel, delegateClose: Boolean) {
10941101
val joined = src.setupDelegateTo(this, delegateClose)
10951102
copyDirect(src, Long.MAX_VALUE, joined)
10961103

@@ -1316,7 +1323,7 @@ internal class ByteBufferChannel(
13161323

13171324
if (rem == 0) return
13181325

1319-
writeFullySuspend(src, off, rem)
1326+
return writeFullySuspend(src, off, rem)
13201327
}
13211328

13221329
private tailrec suspend fun writeFullySuspend(src: ByteArray, offset: Int, length: Int) {
@@ -1456,9 +1463,9 @@ internal class ByteBufferChannel(
14561463
* Never invokes [visitor] with empty buffer unless [last] = true. Invokes visitor with last = true at most once
14571464
* even if there are remaining bytes and visitor returned true.
14581465
*/
1459-
override suspend fun consumeEachBufferRange(visitor: (buffer: ByteBuffer, last: Boolean) -> Boolean) {
1466+
final override suspend fun consumeEachBufferRange(visitor: (buffer: ByteBuffer, last: Boolean) -> Boolean) {
14601467
if (consumeEachBufferRangeFast(false, visitor)) return
1461-
consumeEachBufferRangeSuspend(visitor)
1468+
return consumeEachBufferRangeSuspend(visitor)
14621469
}
14631470

14641471
override fun <R> lookAhead(visitor: LookAheadSession.() -> R): R {
@@ -1511,7 +1518,16 @@ internal class ByteBufferChannel(
15111518
}
15121519
}
15131520

1514-
suspend override fun awaitAtLeast(n: Int) {
1521+
final suspend override fun awaitAtLeast(n: Int) {
1522+
if (state.capacity.availableForRead >= n) {
1523+
if (state.idle) setupStateForRead()
1524+
return
1525+
}
1526+
1527+
return awaitAtLeastSuspend(n)
1528+
}
1529+
1530+
private suspend fun awaitAtLeastSuspend(n: Int) {
15151531
if (readSuspend(n) && state.idle) {
15161532
setupStateForRead()
15171533
}
@@ -1562,12 +1578,12 @@ internal class ByteBufferChannel(
15621578
return rc
15631579
}
15641580

1565-
private suspend fun consumeEachBufferRangeSuspend(visitor: (buffer: ByteBuffer, last: Boolean) -> Boolean): Boolean {
1581+
private suspend fun consumeEachBufferRangeSuspend(visitor: (buffer: ByteBuffer, last: Boolean) -> Boolean) {
15661582
var last = false
15671583

15681584
do {
1569-
if (consumeEachBufferRangeFast(last, visitor)) return true
1570-
if (last) return false
1585+
if (consumeEachBufferRangeFast(last, visitor)) return
1586+
if (last) return
15711587
if (!readSuspend(1)) {
15721588
last = true
15731589
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ suspend fun ByteReadChannel.joinTo(dst: ByteWriteChannel, closeOnEnd: Boolean) {
144144
return dst.joinFrom(this, closeOnEnd)
145145
}
146146

147-
return joinToImpl(dst, closeOnEnd)
147+
return joinToImplSuspend(dst, closeOnEnd)
148148
}
149149

150-
private suspend fun ByteReadChannel.joinToImpl(dst: ByteWriteChannel, close: Boolean) {
150+
private suspend fun ByteReadChannel.joinToImplSuspend(dst: ByteWriteChannel, close: Boolean) {
151151
copyToImpl(dst, Long.MAX_VALUE)
152152
if (close) {
153153
dst.close()

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,55 +142,58 @@ public interface ByteWriteChannel {
142142
}
143143

144144
suspend fun ByteWriteChannel.writeShort(s: Int) {
145-
writeShort((s and 0xffff).toShort())
145+
return writeShort((s and 0xffff).toShort())
146146
}
147147

148148
suspend fun ByteWriteChannel.writeByte(b: Int) {
149-
writeByte((b and 0xff).toByte())
149+
return writeByte((b and 0xff).toByte())
150150
}
151151

152152
suspend fun ByteWriteChannel.writeInt(i: Long) {
153-
writeInt(i.toInt())
153+
return writeInt(i.toInt())
154154
}
155155

156156
suspend fun ByteWriteChannel.writeStringUtf8(s: CharSequence) {
157157
val packet = buildPacket {
158158
writeStringUtf8(s)
159159
}
160-
writePacket(packet)
160+
161+
return writePacket(packet)
161162
}
162163

163164
suspend fun ByteWriteChannel.writeStringUtf8(s: CharBuffer) {
164165
val packet = buildPacket {
165166
writeStringUtf8(s)
166167
}
167-
writePacket(packet)
168+
169+
return writePacket(packet)
168170
}
169171

170172
suspend fun ByteWriteChannel.writeStringUtf8(s: String) {
171173
val packet = buildPacket {
172174
writeStringUtf8(s)
173175
}
174-
writePacket(packet)
176+
177+
return writePacket(packet)
175178
}
176179

177180
suspend fun ByteWriteChannel.writeBoolean(b: Boolean) {
178-
writeByte(if (b) 1 else 0)
181+
return writeByte(if (b) 1 else 0)
179182
}
180183

181184
/**
182185
* Writes UTF16 character
183186
*/
184187
suspend fun ByteWriteChannel.writeChar(ch: Char) {
185-
writeShort(ch.toInt())
188+
return writeShort(ch.toInt())
186189
}
187190

188191
inline suspend fun ByteWriteChannel.writePacket(headerSizeHint: Int = 0, builder: ByteWritePacket.() -> Unit) {
189-
writePacket(buildPacket(headerSizeHint, builder))
192+
return writePacket(buildPacket(headerSizeHint, builder))
190193
}
191194

192195
suspend fun ByteWriteChannel.writePacketSuspend(builder: suspend ByteWritePacket.() -> Unit) {
193-
writePacket(buildPacket { builder() })
196+
return writePacket(buildPacket { builder() })
194197
}
195198

196199
/**

0 commit comments

Comments
 (0)