Use inlining for read/writeVar*

This commit is contained in:
DBotThePony 2024-03-28 10:15:23 +07:00
parent f64dcacd63
commit a772a08a2f
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 11 additions and 11 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons projectGroup=ru.dbotthepony.kommons
projectVersion=2.10.2 projectVersion=2.10.3
guavaDepVersion=33.0.0 guavaDepVersion=33.0.0
gsonDepVersion=2.8.9 gsonDepVersion=2.8.9

View File

@ -79,9 +79,9 @@ fun InputStream.readDouble() = Double.fromBits(readLong())
data class VarIntReadResult(val value: Int, val cells: Int) data class VarIntReadResult(val value: Int, val cells: Int)
data class VarLongReadResult(val value: Long, 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 result = 0L
var read = supplier.asInt var read = supplier()
var i = 1 var i = 1
if (read == -1) if (read == -1)
@ -93,7 +93,7 @@ private fun readVarLongInfo(supplier: IntSupplier): VarLongReadResult {
if (read and 0x80 == 0) if (read and 0x80 == 0)
break break
read = supplier.asInt read = supplier()
if (read == -1) if (read == -1)
throw EOFException("Reached end of stream before finished reading variable length integer (read totally $i bytes)") 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) return VarLongReadResult(result, i)
} }
private fun readVarIntInfo(supplier: IntSupplier): VarIntReadResult { private inline fun readVarIntInfo(supplier: () -> Int): VarIntReadResult {
val read = readVarLongInfo(supplier) val read = readVarLongInfo(supplier)
return VarIntReadResult(read.value.toInt(), read.cells) return VarIntReadResult(read.value.toInt(), read.cells)
} }
private fun readVarInt(supplier: IntSupplier): Int { private inline fun readVarInt(supplier: () -> Int): Int {
return readVarIntInfo(supplier).value return readVarIntInfo(supplier).value
} }
private fun readVarLong(supplier: IntSupplier): Long { private inline fun readVarLong(supplier: () -> Int): Long {
return readVarLongInfo(supplier).value return readVarLongInfo(supplier).value
} }

View File

@ -81,7 +81,7 @@ fun OutputStream.writeLong(value: Long) {
fun OutputStream.writeFloat(value: Float) = writeInt(value.toBits()) fun OutputStream.writeFloat(value: Float) = writeInt(value.toBits())
fun OutputStream.writeDouble(value: Double) = writeLong(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 var length = 9
while (length > 0) { while (length > 0) {
@ -95,15 +95,15 @@ private fun writeVarLong(write: IntConsumer, value: Long): Int {
} }
for (octet in length downTo 1) { 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 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()) return writeVarLong(write, value.toLong())
} }