From 962d21d92d11ef11164168f8fc26ccaffc753bbd Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Thu, 13 Aug 2026 16:31:44 -0700 Subject: [PATCH] Mark _Word.allBits as @inlinable --- .../Benchmarks/BitArrayBenchmarks.swift | 90 +++++++++++++++++++ .../Sources/Benchmarks/BitsetBenchmarks.swift | 68 ++++++++++++++ Benchmarks/Sources/benchmark-tool/main.swift | 1 + .../UnsafeBitSet/_UnsafeBitSet+_Word.swift | 1 + 4 files changed, 160 insertions(+) create mode 100644 Benchmarks/Sources/Benchmarks/BitArrayBenchmarks.swift diff --git a/Benchmarks/Sources/Benchmarks/BitArrayBenchmarks.swift b/Benchmarks/Sources/Benchmarks/BitArrayBenchmarks.swift new file mode 100644 index 00000000..da0f2dd0 --- /dev/null +++ b/Benchmarks/Sources/Benchmarks/BitArrayBenchmarks.swift @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 - 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +import CollectionsBenchmark +import BitCollections + +extension Benchmark { + + public mutating func addBitArrayBenchmarks() { + self.add( + title: "BitArray fill(with: true)", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + var a = BitArray(repeating: false, count: input) + timer.measure { + a.fill(with: true) + } + blackHole(a) + } + } + + self.add( + title: "BitArray init(repeating: true, count:)", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + timer.measure { + blackHole(BitArray(repeating: true, count: input)) + } + } + } + + self.add( + title: "BitArray append(repeating: true, count:)", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + var a = BitArray() + a.reserveCapacity(input) + timer.measure { + a.append(repeating: true, count: input) + } + blackHole(a) + } + } + + self.add( + title: "BitArray insert(repeating: true, count:at:)", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + var a = BitArray(repeating: false, count: 64) + timer.measure { + a.insert(repeating: true, count: input, at: 32) + } + blackHole(a) + } + } + + self.add( + title: "BitArray truncateOrExtend(toCount:with: true)", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + var a = BitArray() + a.reserveCapacity(input) + timer.measure { + a.truncateOrExtend(toCount: input, with: true) + } + blackHole(a) + } + } + } +} diff --git a/Benchmarks/Sources/Benchmarks/BitsetBenchmarks.swift b/Benchmarks/Sources/Benchmarks/BitsetBenchmarks.swift index 6b17a955..25d13578 100644 --- a/Benchmarks/Sources/Benchmarks/BitsetBenchmarks.swift +++ b/Benchmarks/Sources/Benchmarks/BitsetBenchmarks.swift @@ -383,5 +383,73 @@ extension Benchmark { blackHole(a) } } + + self.add( + title: "BitSet formUnion with Range", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + var a = BitSet(reservingCapacity: input) + a.insert(input - 1) + timer.measure { + a.formUnion(0 ..< input) + } + blackHole(a) + } + } + + self.add( + title: "BitSet init from Range", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + timer.measure { + blackHole(BitSet(0 ..< input)) + } + } + } + + self.add( + title: "BitSet union with Range", + input: Int.self + ) { input in + guard input > 0 else { return nil } + var a = BitSet(reservingCapacity: input) + a.insert(input - 1) + return { timer in + timer.measure { + blackHole(a.union(0 ..< input)) + } + } + } + + self.add( + title: "BitSet.Counted formUnion with Range", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + var a = BitSet.Counted(reservingCapacity: input) + a.insert(input - 1) + timer.measure { + a.formUnion(0 ..< input) + } + blackHole(a) + } + } + + self.add( + title: "BitSet.Counted init from Range", + input: Int.self + ) { input in + guard input > 0 else { return nil } + return { timer in + timer.measure { + blackHole(BitSet.Counted(0 ..< input)) + } + } + } } } diff --git a/Benchmarks/Sources/benchmark-tool/main.swift b/Benchmarks/Sources/benchmark-tool/main.swift index b9b22d12..1234a362 100644 --- a/Benchmarks/Sources/benchmark-tool/main.swift +++ b/Benchmarks/Sources/benchmark-tool/main.swift @@ -43,6 +43,7 @@ benchmark.addSortedDictionaryBenchmarks() #endif benchmark.addHeapBenchmarks() benchmark.addBitSetBenchmarks() +benchmark.addBitArrayBenchmarks() benchmark.addTreeSetBenchmarks() benchmark.addCppBenchmarks() #if EnableRustBenchmarks diff --git a/Sources/InternalCollectionsUtilities/UnsafeBitSet/_UnsafeBitSet+_Word.swift b/Sources/InternalCollectionsUtilities/UnsafeBitSet/_UnsafeBitSet+_Word.swift index f0af791b..1de5a909 100644 --- a/Sources/InternalCollectionsUtilities/UnsafeBitSet/_UnsafeBitSet+_Word.swift +++ b/Sources/InternalCollectionsUtilities/UnsafeBitSet/_UnsafeBitSet+_Word.swift @@ -250,6 +250,7 @@ extension _UnsafeBitSet._Word { Self(0) } + @inlinable @inline(__always) package static var allBits: Self { Self(UInt.max)