-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation for computing the required bytes to encode a message
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
1 parent
694e2f7
commit 9c6ad21
Showing
8 changed files
with
1,200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
643 changes: 643 additions & 0 deletions
643
formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/ProtoBufSerializedSize.kt
Large diffs are not rendered by default.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
formats/protobuf/jsMain/src/kotlinx/serialization/protobuf/SerializedSizeCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
formats/protobuf/jvmMain/src/kotlinx/serialization/protobuf/SerializedSizeCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.