@@ -753,6 +753,24 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
753
753
_fixLifetime ( owner)
754
754
return target + initializedCount
755
755
}
756
+
757
+ @_alwaysEmitIntoClient
758
+ @discardableResult
759
+ internal func _moveContents(
760
+ subRange bounds: Range < Int > ,
761
+ initializing target: UnsafeMutablePointer < Element >
762
+ ) -> UnsafeMutablePointer < Element > {
763
+ _internalInvariant ( bounds. lowerBound >= 0 )
764
+ _internalInvariant ( bounds. upperBound >= bounds. lowerBound)
765
+ _internalInvariant ( bounds. upperBound <= count)
766
+
767
+ let initializedCount = bounds. upperBound - bounds. lowerBound
768
+ target. moveInitialize (
769
+ from: firstElementAddress + bounds. lowerBound, count: initializedCount)
770
+ _fixLifetime ( owner)
771
+ return target + initializedCount
772
+ }
773
+
756
774
757
775
@inlinable
758
776
internal __consuming func _copyContents(
@@ -893,7 +911,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
893
911
bufferIsUnique: Bool ,
894
912
minimumCapacity: Int ,
895
913
growForAppend: Bool ,
896
- rangeToNotCopy: Range < Int >
914
+ rangeToNotCopy hole : Range < Int >
897
915
) -> _ContiguousArrayBuffer {
898
916
let newCapacity = _growArrayCapacity ( oldCapacity: capacity,
899
917
minimumCapacity: minimumCapacity,
@@ -903,31 +921,29 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
903
921
904
922
let newBuffer = _ContiguousArrayBuffer < Element > (
905
923
_uninitializedCount: c, minimumCapacity: newCapacity)
906
- let dest = newBuffer. mutableFirstElementAddress
907
- let offset = MemoryLayout < Element > . stride * rangeToNotCopy. upperBound
924
+ let destStart = newBuffer. mutableFirstElementAddress
925
+ let destTailStart = destStart + hole. upperBound
926
+ let beforeHole = 0 ..< hole. lowerBound
927
+ let afterHole = hole. upperBound ..< c
908
928
909
929
if bufferIsUnique {
910
930
// As an optimization, if the original buffer is unique, we can just move
911
931
// the elements instead of copying.
912
- if rangeToNotCopy . lowerBound > 0 {
913
- dest . moveInitialize ( from: firstElementAddress,
914
- count : ( 0 ..< rangeToNotCopy . lowerBound ) . count)
932
+ if !beforeHole . isEmpty {
933
+ destStart . moveInitialize ( from: firstElementAddress,
934
+ count : beforeHole . count)
915
935
}
916
- if rangeToNotCopy . upperBound < c {
917
- dest . moveInitialize ( from: firstElementAddress + offset ,
918
- count : ( rangeToNotCopy . upperBound ..< c ) . count)
936
+ if !afterHole . isEmpty {
937
+ destStart . moveInitialize ( from: destTailStart ,
938
+ count : afterHole . count)
919
939
}
920
940
mutableCount = 0
921
941
} else {
922
- if rangeToNotCopy. lowerBound > 0 {
923
- _copyContents (
924
- subRange: 0 ..< rangeToNotCopy. lowerBound,
925
- initializing: dest)
942
+ if !beforeHole. isEmpty {
943
+ _copyContents ( subRange: beforeHole, initializing: destStart)
926
944
}
927
- if rangeToNotCopy. upperBound < c {
928
- _copyContents (
929
- subRange: rangeToNotCopy. upperBound ..< c,
930
- initializing: dest + offset)
945
+ if !afterHole. isEmpty {
946
+ _copyContents ( subRange: afterHole, initializing: destTailStart)
931
947
}
932
948
}
933
949
return newBuffer
0 commit comments