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 (int i = 0; i <= widthInLong; i++) {
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 (int i = (int)(SIZE_OF_LONG * widthInLong); i < bitSetWidthInBytes; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this block now contains two for loops. A bad merge conflict resolution, perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so.
The second loop is for the remaining bytes :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sorry for overlooking that. I guess I was thinking of optimizing for the case where the bitset width was a multiple of the word size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That case is covered by the first loop.

Copy link
Contributor

Choose a reason for hiding this comment

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

do we need the 2nd loop? I think the assumption is that all bitsets are word-aligned. We should definitely document that though.

if (PlatformDependent.UNSAFE.getByte(baseObject, baseOffset + i) != 0) {
return true;
}
Expand Down