Skip to content

[WIP][stdlib] Finalize Hasher ABI #19685

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion stdlib/public/core/BridgeStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ struct _BridgeStorage<
_sanityCheck(_usesNativeSwiftReferenceCounting(NativeClass.self))
rawValue = Builtin.reinterpretCast(native)
}


@inlinable
@inline(__always)
internal init(taggedPayload: UInt) {
rawValue = _bridgeObject(taggingPayload: taggedPayload)
}

@inlinable // FIXME(sil-serialize-all)
public // @testable
var spareBits: Int {
Expand Down
158 changes: 96 additions & 62 deletions stdlib/public/core/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,13 @@ public struct Dictionary<Key: Hashable, Value> {

@inlinable
internal init(_native: __owned _NativeDictionary<Key, Value>) {
_variant = .native(_native)
_variant = _Variant(native: _native)
}

#if _runtime(_ObjC)
@inlinable
internal init(_cocoa: __owned _CocoaDictionary) {
_variant = .cocoa(_cocoa)
_variant = _Variant(cocoa: _cocoa)
}

/// Private initializer used for bridging.
Expand Down Expand Up @@ -444,7 +444,7 @@ public struct Dictionary<Key: Hashable, Value> {
/// reallocating its storage buffer.
public // FIXME(reserveCapacity): Should be inlinable
init(minimumCapacity: Int) {
_variant = .native(_NativeDictionary(capacity: minimumCapacity))
_variant = _Variant(native: _NativeDictionary(capacity: minimumCapacity))
}

/// Creates a new dictionary from the key-value pairs in the given sequence.
Expand Down Expand Up @@ -1317,7 +1317,7 @@ extension Dictionary {
}
_modify {
var values = Values(_dictionary: self)
_variant = .native(_NativeDictionary())
_variant = _Variant(native: _NativeDictionary())
yield &values
self._variant = values._variant
}
Expand Down Expand Up @@ -1401,10 +1401,22 @@ extension Dictionary {
@inlinable
public static func ==(lhs: Keys, rhs: Keys) -> Bool {
// Equal if the two dictionaries share storage.
if case (.native(let ln), .native(let rn)) = (lhs._variant, rhs._variant),
ln._storage === rn._storage {
if
lhs._variant.isNative,
rhs._variant.isNative,
lhs._variant.asNative._storage === rhs._variant.asNative._storage
{
return true
}
#if _runtime(_ObjC)
if
!lhs._variant.isNative,
!rhs._variant.isNative,
lhs._variant.asCocoa.object === rhs._variant.asCocoa.object
{
return true
}
#endif

// Not equal if the dictionaries are different sizes.
if lhs.count != rhs.count {
Expand Down Expand Up @@ -1514,20 +1526,14 @@ extension Dictionary {
@inlinable
public mutating func swapAt(_ i: Index, _ j: Index) {
guard i != j else { return }
let (a, b): (_HashTable.Bucket, _HashTable.Bucket)
switch _variant {
case .native(let native):
a = native.validatedBucket(for: i)
b = native.validatedBucket(for: j)
#if _runtime(_ObjC)
case .cocoa(let cocoa):
_variant.cocoaPath()
let native = _NativeDictionary<Key, Value>(cocoa)
a = native.validatedBucket(for: i)
b = native.validatedBucket(for: j)
_variant = .native(native)
#endif
if !_variant.isNative {
_variant = .init(native: _NativeDictionary(_variant.asCocoa))
}
#endif
let native = _variant.asNative
let a = native.validatedBucket(for: i)
let b = native.validatedBucket(for: j)
let isUnique = _variant.isUniquelyReferenced()
_variant.asNative.swapValuesAt(a, b, isUnique: isUnique)
}
Expand Down Expand Up @@ -1597,48 +1603,44 @@ extension Dictionary.Values {
extension Dictionary: Equatable where Value: Equatable {
@inlinable
public static func == (lhs: [Key: Value], rhs: [Key: Value]) -> Bool {
switch (lhs._variant, rhs._variant) {
case (.native(let lhsNative), .native(let rhsNative)):
switch (lhs._variant.isNative, rhs._variant.isNative) {
case (true, true):
let lhs = lhs._variant.asNative
let rhs = rhs._variant.asNative

if lhsNative._storage === rhsNative._storage {
return true
}

if lhsNative.count != rhsNative.count {
return false
}
if lhs._storage === rhs._storage { return true }
if lhs.count != rhs.count { return false }

for (k, v) in lhs {
let (bucket, found) = rhsNative.find(k)
guard found, rhsNative.uncheckedValue(at: bucket) == v else {
return false
}
let (bucket, found) = rhs.find(k)
guard found, rhs.uncheckedValue(at: bucket) == v else { return false }
}
return true

#if _runtime(_ObjC)
case (.cocoa(let lhsCocoa), .cocoa(let rhsCocoa)):
return lhsCocoa == rhsCocoa
#if _runtime(_ObjC)
case (false, false):
return lhs._variant.asCocoa == rhs._variant.asCocoa

case (.native(let lhsNative), .cocoa(let rhsCocoa)):
if lhsNative.count != rhsCocoa.count {
return false
}
case (true, false):
let lhs = lhs._variant.asNative
let rhs = rhs._variant.asCocoa

if lhs.count != rhs.count { return false }

defer { _fixLifetime(lhsNative) }
for bucket in lhsNative.hashTable {
let key = lhsNative.uncheckedKey(at: bucket)
let value = lhsNative.uncheckedValue(at: bucket)
defer { _fixLifetime(lhs) }
for bucket in lhs.hashTable {
let key = lhs.uncheckedKey(at: bucket)
let value = lhs.uncheckedValue(at: bucket)
guard
let rhsValue = rhsCocoa.lookup(_bridgeAnythingToObjectiveC(key)),
value == _forceBridgeFromObjectiveC(rhsValue, Value.self)
let cocoaValue = rhs.lookup(_bridgeAnythingToObjectiveC(key)),
value == _forceBridgeFromObjectiveC(cocoaValue, Value.self)
else {
return false
}
}
return true

case (.cocoa, .native):
case (false, true):
return rhs == lhs
#endif
}
Expand Down Expand Up @@ -1851,6 +1853,17 @@ extension Dictionary.Index {
var handle = _asCocoa.handleBitPattern
return handle == 0 || _isUnique_native(&handle)
}

@usableFromInline @_transparent
internal var _isNative: Bool {
switch _variant {
case .native:
return true
case .cocoa:
_cocoaPath()
return false
}
}
#endif

@usableFromInline @_transparent
Expand Down Expand Up @@ -1936,19 +1949,17 @@ extension Dictionary.Index: Comparable {
extension Dictionary.Index: Hashable {
public // FIXME(cocoa-index): Make inlinable
func hash(into hasher: inout Hasher) {
#if _runtime(_ObjC)
switch _variant {
case .native(let nativeIndex):
hasher.combine(0 as UInt8)
hasher.combine(nativeIndex.bucket.offset)
case .cocoa(let cocoaIndex):
_cocoaPath()
#if _runtime(_ObjC)
guard _isNative else {
hasher.combine(1 as UInt8)
hasher.combine(cocoaIndex.storage.currentKeyIndex)
hasher.combine(_asCocoa.storage.currentKeyIndex)
return
}
#else
hasher.combine(0 as UInt8)
hasher.combine(_asNative.bucket.offset)
#endif
#else
hasher.combine(_asNative.bucket.offset)
#endif
}
}

Expand Down Expand Up @@ -2012,6 +2023,17 @@ extension Dictionary.Iterator {
_conditionallyUnreachable()
}
}

@usableFromInline @_transparent
internal var _isNative: Bool {
switch _variant {
case .native:
return true
case .cocoa:
_cocoaPath()
return false
}
}
#endif

@usableFromInline @_transparent
Expand All @@ -2030,6 +2052,21 @@ extension Dictionary.Iterator {
self._variant = .native(newValue)
}
}

#if _runtime(_ObjC)
@usableFromInline @_transparent
internal var _asCocoa: _CocoaDictionary.Iterator {
get {
switch _variant {
case .native:
_sanityCheckFailure("internal error: does not contain a Cocoa index")
case .cocoa(let cocoa):
return cocoa
}
}
}
#endif

}

extension Dictionary.Iterator: IteratorProtocol {
Expand All @@ -2040,20 +2077,17 @@ extension Dictionary.Iterator: IteratorProtocol {
@inlinable
@inline(__always)
public mutating func next() -> (key: Key, value: Value)? {
switch _variant {
case .native:
return _asNative.next()
#if _runtime(_ObjC)
case .cocoa(let cocoaIterator):
_cocoaPath()
if let (cocoaKey, cocoaValue) = cocoaIterator.next() {
guard _isNative else {
if let (cocoaKey, cocoaValue) = _asCocoa.next() {
let nativeKey = _forceBridgeFromObjectiveC(cocoaKey, Key.self)
let nativeValue = _forceBridgeFromObjectiveC(cocoaValue, Value.self)
return (nativeKey, nativeValue)
}
return nil
#endif
}
#endif
return _asNative.next()
}
}

Expand Down
8 changes: 3 additions & 5 deletions stdlib/public/core/DictionaryBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,10 @@ extension _CocoaDictionary.Iterator: IteratorProtocol {
extension Dictionary {
@inlinable
public __consuming func _bridgeToObjectiveCImpl() -> _NSDictionaryCore {
switch _variant {
case .native(let nativeDictionary):
return nativeDictionary.bridged()
case .cocoa(let cocoaDictionary):
return cocoaDictionary.object
guard _variant.isNative else {
return _variant.asCocoa.object
}
return _variant.asNative.bridged()
}

/// Returns the native Dictionary hidden inside this NSDictionary;
Expand Down
14 changes: 7 additions & 7 deletions stdlib/public/core/DictionaryCasting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public func _dictionaryDownCast<BaseKey, BaseValue, DerivedKey, DerivedValue>(
&& _isClassOrObjCExistential(DerivedKey.self)
&& _isClassOrObjCExistential(DerivedValue.self) {

switch source._variant {
case .native(let native):
// Note: it is safe to treat the buffer as immutable here because
// Dictionary will not mutate buffer with reference count greater than 1.
return Dictionary(_immutableCocoaDictionary: native.bridged())
case .cocoa(let cocoa):
return Dictionary(_immutableCocoaDictionary: cocoa.object)
guard source._variant.isNative else {
return Dictionary(
_immutableCocoaDictionary: source._variant.asCocoa.object)
}
// Note: it is safe to treat the buffer as immutable here because
// Dictionary will not mutate buffer with reference count greater than 1.
return Dictionary(
_immutableCocoaDictionary: source._variant.asNative.bridged())
}
#endif
return _dictionaryDownCastConditional(source)!
Expand Down
Loading