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

Implement the new Buffer API in terms of the ByteBuf API #12099

Merged
merged 10 commits into from
Feb 17, 2022

Conversation

chrisvest
Copy link
Contributor

Motivation:
This will be useful for bridging the gap between the two APIs.
Integrators will be able to use this as they migrate their code to the new API bit-by-bit.

Modification:
Add a complete implementation of the Buffer interface, that is written in terms of the ByteBuf API.
The implementation passes all of the Buffer tests.

Result:
It is now possible to adapt a ByteBuf into a Buffer.
This capability joins the ByteBufAdaptor as a sibling.
The ByteBufAdaptor converts a Buffer into a ByteBuf, while the new ByteBufBuffer works in the opposite direction, and converts a ByteBuf into a Buffer.


@Override
public int writableBytes() {
return delegate.capacity() - delegate.writerIndex();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return delegate.capacity() - delegate.writerIndex();
return delegate.writableBytes();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, that breaks for read-only buffers. I did try it. 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

in what way ? Because it always return 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. In the Buffer interface, it's expected to just be based on the write offset and the capacity.

Copy link
Member

Choose a reason for hiding this comment

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

I am a bit on the fence here... Generally speaking it seems odd that a ReadOnly buffer would return anything different then 0 for writableBytes.

@chrisvest
Copy link
Contributor Author

@normanmaurer Addressed your comments. Please take a look.

@chrisvest
Copy link
Contributor Author

Looks like a test is running out of memory somewhere.

Motivation:
This will be useful for bridging the gap between the two APIs.
Integrators will be able to use this as they migrate their code to the new API bit-by-bit.

Modification:
Add a complete implementation of the Buffer interface, that is written in terms of the ByteBuf API.
The implementation passes all of the Buffer tests.

Result:
It is now possible to adapt a ByteBuf into a Buffer.
This capability joins the ByteBufAdaptor as a sibling.
The ByteBufAdaptor converts a Buffer into a ByteBuf, while the new ByteBufBuffer works in the opposite direction, and converts a ByteBuf into a Buffer.
@@ -133,7 +133,7 @@ public void testWithoutUseCacheForAllThreads() {
/*nHeapArena=*/ 1,
/*nDirectArena=*/ 1,
/*pageSize=*/8192,
/*maxOrder=*/ 11,
/*maxOrder=*/ 9,
Copy link
Member

Choose a reason for hiding this comment

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

to reduce memory overhead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was starting to get OOMEs from this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like I have to change the default max order as well. Not clear why this is suddenly such a big problem.

Motivation:
A max order of 11 implies a chunk size of 16 MiB, which can be a large investment in memory, and occasionally causes the tests to run out of memory.
It is also a large investment on machines with many cores, since the number of arenas are correlated with the number of CPU cores, and each arena that is used will likely allocate a chunk.

Modification:
Reduce the default max order such that the default chunk size is reduced from 16 MiB to 4 MiB.
This potentially increases the number of huge allocations, but I think it's a fair trade-off for Netty's use cases.

Result:
If we don't need a lot of memory, we'll now use less memory.
It is troublesome to initialise objects at image build time, if they have references to an AbstractReferenceCountedByteBuf, for instance.
This way we make sure to always deallocate the target copy, if the copying fails.
@chrisvest
Copy link
Contributor Author

@normanmaurer I think this is good now, unless you insist on preserving the ByteBuf behaviour of read-only buffers.

@chrisvest
Copy link
Contributor Author

Let's tackle what Buffer.writableBytes() means for read-only buffers in a different PR. In this one we just implement it to match the existing spec of Buffer.

@chrisvest chrisvest merged commit 42bd497 into netty:main Feb 17, 2022
@chrisvest chrisvest deleted the 5x-bytebuf-buffer branch February 17, 2022 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants