Skip to content

Commit 931f7ba

Browse files
author
Alexis Beingessner
committed
actually used the cached keys/values pointer
1 parent 5e776e5 commit 931f7ba

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,11 +2903,19 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
29032903
bitCount: capacity)
29042904
initializedEntries.initializeToZero()
29052905

2906+
// Compute all the array offsets now, so we don't have to later
2907+
let bitmapAddr = Builtin.projectTailElems(_storage, UInt.self)
2908+
let numWordsForBitmap = _UnsafeBitMap.sizeInWords(forSizeInBits: capacity)
2909+
let keysAddr = Builtin.getTailAddr_Word(bitmapAddr,
2910+
numWordsForBitmap._builtinWordValue, UInt.self, Key.self)
2911+
29062912
// Initialize header
29072913
_body.initializedEntries = initializedEntries
2908-
_body.keys = UnsafeMutableRawPointer(_keys)
2914+
_body.keys = UnsafeMutableRawPointer(keysAddr)
29092915
%if Self == 'Dictionary':
2910-
_body.values = UnsafeMutableRawPointer(_values)
2916+
let valuesAddr = Builtin.getTailAddr_Word(keysAddr,
2917+
capacity._builtinWordValue, Key.self, Value.self)
2918+
_body.values = UnsafeMutableRawPointer(valuesAddr)
29112919
%end
29122920
}
29132921

@@ -2947,27 +2955,15 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
29472955
return _storage._initializedHashtableEntriesBitMapBuffer
29482956
}
29492957

2950-
// This is just a convenience for computing the offset of the keys array to
2951-
// avoid _keys and _values duplicating logic.
2952-
internal var _keysRawAddr: Builtin.RawPointer {
2953-
// Gets the offset for the bitmap, then derives the key offset from that.
2954-
let bitmapAddr = Builtin.projectTailElems(_storage, UInt.self)
2955-
let numWordsForBitmap = _UnsafeBitMap.sizeInWords(forSizeInBits: capacity)
2956-
return Builtin.getTailAddr_Word(bitmapAddr,
2957-
numWordsForBitmap._builtinWordValue, UInt.self, Key.self)
2958-
}
2959-
29602958
// This API is unsafe and needs a `_fixLifetime` in the caller.
2961-
internal var _keys: UnsafeMutablePointer<Key> {
2962-
return UnsafeMutablePointer(_keysRawAddr)
2959+
internal var keys: UnsafeMutablePointer<Key> {
2960+
return _body.keys.assumingMemoryBound(to: Key.self)
29632961
}
29642962

29652963
%if Self == 'Dictionary':
29662964
// This API is unsafe and needs a `_fixLifetime` in the caller.
2967-
internal var _values: UnsafeMutablePointer<Value> {
2968-
let valuesAddr = Builtin.getTailAddr_Word(_keysRawAddr,
2969-
capacity._builtinWordValue, Key.self, Value.self)
2970-
return UnsafeMutablePointer(valuesAddr)
2965+
internal var values: UnsafeMutablePointer<Value> {
2966+
return _body.values.assumingMemoryBound(to: Value.self)
29712967
}
29722968
%end
29732969

@@ -2993,7 +2989,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
29932989
_sanityCheck(isInitializedEntry(at: i))
29942990
defer { _fixLifetime(self) }
29952991

2996-
let res = (_keys + i).pointee
2992+
let res = (keys + i).pointee
29972993
return res
29982994
}
29992995

@@ -3028,9 +3024,9 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
30283024
_sanityCheck(isInitializedEntry(at: i))
30293025
defer { _fixLifetime(self) }
30303026

3031-
(_keys + i).deinitialize()
3027+
(keys + i).deinitialize()
30323028
%if Self == 'Dictionary':
3033-
(_values + i).deinitialize()
3029+
(values + i).deinitialize()
30343030
%end
30353031
_body.initializedEntries[i] = false
30363032
}
@@ -3041,7 +3037,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
30413037
_sanityCheck(!isInitializedEntry(at: i))
30423038
defer { _fixLifetime(self) }
30433039

3044-
(_keys + i).initialize(to: k)
3040+
(keys + i).initialize(to: k)
30453041
_body.initializedEntries[i] = true
30463042
}
30473043

@@ -3051,7 +3047,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
30513047

30523048
defer { _fixLifetime(self) }
30533049

3054-
(_keys + toEntryAt).initialize(to: (from._keys + at).move())
3050+
(keys + toEntryAt).initialize(to: (from.keys + at).move())
30553051
from._body.initializedEntries[at] = false
30563052
_body.initializedEntries[toEntryAt] = true
30573053
}
@@ -3068,7 +3064,7 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
30683064
_sanityCheck(isInitializedEntry(at: i))
30693065
defer { _fixLifetime(self) }
30703066

3071-
(_keys + i).pointee = key
3067+
(keys + i).pointee = key
30723068
}
30733069

30743070
%elif Self == 'Dictionary':
@@ -3077,8 +3073,8 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
30773073
_sanityCheck(!isInitializedEntry(at: i))
30783074
defer { _fixLifetime(self) }
30793075

3080-
(_keys + i).initialize(to: k)
3081-
(_values + i).initialize(to: v)
3076+
(keys + i).initialize(to: k)
3077+
(values + i).initialize(to: v)
30823078
_body.initializedEntries[i] = true
30833079
}
30843080

@@ -3087,8 +3083,8 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
30873083
_sanityCheck(!isInitializedEntry(at: toEntryAt))
30883084
defer { _fixLifetime(self) }
30893085

3090-
(_keys + toEntryAt).initialize(to: (from._keys + at).move())
3091-
(_values + toEntryAt).initialize(to: (from._values + at).move())
3086+
(keys + toEntryAt).initialize(to: (from.keys + at).move())
3087+
(values + toEntryAt).initialize(to: (from.values + at).move())
30923088
from._body.initializedEntries[at] = false
30933089
_body.initializedEntries[toEntryAt] = true
30943090
}
@@ -3099,16 +3095,16 @@ internal struct _Native${Self}Buffer<${TypeParameters}> {
30993095
_sanityCheck(isInitializedEntry(at: i))
31003096
defer { _fixLifetime(self) }
31013097

3102-
return (_values + i).pointee
3098+
return (values + i).pointee
31033099
}
31043100

31053101
@_transparent
31063102
internal func setKey(_ key: Key, value: Value, at i: Int) {
31073103
_sanityCheck(isInitializedEntry(at: i))
31083104
defer { _fixLifetime(self) }
31093105

3110-
(_keys + i).pointee = key
3111-
(_values + i).pointee = value
3106+
(keys + i).pointee = key
3107+
(values + i).pointee = value
31123108
}
31133109

31143110
%end

0 commit comments

Comments
 (0)