Skip to content

Commit

Permalink
IPROTO-394 Optimize TagWriterImpl#writeString for ASCII only Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
wburns committed Oct 4, 2024
1 parent 66a09ae commit 47d2994
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,30 @@ public void writeVarint64(long value) throws IOException {
encoder.writeVarint64(value);
}

public static boolean onlyUTF8(String ba, int off, int len) {
int limit = off + len;
for (int i = off; i < limit; i++) {
if (ba.charAt(i) > 127) {
return false;
}
}
return true;
}

@Override
public void writeString(int number, String value) throws IOException {
// TODO [anistor] This is expensive! What can we do to make it more efficient?
public void writeString(int number, String s) throws IOException {
// Also, when just count bytes for message size we do a useless first conversion, and another one will follow later.

if (onlyUTF8(s, 0, s.length())) {
encoder.writeLengthDelimitedField(number, s.length());
for (int i = 0; i < s.length(); ++i) {
encoder.writeByte((byte) s.charAt(i));
}
return;
}

// Charset.encode is not able to encode directly into our own buffers!
byte[] utf8buffer = value.getBytes(StandardCharsets.UTF_8);
byte[] utf8buffer = s.getBytes(StandardCharsets.UTF_8);

encoder.writeLengthDelimitedField(number, utf8buffer.length);
encoder.writeBytes(utf8buffer, 0, utf8buffer.length);
Expand Down

0 comments on commit 47d2994

Please sign in to comment.