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

Buffer reusal in the BufferingResponseListener #12687

Open
wendigo opened this issue Jan 9, 2025 · 5 comments · May be fixed by #12782
Open

Buffer reusal in the BufferingResponseListener #12687

wendigo opened this issue Jan 9, 2025 · 5 comments · May be fixed by #12782
Assignees

Comments

@wendigo
Copy link

wendigo commented Jan 9, 2025

Jetty version(s)
12.x

Enhancement Description

Is it possible for the BufferingResponseListener to accept ByteBufferPool to reuse buffers instead of allocating them per request?

@sbordet
Copy link
Contributor

sbordet commented Jan 9, 2025

@wendigo definitely a good to have.

@sbordet
Copy link
Contributor

sbordet commented Jan 9, 2025

@wendigo this might be trickier than expected.

The default max size for BufferingResponseListener is 2 MiB.

The default max capacity for ArrayByteBufferPool is 64 KiB.

If the response content is within 64 KiB, then the ByteBuffer is pooled, otherwise the pool will just allocate it on-the-fly like BufferingResponseListener is doing right now.

So yes, there is a benefit for small content, but for larger content you need to size the ArrayByteBufferPool max capacity accordingly.

@wendigo
Copy link
Author

wendigo commented Jan 10, 2025

@sbordet that's why in the PR i've posted I've used NonPooled one to make it compatible with the current implementation. For the pooled usage, the consumer should be responsible for providing a pool that actually pools

@sbordet sbordet self-assigned this Feb 5, 2025
@sbordet
Copy link
Contributor

sbordet commented Feb 6, 2025

@wendigo do you need this in 12.0.x?

I ask because there is a better way to manage the buffers without the ByteBufferPool just by retaining the content chunks, accumulating them, and converting them to a byte[] only once at the end.
Since there is no copy, there are no buffers involved, and hence no need of ByteBufferPool.

However, the behavior of BufferingResponseListener would be slightly changed, and it would be best for this behavior change to be in 12.1.x.

WDYT?

@wendigo
Copy link
Author

wendigo commented Feb 6, 2025

12.1 works for me

sbordet added a commit that referenced this issue Feb 9, 2025
* Introduced RetainingResponseListener as an efficient alternative to BufferingResponseListener, so there is no need to allocate buffers, just to retain the chunks.
* Fixed bug in RBB.takeByteArray().
* Replaced usages of BufferingResponseListener with RetainingResponseListener.

Signed-off-by: Simone Bordet <[email protected]>
@sbordet sbordet moved this to 🏗 In progress in Jetty 12.1.0 Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🏗 In progress
2 participants