Skip to content

Commit 657f639

Browse files
committed
Optimize LongHashFunctions.hashBytes(ByteBuffer) and bound checks in other methods in LongHashFunction class
1 parent 6f2f7c6 commit 657f639

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

src/main/java/net/openhft/hashing/LongHashFunction.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package net.openhft.hashing;
1818

1919
import org.jetbrains.annotations.NotNull;
20+
import sun.nio.ch.DirectBuffer;
2021

2122
import java.io.Serializable;
2223
import java.nio.ByteBuffer;
@@ -397,7 +398,7 @@ public long hashBoolean(boolean input) {
397398
* Shortcut for {@link #hashBooleans(boolean[], int, int) hashBooleans(input, 0, input.length)}.
398399
*/
399400
public long hashBooleans(@NotNull boolean[] input) {
400-
return hashBooleans(input, 0, input.length);
401+
return unsafeHash(input, BOOLEAN_BASE, input.length);
401402
}
402403

403404
/**
@@ -422,7 +423,7 @@ public long hashBooleans(@NotNull boolean[] input, int off, int len) {
422423
* Shortcut for {@link #hashBytes(byte[], int, int) hashBytes(input, 0, input.length)}.
423424
*/
424425
public long hashBytes(@NotNull byte[] input) {
425-
return hashBytes(input, 0, input.length);
426+
return unsafeHash(input, BYTE_BASE, input.length);
426427
}
427428

428429
/**
@@ -448,7 +449,7 @@ public long hashBytes(@NotNull byte[] input, int off, int len) {
448449
* hashBytes(input, input.position(), input.remaining())}.
449450
*/
450451
public long hashBytes(ByteBuffer input) {
451-
return hashBytes(input, input.position(), input.remaining());
452+
return hashByteBuffer(input, input.position(), input.remaining());
452453
}
453454

454455
/**
@@ -469,7 +470,17 @@ public long hashBytes(ByteBuffer input) {
469470
*/
470471
public long hashBytes(@NotNull ByteBuffer input, int off, int len) {
471472
checkArrayOffs(input.capacity(), off, len);
472-
return hash(input, ByteBufferAccess.INSTANCE, off, len);
473+
return hashByteBuffer(input, off, len);
474+
}
475+
476+
private long hashByteBuffer(@NotNull ByteBuffer input, int off, int len) {
477+
if (input.hasArray()) {
478+
return unsafeHash(input.array(), BYTE_BASE + input.arrayOffset() + off, len);
479+
} else if (input instanceof DirectBuffer) {
480+
return unsafeHash(null, ((DirectBuffer) input).address() + off, len);
481+
} else {
482+
return hash(input, ByteBufferAccess.INSTANCE, off, len);
483+
}
473484
}
474485

475486
/**
@@ -490,7 +501,7 @@ public long hashMemory(long address, long len) {
490501
* Shortcut for {@link #hashChars(char[], int, int) hashChars(input, 0, input.length)}.
491502
*/
492503
public long hashChars(@NotNull char[] input) {
493-
return hashChars(input, 0, input.length);
504+
return unsafeHash(input, CHAR_BASE, input.length * 2L);
494505
}
495506

496507
/**
@@ -563,6 +574,7 @@ public long hashChars(@NotNull StringBuilder input) {
563574
* or {@code len < 0}
564575
*/
565576
public long hashChars(@NotNull StringBuilder input, int off, int len) {
577+
checkArrayOffs(input.length(), off, len);
566578
return hashNativeChars(input, off, len);
567579
}
568580

@@ -571,15 +583,14 @@ long hashNativeChars(CharSequence input) {
571583
}
572584

573585
long hashNativeChars(CharSequence input, int off, int len) {
574-
checkArrayOffs(input.length(), off, len);
575586
return hash(input, nativeCharSequenceAccess(), off * 2L, len * 2L);
576587
}
577588

578589
/**
579590
* Shortcut for {@link #hashShorts(short[], int, int) hashShorts(input, 0, input.length)}.
580591
*/
581592
public long hashShorts(@NotNull short[] input) {
582-
return hashShorts(input, 0, input.length);
593+
return unsafeHash(input, SHORT_BASE, input.length * 2L);
583594
}
584595

585596
/**
@@ -606,7 +617,7 @@ public long hashShorts(@NotNull short[] input, int off, int len) {
606617
* Shortcut for {@link #hashInts(int[], int, int) hashInts(input, 0, input.length)}.
607618
*/
608619
public long hashInts(@NotNull int[] input) {
609-
return hashInts(input, 0, input.length);
620+
return unsafeHash(input, INT_BASE, input.length * 4L);
610621
}
611622

612623
/**
@@ -633,7 +644,7 @@ public long hashInts(@NotNull int[] input, int off, int len) {
633644
* Shortcut for {@link #hashLongs(long[], int, int) hashLongs(input, 0, input.length)}.
634645
*/
635646
public long hashLongs(@NotNull long[] input) {
636-
return hashLongs(input, 0, input.length);
647+
return unsafeHash(input, LONG_BASE, input.length * 8L);
637648
}
638649

639650
/**

0 commit comments

Comments
 (0)