Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OrderedCollection] Use standard temp allocation facility, if available #160

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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