From ddb6d141b3e4e8726a15e3e9e916ee697bd310db Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 17 Aug 2022 13:52:00 -0700 Subject: [PATCH] [OrderedCollection] Use standard temp allocation facility, if available The 5.6 stdlib introduced a new facility for temporary allocations that promises better performance than direct allocations. Use it when the toolchain version is above 5.6. --- .../OrderedCollections/Utilities/_UnsafeBitset.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/OrderedCollections/Utilities/_UnsafeBitset.swift b/Sources/OrderedCollections/Utilities/_UnsafeBitset.swift index 9470c610..aa10c684 100644 --- a/Sources/OrderedCollections/Utilities/_UnsafeBitset.swift +++ b/Sources/OrderedCollections/Utilities/_UnsafeBitset.swift @@ -73,6 +73,15 @@ extension _UnsafeBitset { run body: (inout _UnsafeBitset) throws -> Void ) rethrows { let wordCount = _UnsafeBitset.wordCount(forCapacity: capacity) +#if compiler(>=5.6) + return try withUnsafeTemporaryAllocation( + of: Word.self, capacity: wordCount + ) { words in + words.initialize(repeating: .empty) + var bitset = Self(words: words, count: 0) + return try body(&bitset) + } +#else if wordCount <= 2 { var buffer: (Word, Word) = (.empty, .empty) return try withUnsafeMutablePointer(to: &buffer) { p in @@ -87,6 +96,7 @@ extension _UnsafeBitset { defer { words.deallocate() } var bitset = _UnsafeBitset(words: words, count: 0) return try body(&bitset) +#endif } }