chore(spanner): reuse CharsetEncoder in ChecksumResultSet#13455
Open
olavloite wants to merge 1 commit into
Open
chore(spanner): reuse CharsetEncoder in ChecksumResultSet#13455olavloite wants to merge 1 commit into
olavloite wants to merge 1 commit into
Conversation
Reuse the CharsetEncoder in ChecksumResultSet to prevent the creation of a new encoder for each string that we encounter.
Contributor
There was a problem hiding this comment.
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; |
Contributor
There was a problem hiding this comment.
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(); | ||
| } |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reuse the CharsetEncoder in ChecksumResultSet to prevent the creation of a new encoder for each string that we encounter.