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

Avoid un-necessary byte copies when decoding a Vert.x Buffer to a protobuf object and perform more optimal allocation #136

Merged
merged 1 commit into from
Mar 5, 2025

Conversation

vietj
Copy link
Member

@vietj vietj commented Mar 5, 2025

Motivation:

The current implementation of the bridge uses a plain ByteArrayInputStream and then let protobuf parse the message. It turns out there is a KnownLength interface used by the parser to know the actual buffer size to be decoded and optimize the decoding process.

In addition we can use Netty ByteBufInputStream to avoid using ByteArrayInputStream and un-necessary copies.

Changes:

Swap usage of ByteArrayInputStream with a class that extends ByteBufInputStream and implement KnownLength.

…tobuf object and perform more optimal allocation.

Motivation:

The current implementation of the bridge uses a plain ByteArrayInputStream and then let protobuf parse the message. It turns out there is a KnownLength interface used by the parser to know the actual buffer size to be decoded and optimize the decoding process.

In addition we can use Netty ByteBufInputStream to avoid using ByteArrayInputStream and un-necessary copies.

Changes:

Swap usage of ByteArrayInputStream with a class that extends ByteBufInputStream and implement KnownLength.
@vietj vietj added this to the 4.5.14 milestone Mar 5, 2025
@vietj vietj added the enhancement New feature or request label Mar 5, 2025
@vietj
Copy link
Member Author

vietj commented Mar 5, 2025

@franz1981 ping

@@ -29,15 +33,31 @@ public BridgeMessageDecoder(MethodDescriptor.Marshaller<T> marshaller, Decompres
this.decompressor = decompressor;
}

private static class KnownLengthStream extends ByteBufInputStream implements KnownLength {
public KnownLengthStream(Buffer buffer) {
super(((BufferImpl)buffer).getByteBuf(), buffer.length());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the buffer is doesn't have any weird requirement re release ref cnt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does create a slice but closing the ByteBufInputStream releases it

@alesj
Copy link

alesj commented Mar 5, 2025

((BufferImpl)buffer).getByteBuf()

Casting this b/c of the method going away in Buffer interface in Vert.x5 ?

@vietj
Copy link
Member Author

vietj commented Mar 5, 2025

((BufferImpl)buffer).getByteBuf()

Casting this b/c of the method going away in Buffer interface in Vert.x5 ?

this method is available on BufferInternal

@vietj vietj merged commit b72f6fb into 4.x Mar 5, 2025
4 checks passed
@vietj vietj deleted the optimize-bridge-message-decoder branch March 5, 2025 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants