Merge pull request #843 from lorentey/immortalize-deque-empty-singleton

Deque: Make the empty singleton immortal
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2026-11-09 14:07:29 -08:00
commit d01e52d04d
3 files changed
+38 -6

No files matched your search

+11 -6
View File
@@ -11,6 +11,10 @@
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
@_fixed_layout
@usableFromInline
internal final class _DequeBuffer<Element>: ManagedBuffer<_DequeBufferHeader, Element> {
@@ -45,9 +49,10 @@ extension _DequeBuffer: CustomStringConvertible {
/// The type-punned empty singleton storage instance.
@usableFromInline
nonisolated(unsafe) internal let _emptyDequeStorage
= _DequeBuffer<Void>.create(
minimumCapacity: 0,
makingHeaderWith: { _ in
_DequeBufferHeader(capacity: 0, count: 0, startSlot: .init(at: 0))
})
nonisolated(unsafe) internal let _emptyDequeStorage = (
_immortalize(
_DequeBuffer<Void>.create(
minimumCapacity: 0,
makingHeaderWith: { _ in
_DequeBufferHeader(capacity: 0, count: 0, startSlot: .init(at: 0))
})))
@@ -35,6 +35,7 @@ target_sources(${module_name} PRIVATE
"BorrowingIteratorProtocol+Extras.swift"
"Debugging.swift"
"Descriptions.swift"
"Immortalize.swift"
"Iterable+CopyContents.swift"
"Iterable+Equality.swift"
"LifetimeOverride.swift"
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 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 SwiftShims
/// Immortalize the given instance, ensuring that it never deinitializes, and
/// rendering its refcount operations noops.
///
/// This intentionally leaks memory, but it avoids a costly compare/exchange
/// loop on retains/releases.
@inlinable
@_alwaysEmitIntoClient
package func _immortalize<T: AnyObject>(_ object: consuming T) -> T {
unsafe _swift_stdlib_immortalize(Unmanaged.passUnretained(object).toOpaque())
return object
}