Skip to content

Commit

Permalink
Merge pull request alteryx#173 from davidnavas/bitSetPerf
Browse files Browse the repository at this point in the history
SKIPME: Clear just the bits that are necessary
  • Loading branch information
markhamstra committed Aug 15, 2016
2 parents b2cbc07 + f3910fa commit e69d5c5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
14 changes: 14 additions & 0 deletions core/src/main/scala/org/apache/spark/util/collection/BitSet.scala
Expand Up @@ -57,6 +57,20 @@ class BitSet(numBits: Int) extends Serializable {
}
}

/**
* Clear all the bits up to a given index
*/
def clearUntil(bitIndex: Int) {
val wordIndex = bitIndex >> 6 // divide by 64
var i = 0
while(i < wordIndex) { words(i) = 0; i += 1 }
if(wordIndex < words.length) {
// Set the remaining bits (note that the mask could still be zero)
val mask = ~(-1L << (bitIndex & 0x3f))
words(wordIndex) &= ~mask
}
}

/**
* Compute the bit-wise AND of the two sets returning the
* result.
Expand Down
Expand Up @@ -152,4 +152,36 @@ class BitSetSuite extends SparkFunSuite {
assert(bitsetDiff.nextSetBit(85) === 85)
assert(bitsetDiff.nextSetBit(86) === -1)
}

test( "[gs]etUntil" ) {
val bitSet = new BitSet(100)

bitSet.setUntil(bitSet.capacity)

(0 until bitSet.capacity).foreach { i =>
assert(bitSet.get(i))
}

bitSet.clearUntil(bitSet.capacity)

(0 until bitSet.capacity).foreach { i =>
assert(!bitSet.get(i))
}

val setUntil = bitSet.capacity / 2
bitSet.setUntil(setUntil)

val clearUntil = setUntil / 2
bitSet.clearUntil(clearUntil)

(0 until clearUntil).foreach { i =>
assert(!bitSet.get(i))
}
(clearUntil until setUntil).foreach { i =>
assert(bitSet.get(i))
}
(setUntil until bitSet.capacity).foreach { i =>
assert(!bitSet.get(i))
}
}
}
Expand Up @@ -371,12 +371,12 @@ private class SortMergeFullOuterJoinScanner(
}

if (leftMatches.size <= leftMatched.capacity) {
leftMatched.clear()
leftMatched.clearUntil(leftMatches.size)
} else {
leftMatched = new BitSet(leftMatches.size)
}
if (rightMatches.size <= rightMatched.capacity) {
rightMatched.clear()
rightMatched.clearUntil(rightMatches.size)
} else {
rightMatched = new BitSet(rightMatches.size)
}
Expand Down

0 comments on commit e69d5c5

Please sign in to comment.