Skip to content

Commit cfc6ba7

Browse files
authored
chore(spanner): reuse CharsetEncoder in ChecksumResultSet (#13455)
Reuse the CharsetEncoder in ChecksumResultSet to prevent the creation of a new encoder for each string that we encounter.
1 parent 05c2a56 commit cfc6ba7

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ChecksumResultSet.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ private static final class ChecksumCalculator {
212212
private final MessageDigest digest;
213213
private ByteBuffer buffer;
214214
private ByteBuffer float64Buffer;
215+
private CharsetEncoder encoder;
215216

216217
ChecksumCalculator() {
217218
try {
@@ -338,7 +339,11 @@ private void putString(String stringValue) {
338339
// creating a new copy of (a part of) the string. E.g. using something like substring(..)
339340
// would create a copy of that part of the string, using CharBuffer.wrap(..) does not.
340341
CharBuffer source = CharBuffer.wrap(stringValue);
341-
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
342+
if (encoder == null) {
343+
encoder = StandardCharsets.UTF_8.newEncoder();
344+
} else {
345+
encoder.reset();
346+
}
342347
// source.hasRemaining() returns false when all the characters in the string have been
343348
// processed.
344349
while (source.hasRemaining()) {

0 commit comments

Comments
 (0)