@@ -2826,7 +2826,7 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
2826
2826
//
2827
2827
// Do not access this property directly.
2828
2828
@nonobjc
2829
- internal var _heapStorageBridged_DoNotUse : AnyObject ?
2829
+ private var _bridgedStorage_DoNotUse : AnyObject ?
2830
2830
2831
2831
/// The unbridged elements.
2832
2832
internal var native : _NativeDictionary < Key , Value >
@@ -2836,11 +2836,63 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
2836
2836
super. init ( )
2837
2837
}
2838
2838
2839
- @objc ( copyWithZone: )
2840
- internal func copy( with zone: _SwiftNSZone ? ) -> AnyObject {
2841
- // Instances of this class should be visible outside of standard library as
2842
- // having `NSDictionary` type, which is immutable.
2843
- return self
2839
+ /// Returns the pointer to the stored property, which contains bridged
2840
+ /// Dictionary elements.
2841
+ @nonobjc
2842
+ private var _bridgedStoragePtr : UnsafeMutablePointer < AnyObject ? > {
2843
+ return _getUnsafePointerToStoredProperties ( self ) . assumingMemoryBound (
2844
+ to: Optional< AnyObject> . self )
2845
+ }
2846
+
2847
+ /// The buffer for bridged Dictionary elements, if present.
2848
+ @nonobjc
2849
+ private var _bridgedStorage : _RawNativeDictionaryStorage ? {
2850
+ get {
2851
+ if let ref = _stdlib_atomicLoadARCRef ( object: _bridgedStoragePtr) {
2852
+ return unsafeDowncast ( ref, to: _RawNativeDictionaryStorage. self)
2853
+ }
2854
+ return nil
2855
+ }
2856
+ }
2857
+
2858
+ /// Attach a buffer for bridged Dictionary elements.
2859
+ @nonobjc
2860
+ private func _initializeBridgedStorage( _ storage: AnyObject ) {
2861
+ _stdlib_atomicInitializeARCRef ( object: _bridgedStoragePtr, desired: storage)
2862
+ }
2863
+
2864
+ /// Returns the bridged Dictionary values.
2865
+ internal var bridged : _NativeDictionary < AnyObject , AnyObject > {
2866
+ return _NativeDictionary ( _storage: _bridgedStorage!)
2867
+ }
2868
+
2869
+ @nonobjc
2870
+ internal func bridgeEverything( ) {
2871
+ if _fastPath ( _bridgedStorage != nil ) {
2872
+ return
2873
+ }
2874
+
2875
+ // FIXME: rdar://problem/19486139 (split bridged buffers for keys and values)
2876
+ // We bridge keys and values unconditionally here, even if one of them
2877
+ // actually is verbatim bridgeable (e.g. Dictionary<Int, AnyObject>).
2878
+ // Investigate only allocating the buffer for a Set in this case.
2879
+
2880
+ // Create buffer for bridged data.
2881
+ let bridged = _NativeDictionary < AnyObject , AnyObject > (
2882
+ _exactBucketCount: native. bucketCount,
2883
+ unhashable: ( ) )
2884
+
2885
+ // Bridge everything.
2886
+ for i in 0 ..< native. bucketCount {
2887
+ if native. isInitializedEntry ( at: i) {
2888
+ let key = _bridgeAnythingToObjectiveC ( native. key ( at: i) )
2889
+ let val = _bridgeAnythingToObjectiveC ( native. value ( at: i) )
2890
+ bridged. initializeKey ( key, value: val, at: i)
2891
+ }
2892
+ }
2893
+
2894
+ // Atomically put the bridged elements in place.
2895
+ _initializeBridgedStorage ( bridged. _storage)
2844
2896
}
2845
2897
2846
2898
//
@@ -2859,9 +2911,24 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
2859
2911
_sanityCheckFailure ( " don't call this designated initializer " )
2860
2912
}
2861
2913
2914
+ @objc ( copyWithZone: )
2915
+ internal func copy( with zone: _SwiftNSZone ? ) -> AnyObject {
2916
+ // Instances of this class should be visible outside of standard library as
2917
+ // having `NSDictionary` type, which is immutable.
2918
+ return self
2919
+ }
2920
+
2862
2921
@objc ( objectForKey: )
2863
2922
internal func objectFor( _ aKey: AnyObject ) -> AnyObject ? {
2864
- return bridgingObjectForKey ( aKey)
2923
+ guard let nativeKey = _conditionallyBridgeFromObjectiveC ( aKey, Key . self)
2924
+ else { return nil }
2925
+
2926
+ let ( i, found) = native. _find ( nativeKey)
2927
+ if found {
2928
+ bridgeEverything ( )
2929
+ return bridged. value ( at: i. bucket)
2930
+ }
2931
+ return nil
2865
2932
}
2866
2933
2867
2934
@objc
@@ -2885,18 +2952,18 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
2885
2952
if let unmanagedObjects = _UnmanagedAnyObjectArray ( objects) {
2886
2953
// keys nonnull, objects nonnull
2887
2954
for position in 0 ..< bucketCount {
2888
- if bridgedDictionary . isInitializedEntry ( at: position) {
2889
- unmanagedObjects [ i] = bridgedDictionary . value ( at: position)
2890
- unmanagedKeys [ i] = bridgedDictionary . key ( at: position)
2955
+ if bridged . isInitializedEntry ( at: position) {
2956
+ unmanagedObjects [ i] = bridged . value ( at: position)
2957
+ unmanagedKeys [ i] = bridged . key ( at: position)
2891
2958
i += 1
2892
2959
guard i < count else { break }
2893
2960
}
2894
2961
}
2895
2962
} else {
2896
2963
// keys nonnull, objects null
2897
2964
for position in 0 ..< bucketCount {
2898
- if bridgedDictionary . isInitializedEntry ( at: position) {
2899
- unmanagedKeys [ i] = bridgedDictionary . key ( at: position)
2965
+ if bridged . isInitializedEntry ( at: position) {
2966
+ unmanagedKeys [ i] = bridged . key ( at: position)
2900
2967
i += 1
2901
2968
guard i < count else { break }
2902
2969
}
@@ -2906,8 +2973,8 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
2906
2973
if let unmanagedObjects = _UnmanagedAnyObjectArray ( objects) {
2907
2974
// keys null, objects nonnull
2908
2975
for position in 0 ..< bucketCount {
2909
- if bridgedDictionary . isInitializedEntry ( at: position) {
2910
- unmanagedObjects [ i] = bridgedDictionary . value ( at: position)
2976
+ if bridged . isInitializedEntry ( at: position) {
2977
+ unmanagedObjects [ i] = bridged . value ( at: position)
2911
2978
i += 1
2912
2979
guard i < count else { break }
2913
2980
}
@@ -2926,99 +2993,24 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
2926
2993
let bucketCount = native. bucketCount
2927
2994
var stop : UInt8 = 0
2928
2995
for position in 0 ..< bucketCount {
2929
- if bridgedDictionary . isInitializedEntry ( at: position) {
2930
- block ( Unmanaged . passUnretained ( bridgedDictionary . key ( at: position) ) ,
2931
- Unmanaged . passUnretained ( bridgedDictionary . value ( at: position) ) ,
2996
+ if bridged . isInitializedEntry ( at: position) {
2997
+ block ( Unmanaged . passUnretained ( bridged . key ( at: position) ) ,
2998
+ Unmanaged . passUnretained ( bridged . value ( at: position) ) ,
2932
2999
& stop)
2933
3000
}
2934
3001
if stop != 0 { return }
2935
3002
}
2936
3003
}
2937
3004
2938
- /// Returns the pointer to the stored property, which contains bridged
2939
- /// Dictionary elements.
2940
- @nonobjc
2941
- internal var _heapStorageBridgedPtr : UnsafeMutablePointer < AnyObject ? > {
2942
- return _getUnsafePointerToStoredProperties ( self ) . assumingMemoryBound (
2943
- to: Optional< AnyObject> . self )
2944
- }
2945
-
2946
- /// The buffer for bridged Dictionary elements, if present.
2947
- @nonobjc
2948
- internal var _bridgedStorage : _RawNativeDictionaryStorage ? {
2949
- get {
2950
- if let ref = _stdlib_atomicLoadARCRef ( object: _heapStorageBridgedPtr) {
2951
- return unsafeDowncast ( ref, to: _RawNativeDictionaryStorage. self)
2952
- }
2953
- return nil
2954
- }
2955
- }
2956
-
2957
- /// Attach a buffer for bridged Dictionary elements.
2958
- @nonobjc
2959
- internal func _initializeHeapStorageBridged( _ newStorage: AnyObject ) {
2960
- _stdlib_atomicInitializeARCRef (
2961
- object: _heapStorageBridgedPtr, desired: newStorage)
2962
- }
2963
-
2964
- /// Returns the bridged Dictionary values.
2965
- internal var bridgedDictionary : _NativeDictionary < AnyObject , AnyObject > {
2966
- return _NativeDictionary ( _storage: _bridgedStorage!)
2967
- }
2968
-
2969
- @nonobjc
2970
- internal func bridgeEverything( ) {
2971
- if _fastPath ( _bridgedStorage != nil ) {
2972
- return
2973
- }
2974
-
2975
- // FIXME: rdar://problem/19486139 (split bridged buffers for keys and values)
2976
- // We bridge keys and values unconditionally here, even if one of them
2977
- // actually is verbatim bridgeable (e.g. Dictionary<Int, AnyObject>).
2978
- // Investigate only allocating the buffer for a Set in this case.
2979
-
2980
- // Create buffer for bridged data.
2981
- let bridged = _NativeDictionary < AnyObject , AnyObject > (
2982
- _exactBucketCount: native. bucketCount,
2983
- unhashable: ( ) )
2984
-
2985
- // Bridge everything.
2986
- for i in 0 ..< native. bucketCount {
2987
- if native. isInitializedEntry ( at: i) {
2988
- let key = _bridgeAnythingToObjectiveC ( native. key ( at: i) )
2989
- let val = _bridgeAnythingToObjectiveC ( native. value ( at: i) )
2990
- bridged. initializeKey ( key, value: val, at: i)
2991
- }
2992
- }
2993
-
2994
- // Atomically put the bridged elements in place.
2995
- _initializeHeapStorageBridged ( bridged. _storage)
2996
- }
2997
-
2998
3005
@objc
2999
3006
internal var count : Int {
3000
3007
return native. count
3001
3008
}
3002
3009
3003
- @nonobjc
3004
- internal func bridgingObjectForKey( _ aKey: AnyObject )
3005
- -> AnyObject ? {
3006
- guard let nativeKey = _conditionallyBridgeFromObjectiveC ( aKey, Key . self)
3007
- else { return nil }
3008
-
3009
- let ( i, found) = native. _find (
3010
- nativeKey, startBucket: native. _bucket ( nativeKey) )
3011
- if found {
3012
- bridgeEverything ( )
3013
- return bridgedDictionary. value ( at: i. bucket)
3014
- }
3015
- return nil
3016
- }
3017
-
3018
3010
@objc
3019
3011
internal func enumerator( ) -> _NSEnumerator {
3020
3012
bridgeEverything ( )
3021
- return _SwiftDictionaryNSEnumerator < AnyObject , AnyObject > ( bridgedDictionary )
3013
+ return _SwiftDictionaryNSEnumerator < AnyObject , AnyObject > ( bridged )
3022
3014
}
3023
3015
3024
3016
@objc ( countByEnumeratingWithState: objects: count: )
@@ -3058,7 +3050,7 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
3058
3050
break
3059
3051
}
3060
3052
3061
- let bridgedKey = bridgedDictionary . key ( at: currIndex. bucket)
3053
+ let bridgedKey = bridged . key ( at: currIndex. bucket)
3062
3054
unmanagedObjects [ i] = bridgedKey
3063
3055
stored += 1
3064
3056
native. formIndex ( after: & currIndex)
0 commit comments