Skip to content

Commit

Permalink
Replace Okio with kotlinx-io and remove unneeded code
Browse files Browse the repository at this point in the history
The "JSON serialization" performance seems to be slightly less.
  • Loading branch information
ShreckYe committed Oct 29, 2024
1 parent 7edd04f commit f490bff
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 72 deletions.
1 change: 1 addition & 0 deletions frameworks/Kotlin/vertx-web-kotlinx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The tests were run with:
* [Vert.x Reactive PostgreSQL Client](https://vertx.io/docs/vertx-pg-client/java/)
* [kotlinx.coroutines](https://github.com/Kotlin/kotlinx.coroutines)
* [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization)
* [kotlinx-io](https://github.com/Kotlin/kotlinx-io)
* [kotlinx.html](https://github.com/Kotlin/kotlinx.html)

## Test URLs
Expand Down
5 changes: 2 additions & 3 deletions frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion")
//implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-io:$kotlinxSerializationVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-okio:$kotlinxSerializationVersion")
implementation("com.squareup.okio:okio:3.9.1")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-io:$kotlinxSerializationVersion")
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.5.4")

implementation("org.jetbrains.kotlinx:kotlinx-html:0.11.0")
//implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0") // the latest version is 0.6.1
Expand Down
49 changes: 49 additions & 0 deletions frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import io.vertx.core.streams.WriteStream
import io.vertx.kotlin.coroutines.coAwait
import kotlinx.coroutines.runBlocking
import kotlinx.io.RawSink
import kotlinx.io.readByteArray
import io.vertx.core.buffer.Buffer as VertxBuffer
import kotlinx.io.Buffer as KotlinxIoBuffer

@Suppress("NOTHING_TO_INLINE")
private inline fun Long.toIntOrThrow(): Int {
require(this in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong())
return toInt()
}

@JvmInline
value class VertxBufferWriteStreamSink(val writeStream: WriteStream<VertxBuffer>) : RawSink {
override fun write(source: KotlinxIoBuffer, byteCount: Long) {
runBlocking {
// `source` is temporarily converted to a byte array because wrapping it as a Vert.x `Buffer` is too complicated to implement.
writeStream.write(VertxBuffer.buffer(source.readByteArray(byteCount.toIntOrThrow()))).coAwait()
}
}

override fun flush() {}

override fun close() {
writeStream.end()
}
}

// not used currently
fun WriteStream<VertxBuffer>.toRawSink(): RawSink =
VertxBufferWriteStreamSink(this)


@JvmInline
value class VertxBufferSink(val vertxBuffer: VertxBuffer) : RawSink {
override fun write(source: KotlinxIoBuffer, byteCount: Long) {
// same problem as above
vertxBuffer.appendBytes(source.readByteArray(byteCount.toIntOrThrow()))
}

override fun flush() {}

override fun close() {}
}

fun VertxBuffer.toRawSink(): RawSink =
VertxBufferSink(this)
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.html.*
import kotlinx.html.stream.appendHTML
import kotlinx.io.buffered
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.okio.encodeToBufferedSink
import okio.buffer
import kotlinx.serialization.json.io.encodeToSink
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

Expand Down Expand Up @@ -133,17 +133,17 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle() {
/*
// approach 2
// java.lang.IllegalStateException: You must set the Content-Length header to be the total size of the message body BEFORE sending any data if you are not using HTTP chunked encoding.
toSink().buffer().use { bufferedSink ->
toRawSink().buffered().use { bufferedSink ->
@OptIn(ExperimentalSerializationApi::class)
Json.encodeToBufferedSink(serializer, requestHandler(it), bufferedSink)
Json.encodeToSink(serializer, requestHandler(it), bufferedSink)
}
*/

// approach 3
end(Buffer.buffer().apply {
toSink().buffer().use { bufferedSink ->
toRawSink().buffered().use { bufferedSink ->
@OptIn(ExperimentalSerializationApi::class)
Json.encodeToBufferedSink(serializer, requestHandler(it), bufferedSink)
Json.encodeToSink(serializer, requestHandler(it), bufferedSink)
}
})
}
Expand Down
63 changes: 0 additions & 63 deletions frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/Okio.kt

This file was deleted.

0 comments on commit f490bff

Please sign in to comment.