[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.
This commit is contained in:
Karoy Lorentey
2018-07-26 12:32:27 +01:00
parent b0471b575e
commit 9eb2743dd9
2 changed files with 33 additions and 30 deletions

View File

@@ -11,65 +11,68 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// A wrapper around a bitmap storage with room for at least `bitCount` bits. /// A wrapper around a bitmap storage with room for at least `bitCount` bits.
@_fixed_layout // FIXME(sil-serialize-all) @_fixed_layout
public // @testable @usableFromInline // @testable
struct _UnsafeBitMap { internal struct _UnsafeBitMap {
public // @testable @usableFromInline
let values: UnsafeMutablePointer<UInt> internal let values: UnsafeMutablePointer<UInt>
public // @testable @usableFromInline
let bitCount: Int internal let bitCount: Int
@inlinable // FIXME(sil-serialize-all) @inlinable
public // @testable @inline(__always)
static func wordIndex(_ i: Int) -> Int { internal static func wordIndex(_ i: Int) -> Int {
// Note: We perform the operation on UInts to get faster unsigned math // Note: We perform the operation on UInts to get faster unsigned math
// (shifts). // (shifts).
return Int(bitPattern: UInt(bitPattern: i) / UInt(UInt.bitWidth)) return Int(bitPattern: UInt(bitPattern: i) / UInt(UInt.bitWidth))
} }
@inlinable // FIXME(sil-serialize-all) @inlinable
public // @testable @inline(__always)
static func bitIndex(_ i: Int) -> UInt { internal static func bitIndex(_ i: Int) -> UInt {
// Note: We perform the operation on UInts to get faster unsigned math // Note: We perform the operation on UInts to get faster unsigned math
// (shifts). // (shifts).
return UInt(bitPattern: i) % UInt(UInt.bitWidth) return UInt(bitPattern: i) % UInt(UInt.bitWidth)
} }
@inlinable // FIXME(sil-serialize-all) @inlinable
public // @testable @inline(__always)
static func sizeInWords(forSizeInBits bitCount: Int) -> Int { internal static func sizeInWords(forSizeInBits bitCount: Int) -> Int {
return (bitCount + Int.bitWidth - 1) / Int.bitWidth return (bitCount + Int.bitWidth - 1) / Int.bitWidth
} }
@inlinable // FIXME(sil-serialize-all) @inlinable
public // @testable @inline(__always)
init(storage: UnsafeMutablePointer<UInt>, bitCount: Int) { internal init(storage: UnsafeMutablePointer<UInt>, bitCount: Int) {
self.bitCount = bitCount self.bitCount = bitCount
self.values = storage self.values = storage
} }
@inlinable // FIXME(sil-serialize-all) @inlinable
public // @testable internal var numberOfWords: Int {
var numberOfWords: Int { @inline(__always)
return _UnsafeBitMap.sizeInWords(forSizeInBits: bitCount) get {
return _UnsafeBitMap.sizeInWords(forSizeInBits: bitCount)
}
} }
@inlinable // FIXME(sil-serialize-all) @inlinable
public // @testable @inline(__always)
func initializeToZero() { internal func initializeToZero() {
values.initialize(repeating: 0, count: numberOfWords) values.initialize(repeating: 0, count: numberOfWords)
} }
@inlinable // FIXME(sil-serialize-all) @inlinable
public // @testable internal subscript(i: Int) -> Bool {
subscript(i: Int) -> Bool { @inline(__always)
get { get {
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds") _sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
let word = values[_UnsafeBitMap.wordIndex(i)] let word = values[_UnsafeBitMap.wordIndex(i)]
let bit = word & (1 << _UnsafeBitMap.bitIndex(i)) let bit = word & (1 << _UnsafeBitMap.bitIndex(i))
return bit != 0 return bit != 0
} }
@inline(__always)
nonmutating set { nonmutating set {
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds") _sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
let wordIdx = _UnsafeBitMap.wordIndex(i) let wordIdx = _UnsafeBitMap.wordIndex(i)

View File

@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift // RUN: %target-run-stdlib-swift
// REQUIRES: executable_test // REQUIRES: executable_test
import StdlibUnittest import StdlibUnittest