Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ private static final class ChecksumCalculator {
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();


ChecksumCalculator() {
try {
Expand Down Expand Up @@ -338,7 +339,11 @@ private void putString(String stringValue) {
// creating a new copy of (a part of) the string. E.g. using something like substring(..)
// would create a copy of that part of the string, using CharBuffer.wrap(..) does not.
CharBuffer source = CharBuffer.wrap(stringValue);
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
if (encoder == null) {
encoder = StandardCharsets.UTF_8.newEncoder();
} else {
encoder.reset();
}
Comment on lines +342 to +346

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();

// source.hasRemaining() returns false when all the characters in the string have been
// processed.
while (source.hasRemaining()) {
Expand Down
Loading