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

View File

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