Skip to content

Commit

Permalink
Add implementation for computing the required bytes to encode a messa…
Browse files Browse the repository at this point in the history
…ge (#2239)

This commit introduces a new API which computes the required bytes to
encode a message without actually serializing a message. By extension,
this API is meant to be used as a cornerstone for implementing #2075.

Signed-off-by: George Papadopoulos <[email protected]>
  • Loading branch information
GeorgePap-719 committed Apr 7, 2023
1 parent 694e2f7 commit 47bb556
Show file tree
Hide file tree
Showing 8 changed files with 1,205 additions and 0 deletions.
4 changes: 4 additions & 0 deletions formats/protobuf/api/kotlinx-serialization-protobuf.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public final class kotlinx/serialization/protobuf/ProtoBufKt {
public static synthetic fun ProtoBuf$default (Lkotlinx/serialization/protobuf/ProtoBuf;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlinx/serialization/protobuf/ProtoBuf;
}

public final class kotlinx/serialization/protobuf/ProtoBufSerializedSizeCalculatorKt {
public static final fun getOrComputeSerializedSize (Lkotlinx/serialization/protobuf/ProtoBuf;Lkotlinx/serialization/SerializationStrategy;Ljava/lang/Object;)I
}

public final class kotlinx/serialization/protobuf/ProtoIntegerType : java/lang/Enum {
public static final field DEFAULT Lkotlinx/serialization/protobuf/ProtoIntegerType;
public static final field FIXED Lkotlinx/serialization/protobuf/ProtoIntegerType;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kotlinx.serialization.protobuf

import kotlinx.serialization.descriptors.SerialDescriptor

internal actual fun createSerializedSizeCache(): SerializedSizeCache = JsHashMap()

private class JsHashMap : SerializedSizeCache {
private val cache = mutableMapOf<SerialDescriptor, SerializedData>()

override fun get(descriptor: SerialDescriptor, key: SerializedSizeCacheKey): Int? = cache[descriptor]?.get(key)

override fun set(descriptor: SerialDescriptor, key: SerializedSizeCacheKey, serializedSize: Int) {
cache[descriptor] = mapOf(key to serializedSize)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kotlinx.serialization.protobuf

import kotlinx.serialization.descriptors.SerialDescriptor
import java.util.concurrent.ConcurrentHashMap

internal actual fun createSerializedSizeCache(): SerializedSizeCache {
return ConcurrentHashMapSerializedCache()
}

private class ConcurrentHashMapSerializedCache : SerializedSizeCache {
private val cache = ConcurrentHashMap<SerialDescriptor, SerializedData>()

override fun get(descriptor: SerialDescriptor, key: SerializedSizeCacheKey): Int? = cache[descriptor]?.get(key)

override fun set(descriptor: SerialDescriptor, key: SerializedSizeCacheKey, serializedSize: Int) {
cache[descriptor] = mapOf(key to serializedSize)
}
}
Loading

0 comments on commit 47bb556

Please sign in to comment.