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 FilterStreamInput for Network Reads #52395

Merged
Show file tree
Hide file tree
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 Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ public void readBytes(byte[] b, int offset, int len) throws IOException {
delegate.readBytes(b, offset, len);
}

@Override
public short readShort() throws IOException {
return delegate.readShort();
}

@Override
public int readInt() throws IOException {
return delegate.readInt();
}

@Override
public long readLong() throws IOException {
return delegate.readLong();
}

@Override
public void reset() throws IOException {
delegate.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ public void readBytes(byte[] b, int offset, int len) throws IOException {
digest.update(b, offset, len);
}

private static final ThreadLocal<byte[]> buffer = ThreadLocal.withInitial(() -> new byte[8]);

@Override
public short readShort() throws IOException {
final byte[] buf = buffer.get();
Copy link
Member Author

Choose a reason for hiding this comment

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

I think even for short this is better than the previous solution. It certainly is when reading from a Netty ByteBuf backed buffer since we save one round of bounds checks.

readBytes(buf, 0, 2);
return (short) (((buf[0] & 0xFF) << 8) | (buf[1] & 0xFF));
}

@Override
public int readInt() throws IOException {
final byte[] buf = buffer.get();
readBytes(buf, 0, 4);
return ((buf[0] & 0xFF) << 24) | ((buf[1] & 0xFF) << 16) | ((buf[2] & 0xFF) << 8) | (buf[3] & 0xFF);
}

@Override
public long readLong() throws IOException {
final byte[] buf = buffer.get();
readBytes(buf, 0, 8);
return (((long) (((buf[0] & 0xFF) << 24) | ((buf[1] & 0xFF) << 16) | ((buf[2] & 0xFF) << 8) | (buf[3] & 0xFF))) << 32)
| ((((buf[4] & 0xFF) << 24) | ((buf[5] & 0xFF) << 16) | ((buf[6] & 0xFF) << 8) | (buf[7] & 0xFF)) & 0xFFFFFFFFL);
}

@Override
public void reset() throws IOException {
delegate.reset();
Expand Down