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

Reuse byte buffers in BufferingResponseListener #12691

Open
wants to merge 1 commit into
base: jetty-12.0.x
Choose a base branch
from

Conversation

wendigo
Copy link

@wendigo wendigo commented Jan 9, 2025

Fixes #12687

@wendigo wendigo force-pushed the serafin/buffering branch from a919e29 to e6b700d Compare January 9, 2025 17:37
@wendigo
Copy link
Author

wendigo commented Jan 10, 2025

@sbordet ptal

*/
public BufferingResponseListener(ByteBufferPool byteBufferPool, int maxLength)
{
this.buffer = new ByteBufferAccumulator(requireNonNull(byteBufferPool, "byteBufferPool is null"), true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.buffer = new ByteBufferAccumulator(requireNonNull(byteBufferPool, "byteBufferPool is null"), true);
this.buffer = new ByteBufferAccumulator(Objects.requireNonNullElse(byteBufferPool, ByteBufferPool.NON_POOLING), true);

@@ -55,6 +59,18 @@ public BufferingResponseListener()
*/
public BufferingResponseListener(int maxLength)
{
this(ByteBufferPool.NON_POOLING, maxLength);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this(ByteBufferPool.NON_POOLING, maxLength);
this(null, maxLength);

See below

@@ -36,7 +39,8 @@
public abstract class BufferingResponseListener implements Listener
{
private final int maxLength;
private ByteBuffer buffer;
private final ByteBufferAccumulator buffer;
Copy link
Contributor

Choose a reason for hiding this comment

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

This class is deprecated in 12.0. In 12.1 it should probably be a org.eclipse.jetty.io.RetainableByteBuffer.DynamicCapacity

Perhaps it is best to rebase this PR to 12.1 and use that class. 12.1 will soon be the main branch.

Comment on lines +193 to +208
return new ByteArrayInputStream(toByteArray());
}

private byte[] toByteArray()
{
synchronized (buffer)
{
try (ByteBufferAccumulator buffer = this.buffer)
{
if (buffer.getLength() == 0)
result = new byte[0];
else
result = buffer.toByteArray();
}
}
return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

ideally we would not have to copy all the retained buffers to a single buffer just to stream as a InputStream. We should have a mechanism to do this without this extra copy.

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.

Buffer reusal in the BufferingResponseListener
2 participants