From 69d8a5401bdf9de034bf3f21dd4571f63a9ec9b6 Mon Sep 17 00:00:00 2001 From: Shreck Ye Date: Thu, 31 Oct 2024 13:32:30 +0800 Subject: [PATCH] Use `Json.encodeToBuffer` from https://github.com/huanshankeji/kotlinx.serialization/commit/0a4c57a22ba4df962f36befcb08ab080653a9bb3 The "JSON serialization" performance result is on par with that of using `Json.encodeToSink` (or slightly better, at most by 2%). Therefore, this change is not really necessary. --- .../Kotlin/vertx-web-kotlinx/.gitignore | 1 + .../Kotlin/vertx-web-kotlinx/build.gradle.kts | 4 +- .../vertx-web-kotlinx/copy_maven_local.sh | 2 + .../src/main/kotlin/KotlinxIo.kt | 47 ------------------- .../src/main/kotlin/MainVerticle.kt | 11 ++--- .../vertx-web-kotlinx-postgresql.dockerfile | 2 + .../vertx-web-kotlinx.dockerfile | 2 + 7 files changed, 14 insertions(+), 55 deletions(-) create mode 100644 frameworks/Kotlin/vertx-web-kotlinx/.gitignore create mode 100755 frameworks/Kotlin/vertx-web-kotlinx/copy_maven_local.sh delete mode 100644 frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt diff --git a/frameworks/Kotlin/vertx-web-kotlinx/.gitignore b/frameworks/Kotlin/vertx-web-kotlinx/.gitignore new file mode 100644 index 00000000000..eb713db1967 --- /dev/null +++ b/frameworks/Kotlin/vertx-web-kotlinx/.gitignore @@ -0,0 +1 @@ +.m2 diff --git a/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts b/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts index 2e0035ab3e7..a9eb1c72af0 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts +++ b/frameworks/Kotlin/vertx-web-kotlinx/build.gradle.kts @@ -10,11 +10,12 @@ plugins { } repositories { + mavenLocal() // TODO mavenCentral() } val vertxVersion = "4.5.10" -val kotlinxSerializationVersion = "1.7.3" +val kotlinxSerializationVersion = "1.7.4-vertx-buffer-SNAPSHOT" // TODO dependencies { implementation(platform("io.vertx:vertx-stack-depchain:$vertxVersion")) implementation("io.vertx:vertx-web") @@ -30,6 +31,7 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinxSerializationVersion") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-io:$kotlinxSerializationVersion") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-vertx:$kotlinxSerializationVersion") implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.5.4") implementation("org.jetbrains.kotlinx:kotlinx-html:0.11.0") diff --git a/frameworks/Kotlin/vertx-web-kotlinx/copy_maven_local.sh b/frameworks/Kotlin/vertx-web-kotlinx/copy_maven_local.sh new file mode 100755 index 00000000000..e3e3282e087 --- /dev/null +++ b/frameworks/Kotlin/vertx-web-kotlinx/copy_maven_local.sh @@ -0,0 +1,2 @@ +mkdir -p .m2/repository/org/jetbrains/kotlinx/ +cp -r ~/.m2/repository/org/jetbrains/kotlinx/kotlinx-serialization-* .m2/repository/org/jetbrains/kotlinx/ diff --git a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt b/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt deleted file mode 100644 index 6427b91e85e..00000000000 --- a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/KotlinxIo.kt +++ /dev/null @@ -1,47 +0,0 @@ -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 VertxBufferWriteStreamRawSink(val writeStream: WriteStream) : RawSink { - override fun write(source: KotlinxIoBuffer, byteCount: Long) { - runBlocking { - writeStream.write(VertxBuffer.buffer(source.readByteArray(byteCount.toIntOrThrow()))).coAwait() - } - } - - override fun flush() {} - - override fun close() { - writeStream.end() - } -} - -// not used currently -fun WriteStream.toRawSink(): RawSink = - VertxBufferWriteStreamRawSink(this) - - -@JvmInline -value class VertxBufferRawSink(val vertxBuffer: VertxBuffer) : RawSink { - override fun write(source: KotlinxIoBuffer, byteCount: Long) { - vertxBuffer.appendBytes(source.readByteArray(byteCount.toIntOrThrow())) - } - - override fun flush() {} - - override fun close() {} -} - -fun VertxBuffer.toRawSink(): RawSink = - VertxBufferRawSink(this) diff --git a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt b/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt index 2bd701fa083..77d5bee2e61 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt +++ b/frameworks/Kotlin/vertx-web-kotlinx/src/main/kotlin/MainVerticle.kt @@ -20,12 +20,11 @@ import io.vertx.sqlclient.Tuple import kotlinx.coroutines.Dispatchers 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.io.encodeToSink +import kotlinx.serialization.json.vertx.encodeToBuffer import java.time.ZonedDateTime import java.time.format.DateTimeFormatter @@ -134,11 +133,9 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup */ // approach 3 - end(Buffer.buffer().apply { - toRawSink().buffered().use { bufferedSink -> - @OptIn(ExperimentalSerializationApi::class) - Json.encodeToSink(serializer, requestHandler(it), bufferedSink) - } + end(Buffer.buffer().also { buffer -> + @OptIn(ExperimentalSerializationApi::class) + Json.encodeToBuffer(serializer, requestHandler(it), buffer) }) } } diff --git a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile index 17b43ebcb5c..1e704fc27d0 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile +++ b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx-postgresql.dockerfile @@ -1,5 +1,7 @@ FROM gradle:8.10.2-jdk21 +# Publish the dependencies to Maven local and run "copy_maven_local.sh" first. +COPY .m2/repository/org/jetbrains/kotlinx /root/.m2/repository/org/jetbrains/kotlinx WORKDIR /vertx-web-kotlinx COPY build.gradle.kts build.gradle.kts COPY settings.gradle.kts settings.gradle.kts diff --git a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile index 4bdc993707d..07fff6b9a57 100644 --- a/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile +++ b/frameworks/Kotlin/vertx-web-kotlinx/vertx-web-kotlinx.dockerfile @@ -1,5 +1,7 @@ FROM gradle:8.10.2-jdk21 +# Publish the dependencies to Maven local and run "copy_maven_local.sh" first. +COPY .m2/repository/org/jetbrains/kotlinx /root/.m2/repository/org/jetbrains/kotlinx WORKDIR /vertx-web-kotlinx COPY build.gradle.kts build.gradle.kts COPY settings.gradle.kts settings.gradle.kts