@@ -360,11 +360,7 @@ public class KeyPath<Root, Value>: PartialKeyPath<Root> {
360
360
)
361
361
}
362
362
363
- let bufferPtr = buffer. data. baseAddress. _unsafelyUnwrappedUnchecked
364
- let endOfBuffer = MemoryLayout< Int> . _roundingUpToAlignment(
365
- bufferPtr + buffer. data. count
366
- )
367
- let maxSize = endOfBuffer. load ( as: Int . self)
363
+ let maxSize = buffer. maxSize
368
364
let roundedMaxSize = 1 &<< ( Int . bitWidth &- maxSize. leadingZeroBitCount)
369
365
370
366
// 16 is the max alignment allowed on practically every platform we deploy
@@ -542,11 +538,7 @@ public class ReferenceWritableKeyPath<
542
538
543
539
// Project out the reference prefix.
544
540
545
- let bufferPtr = buffer. data. baseAddress. _unsafelyUnwrappedUnchecked
546
- let endOfBuffer = MemoryLayout< Int> . _roundingUpToAlignment(
547
- bufferPtr + buffer. data. count
548
- )
549
- let maxSize = endOfBuffer. load ( as: Int . self)
541
+ let maxSize = buffer. maxSize
550
542
let roundedMaxSize = 1 &<< ( Int . bitWidth &- maxSize. leadingZeroBitCount)
551
543
552
544
// 16 is the max alignment allowed on practically every platform we deploy
@@ -2043,6 +2035,15 @@ internal struct KeyPathBuffer {
2043
2035
return UnsafeMutableRawBufferPointer ( mutating: data)
2044
2036
}
2045
2037
2038
+ internal var maxSize : Int {
2039
+ let bufferPtr = data. baseAddress. _unsafelyUnwrappedUnchecked
2040
+ let endOfBuffer = MemoryLayout< Int> . _roundingUpToAlignment(
2041
+ bufferPtr + data. count
2042
+ )
2043
+
2044
+ return endOfBuffer. load ( as: Int . self)
2045
+ }
2046
+
2046
2047
internal struct Builder {
2047
2048
internal var buffer : UnsafeMutableRawBufferPointer
2048
2049
internal init ( _ buffer: UnsafeMutableRawBufferPointer ) {
@@ -2695,11 +2696,7 @@ internal func _appendingKeyPaths<
2695
2696
2696
2697
let leafHasReferencePrefix = leafBuffer. hasReferencePrefix
2697
2698
2698
- let rootBufferPtr = rootBuffer. data. baseAddress. _unsafelyUnwrappedUnchecked
2699
- let rootEndOfBuffer = MemoryLayout< Int> . _roundingUpToAlignment(
2700
- rootBufferPtr + rootBuffer. data. count
2701
- )
2702
- let rootMaxSize = rootEndOfBuffer. load ( as: Int . self)
2699
+ let rootMaxSize = rootBuffer. maxSize
2703
2700
2704
2701
// Clone the root components into the buffer.
2705
2702
while true {
@@ -2728,11 +2725,7 @@ internal func _appendingKeyPaths<
2728
2725
}
2729
2726
}
2730
2727
2731
- let leafBufferPtr = leafBuffer. data. baseAddress. _unsafelyUnwrappedUnchecked
2732
- let leafEndOfBuffer = MemoryLayout< Int> . _roundingUpToAlignment(
2733
- leafBufferPtr + leafBuffer. data. count
2734
- )
2735
- let leafMaxSize = leafEndOfBuffer. load ( as: Int . self)
2728
+ let leafMaxSize = leafBuffer. maxSize
2736
2729
2737
2730
// Clone the leaf components into the buffer.
2738
2731
while true {
@@ -4283,9 +4276,18 @@ public func _rerootKeyPath<NewRoot>(
4283
4276
4284
4277
let newKpTy = _openExistential ( existingKpTy. rootType, do: openedRoot ( _: ) )
4285
4278
4279
+ // Buffer header + padding (if needed)
4280
+ var capacity = MemoryLayout< Int> . size
4281
+
4282
+ // Size of components
4283
+ capacity += componentSize
4284
+
4285
+ // Max size at the end of the buffer
4286
+ capacity = MemoryLayout< Int> . _roundingUpToAlignment( capacity)
4287
+ capacity += MemoryLayout< Int> . size
4288
+
4286
4289
return newKpTy. _create (
4287
- // This is the buffer header + padding (if needed) + size of components
4288
- capacityInBytes: MemoryLayout < Int > . size + componentSize
4290
+ capacityInBytes: capacity
4289
4291
) {
4290
4292
var builder = KeyPathBuffer . Builder ( $0)
4291
4293
let header = KeyPathBuffer . Header (
@@ -4312,6 +4314,10 @@ public func _rerootKeyPath<NewRoot>(
4312
4314
break
4313
4315
}
4314
4316
}
4317
+
4318
+ // Append the max size at the end of the existing keypath's buffer to the
4319
+ // end of the new keypath's buffer.
4320
+ builder. push ( existingBuffer. maxSize)
4315
4321
}
4316
4322
} as! PartialKeyPath < NewRoot >
4317
4323
}
0 commit comments