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

Improve ByteBufPrimitiveCodec readBytes #1617

Merged
merged 6 commits into from
Aug 21, 2023
Merged

Conversation

chibenwa
Copy link
Contributor

@chibenwa chibenwa commented Nov 8, 2022

-> Remove a needless call to ByteBuff::hasArray

-> Removes needless slicing

This method is always used on sliced ByteBuf (as the first 4 bytes old the
overall size) so the payload will never match the underlying buffer's array
thus defeats the purpose of the optimization and end up costing CPU cycles.

On a typical application this represent ~0.4% of the CPU
Directly read the source ByteBuf while checking for
content length. Reader index are similarily moved.

On a typical application this takes ~0.88% of CPU
// Move the readerIndex just so we consistently consume the input
buffer.readerIndex(buffer.writerIndex());
return buffer.array();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@chibenwa I'm not sure I understand. Is there a reason why we don't want to use the underlying byte array directly if we can get access to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the conditions for reusing the array are never fullfilled.

The buffer layout is 4 bytes size, byyyyyyyyyyyyytes so we would expect the underlying array to match (best case) this layout.

However reading the size means that the leading 4 bytes are read first, and then the payload never matches the underlying array.

Checking if this optimization is doable is impactful enough to be removing it.

Clearer?

Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies @chibenwa, I've been out for the holiday here... just getting back to this.

Yeah, your explanation makes sense, thanks for the additional context. I'm reluctant to give up the re-use of the backing array, though, so I put together an alternate implementation which preserves that feature. What do you think of something in that vein?

Copy link
Contributor

Choose a reason for hiding this comment

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

The alternate proposal never went anywhere so we're moving forward with the original proposal from @chibenwa

@chibenwa
Copy link
Contributor Author

chibenwa commented Dec 2, 2022

I added some tests to better specify the expected behaviour of readBytes and removed the needless test.

@absurdfarce absurdfarce merged commit 4d6e2e7 into apache:4.x Aug 21, 2023
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.

2 participants