-
Notifications
You must be signed in to change notification settings - Fork 658
Speedup ProtoWriteType.from #2879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.benchmarks.protobuf | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.encodeToByteArray | ||
import kotlinx.serialization.protobuf.ProtoBuf | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
@Warmup(iterations = 5, time = 1) | ||
@Measurement(iterations = 5, time = 1) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Benchmark) | ||
@Fork(1) | ||
open class ProtoMapBenchmark { | ||
|
||
@Serializable | ||
class Holder(val map: Map<String, Int>) | ||
|
||
private val value = Holder((0..128).associateBy { it.toString() }) | ||
private val bytes = ProtoBuf.encodeToByteArray(value) | ||
|
||
@Benchmark | ||
fun toBytes() = ProtoBuf.encodeToByteArray(Holder.serializer(), value) | ||
|
||
@Benchmark | ||
fun fromBytes() = ProtoBuf.decodeFromByteArray(Holder.serializer(), bytes) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,15 @@ internal enum class ProtoWireType(val typeId: Int) { | |
; | ||
|
||
companion object { | ||
private val entryArray = Array(8) { typeId -> | ||
ProtoWireType.entries.find { it.typeId == typeId } ?: INVALID | ||
} | ||
|
||
fun from(typeId: Int): ProtoWireType { | ||
return ProtoWireType.entries.find { it.typeId == typeId } ?: INVALID | ||
if (typeId < 0 || typeId >= entryArray.size) { | ||
return INVALID | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From an optimization perspective it may be valid to not check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The array bounds check will "give" us Anyway, the implementation was implicitly based on the idea that the only call site extracts 3 least-significant bits from an int before passing it down this methods, so when this method is inlined the compiler can get rid of the |
||
return entryArray[typeId] | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.