Skip to content

Commit

Permalink
Merge pull request #160 from apple/use-temp-alloc-facility
Browse files Browse the repository at this point in the history
[OrderedCollection] Use standard temp allocation facility, if available
  • Loading branch information
lorentey authored Aug 17, 2022
2 parents c5ea141 + ddb6d14 commit 8cc9d22
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/OrderedCollections/Utilities/_UnsafeBitset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -87,6 +96,7 @@ extension _UnsafeBitset {
defer { words.deallocate() }
var bitset = _UnsafeBitset(words: words, count: 0)
return try body(&bitset)
#endif
}
}

Expand Down

0 comments on commit 8cc9d22

Please sign in to comment.