Skip to content

chore(spanner): reuse CharsetEncoder in ChecksumResultSet#13455

Open
olavloite wants to merge 1 commit into
mainfrom
spanner-reuse-charset-encoder
Open

chore(spanner): reuse CharsetEncoder in ChecksumResultSet#13455
olavloite wants to merge 1 commit into
mainfrom
spanner-reuse-charset-encoder

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Reuse the CharsetEncoder in ChecksumResultSet to prevent the creation of a new encoder for each string that we encounter.

Reuse the CharsetEncoder in ChecksumResultSet to prevent the creation of a
new encoder for each string that we encounter.
@olavloite olavloite requested review from a team as code owners June 12, 2026 17:27
@olavloite olavloite requested a review from sakthivelmanii June 12, 2026 17:27

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes string encoding in ChecksumResultSet by reusing a CharsetEncoder instance in ChecksumCalculator instead of instantiating a new one on every putString call. The reviewer suggested eagerly initializing the encoder as a final field and directly calling encoder.reset() to simplify the code and avoid unnecessary null checks.

private final MessageDigest digest;
private ByteBuffer buffer;
private ByteBuffer float64Buffer;
private CharsetEncoder encoder;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of lazily initializing the encoder field with a null check in putString, we can declare it as final and initialize it directly at the declaration site. This simplifies the code and avoids unnecessary branching during execution.

Suggested change
private CharsetEncoder encoder;
private final CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();

Comment on lines +342 to +346
if (encoder == null) {
encoder = StandardCharsets.UTF_8.newEncoder();
} else {
encoder.reset();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the encoder is now eagerly initialized and declared as final, we can replace the null check with a direct call to encoder.reset().

      encoder.reset();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant