Added networking optimization, to avoid wasted processing on every read.

Thanks to @catbref for finding this.
This commit is contained in:
CalDescent 2022-01-22 18:43:32 +00:00
parent b30445c5f8
commit 25efee55b8

View File

@ -473,16 +473,18 @@ public class Peer {
return;
}
if (bytesRead > 0) {
byte[] leadingBytes = new byte[Math.min(bytesRead, 8)];
this.byteBuffer.asReadOnlyBuffer().position(priorPosition).get(leadingBytes);
String leadingHex = HashCode.fromBytes(leadingBytes).toString();
if (LOGGER.isTraceEnabled()) {
if (bytesRead > 0) {
byte[] leadingBytes = new byte[Math.min(bytesRead, 8)];
this.byteBuffer.asReadOnlyBuffer().position(priorPosition).get(leadingBytes);
String leadingHex = HashCode.fromBytes(leadingBytes).toString();
LOGGER.trace("[{}] Received {} bytes, starting {}, into byteBuffer[{}] from peer {}",
this.peerConnectionId, bytesRead, leadingHex, priorPosition, this);
} else {
LOGGER.trace("[{}] Received {} bytes into byteBuffer[{}] from peer {}", this.peerConnectionId,
bytesRead, priorPosition, this);
LOGGER.trace("[{}] Received {} bytes, starting {}, into byteBuffer[{}] from peer {}",
this.peerConnectionId, bytesRead, leadingHex, priorPosition, this);
} else {
LOGGER.trace("[{}] Received {} bytes into byteBuffer[{}] from peer {}", this.peerConnectionId,
bytesRead, priorPosition, this);
}
}
final boolean wasByteBufferFull = !this.byteBuffer.hasRemaining();