[SPARK-7450] Use UNSAFE.getLong() to speed up BitSetMethods#anySet()

Author: tedyu <yuzhihong@gmail.com>

Closes #5897 from tedyu/master and squashes the following commits:

473bf9d [tedyu] Address Josh's review comments
1719c5b [tedyu] Correct upper bound in for loop
b51dcaf [tedyu] Add unit test in BitSetSuite for BitSet#anySet()
83f9f87 [tedyu] Merge branch 'master' of github.com:apache/spark
817e3f9 [tedyu] Replace constant 8 with SIZE_OF_LONG
75a467b [tedyu] Correct offset for UNSAFE.getLong()
855374b [tedyu] Remove second loop since bitSetWidthInBytes is WORD aligned
093b7a4 [tedyu] Use UNSAFE.getLong() to speed up BitSetMethods#anySet()
63ee050 [tedyu] Use UNSAFE.getLong() to speed up BitSetMethods#anySet()
4ca0ef6 [tedyu] Use UNSAFE.getLong() to speed up BitSetMethods#anySet()
3e9b6919 [tedyu] Use UNSAFE.getLong() to speed up BitSetMethods#anySet()

(cherry picked from commit 88063c6268)
Signed-off-by: Josh Rosen <joshrosen@databricks.com>
This commit is contained in:
tedyu 2015-05-07 16:53:59 -07:00 committed by Josh Rosen
parent 622a0c51c7
commit 99897fe3ef
3 changed files with 19 additions and 3 deletions

View file

@ -102,4 +102,12 @@ public final class BitSet {
public int nextSetBit(int fromIndex) {
return BitSetMethods.nextSetBit(baseObject, baseOffset, fromIndex, numWords);
}
/**
* Returns {@code true} if any bit is set.
*/
public boolean anySet() {
return BitSetMethods.anySet(baseObject, baseOffset, numWords);
}
}

View file

@ -70,9 +70,10 @@ public final class BitSetMethods {
/**
* Returns {@code true} if any bit is set.
*/
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInBytes) {
for (int i = 0; i <= bitSetWidthInBytes; i++) {
if (PlatformDependent.UNSAFE.getByte(baseObject, baseOffset + i) != 0) {
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInWords) {
long addr = baseOffset;
for (int i = 0; i < bitSetWidthInWords; i++, addr += WORD_SIZE) {
if (PlatformDependent.UNSAFE.getLong(baseObject, addr) != 0) {
return true;
}
}

View file

@ -39,6 +39,8 @@ public class BitSetSuite {
for (int i = 0; i < bs.capacity(); i++) {
Assert.assertFalse(bs.isSet(i));
}
// another form of asserting that the bit set is empty
Assert.assertFalse(bs.anySet());
// Set every bit and check it.
for (int i = 0; i < bs.capacity(); i++) {
@ -52,6 +54,11 @@ public class BitSetSuite {
bs.unset(i);
Assert.assertFalse(bs.isSet(i));
}
// Make sure anySet() can detect any set bit
bs = createBitSet(256);
bs.set(64);
Assert.assertTrue(bs.anySet());
}
@Test