Skip to content

Enhance buffer handling to reduce direct list access. #1675

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

Merged
merged 5 commits into from
Apr 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ByteBufferBsonOutput extends OutputBuffer {
private int curBufferIndex = 0; private int curBufferIndex = 0;
private int position = 0; private int position = 0;
private boolean closed; private boolean closed;
private ByteBuf currentByteBuffer;


/** /**
* Construct an instance that uses the given buffer provider to allocate byte buffers as needs as it grows. * Construct an instance that uses the given buffer provider to allocate byte buffers as needs as it grows.
Expand Down Expand Up @@ -169,13 +170,16 @@ public void writeByte(final int value) {
} }


private ByteBuf getCurrentByteBuffer() { private ByteBuf getCurrentByteBuffer() {
ByteBuf curByteBuffer = getByteBufferAtIndex(curBufferIndex); if (currentByteBuffer == null) {
if (curByteBuffer.hasRemaining()) { currentByteBuffer = getByteBufferAtIndex(curBufferIndex);
return curByteBuffer; }
if (currentByteBuffer.hasRemaining()) {
return currentByteBuffer;
} }


curBufferIndex++; curBufferIndex++;
return getByteBufferAtIndex(curBufferIndex); currentByteBuffer = getByteBufferAtIndex(curBufferIndex);
return currentByteBuffer;
} }


private ByteBuf getByteBufferAtIndex(final int index) { private ByteBuf getByteBufferAtIndex(final int index) {
Expand Down Expand Up @@ -259,6 +263,10 @@ public void truncateToPosition(final int newPosition) {


bufferList.get(bufferPositionPair.bufferIndex).position(bufferPositionPair.position); bufferList.get(bufferPositionPair.bufferIndex).position(bufferPositionPair.position);


if (bufferPositionPair.bufferIndex + 1 < bufferList.size()) {
currentByteBuffer = null;
}

while (bufferList.size() > bufferPositionPair.bufferIndex + 1) { while (bufferList.size() > bufferPositionPair.bufferIndex + 1) {
ByteBuf buffer = bufferList.remove(bufferList.size() - 1); ByteBuf buffer = bufferList.remove(bufferList.size() - 1);
buffer.release(); buffer.release();
Expand Down Expand Up @@ -286,6 +294,7 @@ public void close() {
for (final ByteBuf cur : bufferList) { for (final ByteBuf cur : bufferList) {
cur.release(); cur.release();
} }
currentByteBuffer = null;
bufferList.clear(); bufferList.clear();
closed = true; closed = true;
} }
Expand Down Expand Up @@ -325,6 +334,7 @@ private void merge(final ByteBufferBsonOutput branch) {
bufferList.addAll(branch.bufferList); bufferList.addAll(branch.bufferList);
curBufferIndex += branch.curBufferIndex + 1; curBufferIndex += branch.curBufferIndex + 1;
position += branch.position; position += branch.position;
currentByteBuffer = null;
} }


public static final class Branch extends ByteBufferBsonOutput { public static final class Branch extends ByteBufferBsonOutput {
Expand Down