Skip to content

hash64: reduce number of checkSpace calls #1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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
20 changes: 16 additions & 4 deletions spectator-api/src/main/java/com/netflix/spectator/impl/Hash64.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,14 @@ public Hash64 updateString(CharSequence str) {
/** Update the hash with the specified byte value. */
public Hash64 updateByte(byte value) {
checkSpace(Byte.SIZE);
updateByteImpl(value);
return this;
}

private void updateByteImpl(byte value) {
final long v = ((long) value & 0xFFL) << bitPos;
stripe[stripePos] |= v;
bitPos += Byte.SIZE;
return this;
}

/** Update the hash with the specified byte values. */
Expand All @@ -156,17 +160,25 @@ public Hash64 updateBytes(byte[] values, int offset, int length) {

// Complete current stripe
for (int i = offset; i < s; ++i) {
updateByte(values[i]);
updateByteImpl(values[i]);
}
if (++stripePos == stripe.length) {
processStripe();
}
bitPos = 0;

// Write long values
for (int i = s; i < e; i += Long.BYTES) {
updateLong(UnsafeUtils.getLong(values, i));
stripe[stripePos] = UnsafeUtils.getLong(values, i);
if (++stripePos == stripe.length) {
processStripe();
}
}

// Write remaining bytes
stripe[stripePos] = 0L;
for (int i = e; i < offset + length; ++i) {
updateByte(values[i]);
updateByteImpl(values[i]);
}
} else {
for (int i = offset; i < offset + length; ++i) {
Expand Down