Skip to content

Commit 99897fe

Browse files
tedyuJoshRosen
authored andcommitted
[SPARK-7450] Use UNSAFE.getLong() to speed up BitSetMethods#anySet()
Author: tedyu <[email protected]> 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() 3e9b691 [tedyu] Use UNSAFE.getLong() to speed up BitSetMethods#anySet() (cherry picked from commit 88063c6) Signed-off-by: Josh Rosen <[email protected]>
1 parent 622a0c5 commit 99897fe

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

unsafe/src/main/java/org/apache/spark/unsafe/bitset/BitSet.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ public boolean isSet(int index) {
102102
public int nextSetBit(int fromIndex) {
103103
return BitSetMethods.nextSetBit(baseObject, baseOffset, fromIndex, numWords);
104104
}
105+
106+
/**
107+
* Returns {@code true} if any bit is set.
108+
*/
109+
public boolean anySet() {
110+
return BitSetMethods.anySet(baseObject, baseOffset, numWords);
111+
}
112+
105113
}

unsafe/src/main/java/org/apache/spark/unsafe/bitset/BitSetMethods.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ public static boolean isSet(Object baseObject, long baseOffset, int index) {
7070
/**
7171
* Returns {@code true} if any bit is set.
7272
*/
73-
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInBytes) {
74-
for (int i = 0; i <= bitSetWidthInBytes; i++) {
75-
if (PlatformDependent.UNSAFE.getByte(baseObject, baseOffset + i) != 0) {
73+
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInWords) {
74+
long addr = baseOffset;
75+
for (int i = 0; i < bitSetWidthInWords; i++, addr += WORD_SIZE) {
76+
if (PlatformDependent.UNSAFE.getLong(baseObject, addr) != 0) {
7677
return true;
7778
}
7879
}

unsafe/src/test/java/org/apache/spark/unsafe/bitset/BitSetSuite.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public void basicOps() {
3939
for (int i = 0; i < bs.capacity(); i++) {
4040
Assert.assertFalse(bs.isSet(i));
4141
}
42+
// another form of asserting that the bit set is empty
43+
Assert.assertFalse(bs.anySet());
4244

4345
// Set every bit and check it.
4446
for (int i = 0; i < bs.capacity(); i++) {
@@ -52,6 +54,11 @@ public void basicOps() {
5254
bs.unset(i);
5355
Assert.assertFalse(bs.isSet(i));
5456
}
57+
58+
// Make sure anySet() can detect any set bit
59+
bs = createBitSet(256);
60+
bs.set(64);
61+
Assert.assertTrue(bs.anySet());
5562
}
5663

5764
@Test

0 commit comments

Comments
 (0)