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

optimize number of buffer allocations #11879

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
*/
class OkHttpWritableBufferAllocator implements WritableBufferAllocator {

// Use 4k as our minimum buffer size.
private static final int MIN_BUFFER = 4096;
Copy link
Member

Choose a reason for hiding this comment

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

Okio Buffer works quite differently than Netty. Okio uses Segments that are all the same size and then combines adjacent buffers to avoid excessive internal fragmentation. We should leave this in-place and actually set it to 8192.

Copy link
Member

Choose a reason for hiding this comment

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

(If we were more serious, we'd round up: (capacityHint + 8191) / 8192 * 8192.)


// Set the maximum buffer size to 1MB
private static final int MAX_BUFFER = 1024 * 1024;

Expand All @@ -45,7 +42,7 @@ class OkHttpWritableBufferAllocator implements WritableBufferAllocator {
*/
@Override
public WritableBuffer allocate(int capacityHint) {
capacityHint = Math.min(MAX_BUFFER, Math.max(MIN_BUFFER, capacityHint));
capacityHint = Math.min(MAX_BUFFER, capacityHint);
return new OkHttpWritableBuffer(new Buffer(), capacityHint);
}
}