Skip to content

Commit

Permalink
Avoid unnecessary volatile reads
Browse files Browse the repository at this point in the history
  • Loading branch information
rhernandez35 committed Mar 29, 2024
1 parent 6b5de8c commit 3e291fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ private void initialize() throws IOException {
ioSession.setEvent(SelectionKey.OP_WRITE);
}

private void writeOutPreface(final IOSession session) throws IOException {
/**
* @return true if the entire preface has been written out
*/
private boolean writeOutPreface(final IOSession session, ByteBuffer preface) throws IOException {
if (preface.hasRemaining()) {
session.write(preface);
}
Expand All @@ -115,8 +118,9 @@ private void writeOutPreface(final IOSession session) throws IOException {
if (inBuf != null) {
inBuf.clear();
}
preface = null;
return true;
}
return false;
}

@Override
Expand All @@ -131,8 +135,11 @@ public void outputReady(final IOSession session) throws IOException {
if (initialized.compareAndSet(false, true)) {
initialize();
}
ByteBuffer preface = this.preface;
if (preface != null) {
writeOutPreface(session);
if (writeOutPreface(session, preface)) {
this.preface = null;
}
} else {
throw new ProtocolNegotiationException("Unexpected output");
}
Expand All @@ -146,8 +153,11 @@ public void inputReady(final IOSession session, final ByteBuffer src) throws IOE
}
inBuf.put(src);
}
ByteBuffer preface = this.preface;
if (preface != null) {
writeOutPreface(session);
if (writeOutPreface(session, preface)) {
this.preface = null;
}
} else {
throw new ProtocolNegotiationException("Unexpected input");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private void doDecode(final boolean endOfStream) throws IOException {
}

private CharsetDecoder getCharsetDecoder() {
CharsetDecoder charsetDecoder = this.charsetDecoder;
if (charsetDecoder == null) {
Charset charset = this.charset;
if (charset == null) {
Expand All @@ -120,6 +121,7 @@ private CharsetDecoder getCharsetDecoder() {
charset = StandardCharsets.UTF_8;
}
charsetDecoder = charset.newDecoder();
this.charsetDecoder = charsetDecoder;
if (charCodingConfig.getMalformedInputAction() != null) {
charsetDecoder.onMalformedInput(charCodingConfig.getMalformedInputAction());
}
Expand All @@ -134,6 +136,7 @@ private CharsetDecoder getCharsetDecoder() {
public final void consume(final ByteBuffer src) throws IOException {
final CharsetDecoder charsetDecoder = getCharsetDecoder();
while (src.hasRemaining()) {
ByteBuffer byteBuffer = this.byteBuffer;
if (byteBuffer != null && byteBuffer.position() > 0) {
// There are some left-overs from the previous input operation
final int n = byteBuffer.remaining();
Expand All @@ -159,6 +162,7 @@ public final void consume(final ByteBuffer src) throws IOException {
// in case of input underflow src can be expected to be very small (one incomplete UTF8 char)
if (byteBuffer == null) {
byteBuffer = ByteBuffer.allocate(Math.max(src.remaining(), 1024));
this.byteBuffer = byteBuffer;
}
byteBuffer.put(src);
}
Expand Down

0 comments on commit 3e291fb

Please sign in to comment.