@@ -46,14 +46,11 @@ import SwiftShims
46
46
// |
47
47
// V _RawNativeDictionaryStorage (a class)
48
48
// +-----------------------------------------------------------+
49
- // | +-------------------------------------------------------+ |
50
- // | |_DictionaryBufferHeader<K,V> (a struct) | |
51
- // | | capacity | |
52
- // | | count | |
53
- // | | ptrToBits | |
54
- // | | ptrToKeys | |
55
- // | | ptrToValues | |
56
- // | +-------------------------------------------------------+ |
49
+ // | capacity |
50
+ // | count |
51
+ // | ptrToBits |
52
+ // | ptrToKeys |
53
+ // | ptrToValues |
57
54
// | [inline array of bits indicating whether bucket is set ] |
58
55
// | [inline array of keys ] |
59
56
// | [inline array of values ] |
@@ -2470,26 +2467,6 @@ collections = [
2470
2467
2471
2468
% for (Self, a_self, TypeParametersDecl, TypeParameters, AnyTypeParameters, Sequence, AnySequenceType) in collections:
2472
2469
2473
- /// Header part of the RawNativeSelfStorage.
2474
- internal struct _${Self}BufferHeader {
2475
-
2476
- internal init(capacity: Int) {
2477
- self.capacity = capacity
2478
- }
2479
-
2480
- internal let capacity: Int
2481
- internal var count: Int = 0
2482
- internal let maxLoadFactorInverse: Double =
2483
- _hashContainerDefaultMaxLoadFactorInverse
2484
-
2485
- // Placeholder values until the struct can be initialized.
2486
- internal var initializedEntries: _UnsafeBitMap! = nil
2487
- internal var keys: UnsafeMutableRawPointer! = nil
2488
- % if Self == 'Dictionary':
2489
- internal var values: UnsafeMutableRawPointer! = nil
2490
- % end
2491
- }
2492
-
2493
2470
/// An instance of this class has all `${Self}` data tail-allocated.
2494
2471
/// Enough bytes are allocated to hold the bitmap for marking valid entries,
2495
2472
/// keys, and values. The data layout starts with the bitmap, followed by the
@@ -2499,13 +2476,22 @@ internal struct _${Self}BufferHeader {
2499
2476
internal class _RawNative${Self}Storage:
2500
2477
_SwiftNativeNS${Self}, _NS${Self}Core
2501
2478
{
2502
- internal typealias BufferHeader = _${Self}BufferHeader
2503
2479
internal typealias RawStorage = _RawNative${Self}Storage
2480
+
2481
+ internal final var capacity: Int
2482
+ internal final var count: Int
2483
+
2484
+ internal final var initializedEntries: _UnsafeBitMap
2485
+ internal final var keys: UnsafeMutableRawPointer
2486
+ % if Self == 'Dictionary':
2487
+ internal final var values: UnsafeMutableRawPointer
2488
+ % end
2504
2489
2505
- internal var _body: BufferHeader
2490
+ internal final let maxLoadFactorInverse: Double =
2491
+ _hashContainerDefaultMaxLoadFactorInverse
2506
2492
2507
2493
// This API is unsafe and needs a `_fixLifetime` in the caller.
2508
- internal
2494
+ internal final
2509
2495
var _initializedHashtableEntriesBitMapBuffer: UnsafeMutablePointer<UInt> {
2510
2496
return UnsafeMutablePointer(Builtin.projectTailElems(self, UInt.self))
2511
2497
}
@@ -2521,16 +2507,21 @@ internal class _RawNative${Self}Storage:
2521
2507
// We set the capacity to 1 so that there's an empty hole to search.
2522
2508
// Any insertion will lead to a real storage being allocated, because
2523
2509
// Dictionary guarantees there's always another empty hole after insertion.
2524
- storage._body = BufferHeader(capacity: 1)
2510
+ storage.capacity = 1
2511
+ storage.count = 0
2512
+
2513
+ // Initialize pointers to garbage; they should never be loaded
2514
+ storage.keys = UnsafeMutableRawPointer(bitPattern: 1)!
2515
+ % if Self == 'Dictionary':
2516
+ storage.values = UnsafeMutableRawPointer(bitPattern: 1)!
2517
+ % end
2525
2518
2526
2519
let initializedEntries = _UnsafeBitMap(
2527
2520
storage: storage._initializedHashtableEntriesBitMapBuffer,
2528
2521
bitCount: 1)
2529
2522
initializedEntries.initializeToZero()
2530
2523
2531
- // We don't bother to initalize the pointers in the header because
2532
- // they should never be accessed on the empty singleton.
2533
- storage._body.initializedEntries = initializedEntries
2524
+ storage.initializedEntries = initializedEntries
2534
2525
return storage
2535
2526
}()
2536
2527
@@ -2558,10 +2549,6 @@ internal class _RawNative${Self}Storage:
2558
2549
return self
2559
2550
}
2560
2551
2561
- var count: Int {
2562
- return 0
2563
- }
2564
-
2565
2552
@objc(countByEnumeratingWithState:objects:count:)
2566
2553
func countByEnumerating(
2567
2554
with state: UnsafeMutablePointer<_SwiftNSFastEnumerationState>,
@@ -2638,12 +2625,9 @@ internal class _TypedNative${Self}Storage<${TypeParameters}> :
2638
2625
%end
2639
2626
2640
2627
deinit {
2641
- let capacity = _body.capacity
2642
- let initializedEntries = _UnsafeBitMap(
2643
- storage: _initializedHashtableEntriesBitMapBuffer, bitCount: capacity)
2644
- let keys = _body.keys.assumingMemoryBound(to: Key.self)
2628
+ let keys = self.keys.assumingMemoryBound(to: Key.self)
2645
2629
%if Self == 'Dictionary':
2646
- let values = _body .values.assumingMemoryBound(to: Value.self)
2630
+ let values = self .values.assumingMemoryBound(to: Value.self)
2647
2631
%end
2648
2632
2649
2633
if !_isPOD(Key.self) {
@@ -2721,10 +2705,6 @@ final internal class _HashableTypedNative${Self}Storage<${TypeParametersDecl}> :
2721
2705
return FullContainer(_nativeBuffer: buffer)
2722
2706
}
2723
2707
2724
- override var count: Int {
2725
- return full.count
2726
- }
2727
-
2728
2708
override func enumerator() -> _NSEnumerator {
2729
2709
return _Native${Self}NSEnumerator<${TypeParameters}>(
2730
2710
Buffer(_storage: self))
@@ -2854,7 +2834,6 @@ final internal class _HashableTypedNative${Self}Storage<${TypeParametersDecl}> :
2854
2834
/// Hashable can be found in an extension.
2855
2835
internal struct _Native${Self}Buffer<${TypeParameters}> {
2856
2836
2857
- internal typealias BufferHeader = _${Self}BufferHeader
2858
2837
internal typealias RawStorage = _RawNative${Self}Storage
2859
2838
internal typealias TypedStorage = _TypedNative${Self}Storage<${TypeParameters}>
2860
2839
internal typealias Buffer = _Native${Self}Buffer<${TypeParameters}>
@@ -2892,9 +2871,8 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
2892
2871
/// Given a capacity and uninitialized RawStorage, completes the
2893
2872
/// initialization and returns a Buffer.
2894
2873
internal init(capacity: Int, storage: RawStorage) {
2895
- // We can initialize by assignment because _HashedContainerBufferHeader
2896
- // is a trivial type, i.e. contains no references.
2897
- storage._body = BufferHeader(capacity: capacity)
2874
+ storage.capacity = capacity
2875
+ storage.count = 0
2898
2876
2899
2877
self.init(_storage: storage)
2900
2878
@@ -2910,44 +2888,34 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
2910
2888
numWordsForBitmap._builtinWordValue, UInt.self, Key.self)
2911
2889
2912
2890
// Initialize header
2913
- _body .initializedEntries = initializedEntries
2914
- _body .keys = UnsafeMutableRawPointer(keysAddr)
2891
+ _storage .initializedEntries = initializedEntries
2892
+ _storage .keys = UnsafeMutableRawPointer(keysAddr)
2915
2893
%if Self == 'Dictionary':
2916
2894
let valuesAddr = Builtin.getTailAddr_Word(keysAddr,
2917
2895
capacity._builtinWordValue, Key.self, Value.self)
2918
- _body .values = UnsafeMutableRawPointer(valuesAddr)
2896
+ _storage .values = UnsafeMutableRawPointer(valuesAddr)
2919
2897
%end
2920
2898
}
2921
2899
2922
- /// A convenience forwarding the _storage's body
2923
- internal var _body: BufferHeader {
2924
- set {
2925
- _storage._body = newValue
2926
- }
2927
- get {
2928
- return _storage._body
2929
- }
2930
- }
2931
-
2932
2900
// Forwarding the individual fields of the storage in various forms
2933
2901
2934
2902
@_versioned
2935
2903
internal var capacity: Int {
2936
- return _assumeNonNegative(_body .capacity)
2904
+ return _assumeNonNegative(_storage .capacity)
2937
2905
}
2938
2906
2939
2907
@_versioned
2940
2908
internal var count: Int {
2941
2909
set {
2942
- _body .count = newValue
2910
+ _storage .count = newValue
2943
2911
}
2944
2912
get {
2945
- return _assumeNonNegative(_body .count)
2913
+ return _assumeNonNegative(_storage .count)
2946
2914
}
2947
2915
}
2948
2916
2949
2917
internal var _maxLoadFactorInverse: Double {
2950
- return _body .maxLoadFactorInverse
2918
+ return _storage .maxLoadFactorInverse
2951
2919
}
2952
2920
2953
2921
internal
@@ -2957,13 +2925,13 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
2957
2925
2958
2926
// This API is unsafe and needs a `_fixLifetime` in the caller.
2959
2927
internal var keys: UnsafeMutablePointer<Key> {
2960
- return _body .keys.assumingMemoryBound(to: Key.self)
2928
+ return _storage .keys.assumingMemoryBound(to: Key.self)
2961
2929
}
2962
2930
2963
2931
%if Self == 'Dictionary':
2964
2932
// This API is unsafe and needs a `_fixLifetime` in the caller.
2965
2933
internal var values: UnsafeMutablePointer<Value> {
2966
- return _body .values.assumingMemoryBound(to: Value.self)
2934
+ return _storage .values.assumingMemoryBound(to: Value.self)
2967
2935
}
2968
2936
%end
2969
2937
@@ -3016,7 +2984,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
3016
2984
_sanityCheck(i >= 0 && i < capacity)
3017
2985
defer { _fixLifetime(self) }
3018
2986
3019
- return _body .initializedEntries[i]
2987
+ return _storage .initializedEntries[i]
3020
2988
}
3021
2989
3022
2990
@_transparent
@@ -3028,7 +2996,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
3028
2996
%if Self == 'Dictionary':
3029
2997
(values + i).deinitialize()
3030
2998
%end
3031
- _body .initializedEntries[i] = false
2999
+ _storage .initializedEntries[i] = false
3032
3000
}
3033
3001
3034
3002
%if Self == 'Set':
@@ -3038,7 +3006,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
3038
3006
defer { _fixLifetime(self) }
3039
3007
3040
3008
(keys + i).initialize(to: k)
3041
- _body .initializedEntries[i] = true
3009
+ _storage .initializedEntries[i] = true
3042
3010
}
3043
3011
3044
3012
@_transparent
@@ -3048,8 +3016,8 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
3048
3016
defer { _fixLifetime(self) }
3049
3017
3050
3018
(keys + toEntryAt).initialize(to: (from.keys + at).move())
3051
- from._body .initializedEntries[at] = false
3052
- _body .initializedEntries[toEntryAt] = true
3019
+ from._storage .initializedEntries[at] = false
3020
+ _storage .initializedEntries[toEntryAt] = true
3053
3021
}
3054
3022
3055
3023
/// Alias for key(at:) in Sets for better code reuse
@@ -3075,7 +3043,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
3075
3043
3076
3044
(keys + i).initialize(to: k)
3077
3045
(values + i).initialize(to: v)
3078
- _body .initializedEntries[i] = true
3046
+ _storage .initializedEntries[i] = true
3079
3047
}
3080
3048
3081
3049
@_transparent
@@ -3085,8 +3053,8 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
3085
3053
3086
3054
(keys + toEntryAt).initialize(to: (from.keys + at).move())
3087
3055
(values + toEntryAt).initialize(to: (from.values + at).move())
3088
- from._body .initializedEntries[at] = false
3089
- _body .initializedEntries[toEntryAt] = true
3056
+ from._storage .initializedEntries[at] = false
3057
+ _storage .initializedEntries[toEntryAt] = true
3090
3058
}
3091
3059
3092
3060
@_versioned
0 commit comments