Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public final class BitSetMethods {

private static final long WORD_SIZE = 8;
private static final int SIZE_OF_LONG = Long.SIZE;

private BitSetMethods() {
// Make the default constructor private, since this only holds static methods.
Expand Down Expand Up @@ -71,7 +72,13 @@ public static boolean isSet(Object baseObject, long baseOffset, int index) {
* Returns {@code true} if any bit is set.
*/
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInBytes) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, bitSetWidthInBytes should be changed to bitSetWidthInWords and the rest of the code should be updated accordingly.

for (int i = 0; i <= bitSetWidthInBytes; i++) {
long widthInLong = bitSetWidthInBytes / SIZE_OF_LONG;
Copy link
Contributor

Choose a reason for hiding this comment

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

width needs to be an int too :)

for (long i = 0; i <= widthInLong; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

actually we should use int instead of long for i. the reason is that JIT would inject a safepoint for loops over longs which prevents loop unrolling and incurs an extra check for every iteration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed the above comment.

if (PlatformDependent.UNSAFE.getLong(baseObject, baseOffset + i) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that this is correct, since i loops over words but the second argument to getLong is measured in bytes. You could do i * 8 instead.

Reynold pointed out that there might be faster micro-optimizations tricks that can be played here; see https://groups.google.com/d/msg/mechanical-sympathy/k0qd7dLHFQE/gsTFSjP6MVMJ

return true;
}
}
for (long i = SIZE_OF_LONG * widthInLong; i < bitSetWidthInBytes; i++) {
if (PlatformDependent.UNSAFE.getByte(baseObject, baseOffset + i) != 0) {
return true;
}
Expand Down