From a772a08a2f279e044756c5819255926e78722572 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 28 Mar 2024 10:15:23 +0700 Subject: [PATCH] Use inlining for read/writeVar* --- gradle.properties | 2 +- .../ru/dbotthepony/kommons/io/InputStreamUtils.kt | 12 ++++++------ .../ru/dbotthepony/kommons/io/OutputStreamUtils.kt | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/gradle.properties b/gradle.properties index 3d73e82..3e241b7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.10.2 +projectVersion=2.10.3 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/InputStreamUtils.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/InputStreamUtils.kt index 0436053..8558564 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/InputStreamUtils.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/io/InputStreamUtils.kt @@ -79,9 +79,9 @@ fun InputStream.readDouble() = Double.fromBits(readLong()) data class VarIntReadResult(val value: Int, val cells: Int) data class VarLongReadResult(val value: Long, val cells: Int) -private fun readVarLongInfo(supplier: IntSupplier): VarLongReadResult { +private inline fun readVarLongInfo(supplier: () -> Int): VarLongReadResult { var result = 0L - var read = supplier.asInt + var read = supplier() var i = 1 if (read == -1) @@ -93,7 +93,7 @@ private fun readVarLongInfo(supplier: IntSupplier): VarLongReadResult { if (read and 0x80 == 0) break - read = supplier.asInt + read = supplier() if (read == -1) throw EOFException("Reached end of stream before finished reading variable length integer (read totally $i bytes)") @@ -104,16 +104,16 @@ private fun readVarLongInfo(supplier: IntSupplier): VarLongReadResult { return VarLongReadResult(result, i) } -private fun readVarIntInfo(supplier: IntSupplier): VarIntReadResult { +private inline fun readVarIntInfo(supplier: () -> Int): VarIntReadResult { val read = readVarLongInfo(supplier) return VarIntReadResult(read.value.toInt(), read.cells) } -private fun readVarInt(supplier: IntSupplier): Int { +private inline fun readVarInt(supplier: () -> Int): Int { return readVarIntInfo(supplier).value } -private fun readVarLong(supplier: IntSupplier): Long { +private inline fun readVarLong(supplier: () -> Int): Long { return readVarLongInfo(supplier).value } diff --git a/src/main/kotlin/ru/dbotthepony/kommons/io/OutputStreamUtils.kt b/src/main/kotlin/ru/dbotthepony/kommons/io/OutputStreamUtils.kt index 9948cd5..d20b780 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/io/OutputStreamUtils.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/io/OutputStreamUtils.kt @@ -81,7 +81,7 @@ fun OutputStream.writeLong(value: Long) { fun OutputStream.writeFloat(value: Float) = writeInt(value.toBits()) fun OutputStream.writeDouble(value: Double) = writeLong(value.toBits()) -private fun writeVarLong(write: IntConsumer, value: Long): Int { +private inline fun writeVarLong(write: (Int) -> Unit, value: Long): Int { var length = 9 while (length > 0) { @@ -95,15 +95,15 @@ private fun writeVarLong(write: IntConsumer, value: Long): Int { } for (octet in length downTo 1) { - write.accept(((value ushr (octet * 7)) and 0x7FL).toInt() or 0x80) + write(((value ushr (octet * 7)) and 0x7FL).toInt() or 0x80) } - write.accept((value and 0x7FL).toInt()) + write((value and 0x7FL).toInt()) return length + 1 } -private fun writeVarInt(write: IntConsumer, value: Int): Int { +private inline fun writeVarInt(write: (Int) -> Unit, value: Int): Int { return writeVarLong(write, value.toLong()) }