Skip to content

Commit da463e0

Browse files
authored
Merge pull request #19664 from lorentey/dictionary-subscript-setter-is-slow
[stdlib] Speed up _Bitset in size-optimized builds
2 parents 2b1b68f + 267b80f commit da463e0

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

stdlib/public/core/Bitset.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ extension _UnsafeBitset {
3838
@inline(__always)
3939
internal static func word(for element: Int) -> Int {
4040
_sanityCheck(element >= 0)
41-
return element / Word.capacity
41+
// Note: We perform on UInts to get faster unsigned math (shifts).
42+
let element = UInt(bitPattern: element)
43+
let capacity = UInt(bitPattern: Word.capacity)
44+
return Int(bitPattern: element / capacity)
4245
}
4346

4447
@inlinable
@@ -61,22 +64,22 @@ extension _UnsafeBitset {
6164
@inline(__always)
6265
internal static func join(word: Int, bit: Int) -> Int {
6366
_sanityCheck(bit >= 0 && bit < Word.capacity)
64-
return word * Word.capacity + bit
67+
return word &* Word.capacity + bit
6568
}
6669
}
6770

6871
extension _UnsafeBitset {
6972
@inlinable
7073
@inline(__always)
7174
internal static func wordCount(forCapacity capacity: Int) -> Int {
72-
return (capacity + Word.capacity - 1) / Word.capacity
75+
return word(for: capacity + Word.capacity - 1)
7376
}
7477

7578
@inlinable
7679
internal var capacity: Int {
7780
@inline(__always)
7881
get {
79-
return wordCount * Word.capacity
82+
return wordCount &* Word.capacity
8083
}
8184
}
8285

0 commit comments

Comments
 (0)