8000 Improve performance of BitSet.size · scala/scala@989c0d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 989c0d0

Browse files
committed
Improve performance of BitSet.size
Using java.lang.Long.bitCount for the size computation is a lot faster than the previous Scala implementation. Closes SI-2196.
1 parent 947797e commit 989c0d0

File tree

1 file changed

+1
-12
lines changed

1 file changed

+1
-12
lines changed

src/library/scala/collection/BitSetLike.scala

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe
6565
var i = nwords
6666
while (i > 0) {
6767
i -= 1
68-
s += popCount(word(i))
68+
s += java.lang.Long.bitCount(word(i))
6969
}
7070
s
7171
}
@@ -221,15 +221,4 @@ object BitSetLike {
221221
else assert(w == 0L)
222222
newelems
223223
}
224-
225-
private val pc1: Array[Int] = {
226-
def countBits(x: Int): Int = if (x == 0) 0 else x % 2 + countBits(x >>> 1)
227-
Array.tabulate(256)(countBits _)
228-
}
229-
230-
private def popCount(w: Long): Int = {
231-
def pc2(w: Int) = if (w == 0) 0 else pc1(w & 0xff) + pc1(w >>> 8)
232-
def pc4(w: Int) = if (w == 0) 0 else pc2(w & 0xffff) + pc2(w >>> 16)
233-
if (w == 0L) 0 else pc4(w.toInt) + pc4((w >>> 32).toInt)
234-
}
235224
}

0 commit comments

Comments
 (0)
0