Skip to content

Commit 9eb2743

Browse files
committed
[stdlib] _UnsafeBitMap: make internal; force-inline members
A Dictionary.removeValue(forKey:) benchmark regressed 35% because recent changes in this PR caused an _UnsafeBitMap member to not be inlined in its implementation. (This was probably triggered by moving a method from Dictionary._Variant to _NativeDictionary.) Add @inline(__always) to _UnsafeBitMap members. While we’re at it, make _UnsafeBitMap @usableFromInline. It’s only public for testing purposes.
1 parent b0471b5 commit 9eb2743

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

stdlib/public/core/UnsafeBitMap.swift

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,65 +11,68 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// A wrapper around a bitmap storage with room for at least `bitCount` bits.
14-
@_fixed_layout // FIXME(sil-serialize-all)
15-
public // @testable
16-
struct _UnsafeBitMap {
17-
public // @testable
18-
let values: UnsafeMutablePointer<UInt>
14+
@_fixed_layout
15+
@usableFromInline // @testable
16+
internal struct _UnsafeBitMap {
17+
@usableFromInline
18+
internal let values: UnsafeMutablePointer<UInt>
1919

20-
public // @testable
21-
let bitCount: Int
20+
@usableFromInline
21+
internal let bitCount: Int
2222

23-
@inlinable // FIXME(sil-serialize-all)
24-
public // @testable
25-
static func wordIndex(_ i: Int) -> Int {
23+
@inlinable
24+
@inline(__always)
25+
internal static func wordIndex(_ i: Int) -> Int {
2626
// Note: We perform the operation on UInts to get faster unsigned math
2727
// (shifts).
2828
return Int(bitPattern: UInt(bitPattern: i) / UInt(UInt.bitWidth))
2929
}
3030

31-
@inlinable // FIXME(sil-serialize-all)
32-
public // @testable
33-
static func bitIndex(_ i: Int) -> UInt {
31+
@inlinable
32+
@inline(__always)
33+
internal static func bitIndex(_ i: Int) -> UInt {
3434
// Note: We perform the operation on UInts to get faster unsigned math
3535
// (shifts).
3636
return UInt(bitPattern: i) % UInt(UInt.bitWidth)
3737
}
3838

39-
@inlinable // FIXME(sil-serialize-all)
40-
public // @testable
41-
static func sizeInWords(forSizeInBits bitCount: Int) -> Int {
39+
@inlinable
40+
@inline(__always)
41+
internal static func sizeInWords(forSizeInBits bitCount: Int) -> Int {
4242
return (bitCount + Int.bitWidth - 1) / Int.bitWidth
4343
}
4444

45-
@inlinable // FIXME(sil-serialize-all)
46-
public // @testable
47-
init(storage: UnsafeMutablePointer<UInt>, bitCount: Int) {
45+
@inlinable
46+
@inline(__always)
47+
internal init(storage: UnsafeMutablePointer<UInt>, bitCount: Int) {
4848
self.bitCount = bitCount
4949
self.values = storage
5050
}
5151

52-
@inlinable // FIXME(sil-serialize-all)
53-
public // @testable
54-
var numberOfWords: Int {
55-
return _UnsafeBitMap.sizeInWords(forSizeInBits: bitCount)
52+
@inlinable
53+
internal var numberOfWords: Int {
54+
@inline(__always)
55+
get {
56+
return _UnsafeBitMap.sizeInWords(forSizeInBits: bitCount)
57+
}
5658
}
5759

58-
@inlinable // FIXME(sil-serialize-all)
59-
public // @testable
60-
func initializeToZero() {
60+
@inlinable
61+
@inline(__always)
62+
internal func initializeToZero() {
6163
values.initialize(repeating: 0, count: numberOfWords)
6264
}
6365

64-
@inlinable // FIXME(sil-serialize-all)
65-
public // @testable
66-
subscript(i: Int) -> Bool {
66+
@inlinable
67+
internal subscript(i: Int) -> Bool {
68+
@inline(__always)
6769
get {
6870
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
6971
let word = values[_UnsafeBitMap.wordIndex(i)]
7072
let bit = word & (1 << _UnsafeBitMap.bitIndex(i))
7173
return bit != 0
7274
}
75+
@inline(__always)
7376
nonmutating set {
7477
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
7578
let wordIdx = _UnsafeBitMap.wordIndex(i)

validation-test/stdlib/UnsafeBitMap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift
1+
// RUN: %target-run-stdlib-swift
22
// REQUIRES: executable_test
33

44
import StdlibUnittest

0 commit comments

Comments
 (0)