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 UTF8 encoding #259

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Optimize UTF8 encoding
  • Loading branch information
davidik1 committed Nov 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 93122d95b2987b2a13ec9ec7fd56481610f425fd
23 changes: 1 addition & 22 deletions src/main/java/com/timgroup/statsd/StatsDProcessor.java
Original file line number Diff line number Diff line change
@@ -4,11 +4,7 @@

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
@@ -47,10 +43,6 @@ public abstract class StatsDProcessor {

protected abstract class ProcessingTask implements Runnable {
protected StringBuilder builder = new StringBuilder();
protected CharBuffer buffer = CharBuffer.wrap(builder);
protected final CharsetEncoder utf8Encoder = MESSAGE_CHARSET.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);

public final void run() {
try {
@@ -148,20 +140,7 @@ protected void processLoop() {
abstract Message getMessage() throws InterruptedException;

protected void writeBuilderToSendBuffer(ByteBuffer sendBuffer) {

int length = builder.length();
// use existing charbuffer if possible, otherwise re-wrap
if (length <= buffer.capacity()) {
buffer.limit(length).position(0);
} else {
buffer = CharBuffer.wrap(builder);
}

sendBuffer.mark();
if (utf8Encoder.encode(buffer, sendBuffer, true) == CoderResult.OVERFLOW) {
sendBuffer.reset();
throw new BufferOverflowException();
}
sendBuffer.put(builder.toString().getBytes(MESSAGE_CHARSET));
}
}