Skip to content

Commit a6a2f50

Browse files
Use Int(bitPattern) rather than distance(to) to compute the offset.
1 parent 4fb2f9e commit a6a2f50

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

stdlib/public/core/KeyPath.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,27 +162,23 @@ public class AnyKeyPath: Hashable, _AppendKeyPath {
162162

163163
func getOffsetFromStorage() -> Int? {
164164
let maximumOffsetOn32BitArchitecture = 4094
165-
guard let storage = _kvcKeyPathStringPtr else {
165+
guard _kvcKeyPathStringPtr != nil else {
166166
return nil
167167
}
168168

169-
// TODO: Maybe we can get a pointer's raw bits instead of doing
170-
// a distance calculation. Note: offsetBase can't be unwrapped
171-
// forcefully if its bitPattern is 0x00. Hence the 0x01.
172-
let offsetBase = UnsafePointer<CChar>(bitPattern: 0x01)
173169
let architectureSize = MemoryLayout<Int>.size
174170
if architectureSize == 8 {
175-
let storageDistance = storage.distance(to: offsetBase!) - 2
176-
guard storageDistance >= 0 else {
171+
let offset = -Int(bitPattern: _kvcKeyPathStringPtr) - 1
172+
guard offset >= 0 else {
177173
// This happens to be an actual _kvcKeyPathStringPtr, not an offset, if we get here.
178174
return nil
179175
}
180-
return storageDistance
176+
return offset
181177
}
182178
else {
183-
let storageDistance = offsetBase!.distance(to: storage)
184-
if (storageDistance <= maximumOffsetOn32BitArchitecture) {
185-
return storageDistance
179+
let offset = Int(bitPattern: _kvcKeyPathStringPtr) - 1
180+
if (offset <= maximumOffsetOn32BitArchitecture) {
181+
return offset
186182
}
187183
return nil
188184
}

0 commit comments

Comments
 (0)