Skip to content
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

Add implementation for computing the required bytes to encode a message #2239

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

GeorgePap-719
Copy link
Contributor

@GeorgePap-719 GeorgePap-719 commented Mar 17, 2023

Summary

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

Goal:

After feedback on #2082, the most critical problem we had to solve was how to make an actual streaming protobuf without reading all to byte array first. This API solves this problem, as we can calculate the required bytes to encode a message.

By extension, this pr acts as a proof of concept, and also as a base to enable discussions for #2075.

Implementation notes

API

The API which is introduced:

public fun <T : Any> ProtoBuf.getOrComputeSerializedSize(serializer: SerializationStrategy<T>, value: T): Int

Even though this API has some usage for consumers, for example see protobuf.js#162, it can also be considered as an internal API to support only encoding/decoding delimited-messages.

Tests

The main tests are written in jvm platform, because java's serializedSize API result is used to test against getOrComputeSerializedSize() result.

Multiplatform

Native and Js implementations do not use concurrent-safe structures.

Even though the core implementation is written in common module, it is tested only in jvm. In case we decide to keep this API in common then tests should be moved in common or re-think testing strategy.

Additionally, since the goal of this API is to be used as a cornerstone for a jvm only feature, it is debatable if the API really belongs to common module.

Inspiration

The inspiration came from how the official library (google's) solved this issue, in particular java's implementation. That being said, it is to be noted that many byte operations were "rented" from google's impl (some with minor modifications, some not). This is done to avoid re-inventing the wheel, and because i'm not so experienced to create them by myself.

@GeorgePap-719
Copy link
Contributor Author

@qwwdfsad (sorry for tag but since you were the reviewer of 2082, i figured to save some time) Take a look if you have time.

@GeorgePap-719 GeorgePap-719 force-pushed the protobuf-proof-of-concept-serialized-size branch 2 times, most recently from 9c6ad21 to 3fa5eb7 Compare April 4, 2023 15:00
GeorgePap-719 added a commit to GeorgePap-719/kotlinx.serialization that referenced this pull request Apr 4, 2023
…ge (Kotlin#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 Kotlin#2075.

Signed-off-by: George Papadopoulos <[email protected]>
@GeorgePap-719 GeorgePap-719 force-pushed the protobuf-proof-of-concept-serialized-size branch from 3fa5eb7 to 9371b52 Compare April 4, 2023 15:04
@GeorgePap-719 GeorgePap-719 changed the title Proof of concept: Add implementation for computing the required bytes to encode a message Add implementation for computing the required bytes to encode a message Apr 4, 2023
@GeorgePap-719 GeorgePap-719 marked this pull request as ready for review April 4, 2023 15:14
…ge (Kotlin#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 Kotlin#2075.

Signed-off-by: George Papadopoulos <[email protected]>
@GeorgePap-719 GeorgePap-719 force-pushed the protobuf-proof-of-concept-serialized-size branch from 9371b52 to 47bb556 Compare April 7, 2023 07:56
@ShreckYe
Copy link
Contributor

ShreckYe commented Mar 6, 2024

A vote for this.

@GeorgePap-719
Copy link
Contributor Author

Hey @sandwwraith, I think it's time to take a look on this again. I think there are enough use-cases to go forward with this, as mentioned in #2075.

Let me know your opinion on this.

@sandwwraith
Copy link
Member

@GeorgePap-719 I will, but my hands are a bit full now, sorry :( Maybe it is a good idea to also consult with kotlinx.rpc team of whether this would be useful to them while supporting gRPC.

@GeorgePap-719
Copy link
Contributor Author

No worries man! Ok, I will reach out to kotlinx.rpc team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants