Skip to content

Commit 0b10cc5

Browse files
committed
Fix address math, and refactor a little
1 parent 5cae77f commit 0b10cc5

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ extension _ArrayBuffer {
220220
bufferIsUnique: Bool,
221221
minimumCapacity: Int,
222222
growForAppend: Bool,
223-
rangeToNotCopy: Range<Int>
223+
rangeToNotCopy hole: Range<Int>
224224
) -> _ArrayBuffer {
225225
let newCapacity = _growArrayCapacity(oldCapacity: capacity,
226226
minimumCapacity: minimumCapacity,
@@ -230,31 +230,29 @@ extension _ArrayBuffer {
230230

231231
let newBuffer = _ContiguousArrayBuffer<Element>(
232232
_uninitializedCount: c, minimumCapacity: newCapacity)
233-
let dest = newBuffer.mutableFirstElementAddress
234-
let offset = MemoryLayout<Element>.stride * rangeToNotCopy.upperBound
233+
let destStart = newBuffer.mutableFirstElementAddress
234+
let destTailStart = destStart + hole.upperBound
235+
let beforeHole = 0 ..< hole.lowerBound
236+
let afterHole = hole.upperBound ..< c
235237

236238
if bufferIsUnique {
237239
// As an optimization, if the original buffer is unique, we can just move
238240
// the elements instead of copying.
239-
if rangeToNotCopy.lowerBound > 0 {
240-
dest.moveInitialize(from: firstElementAddress,
241-
count: (0 ..< rangeToNotCopy.lowerBound).count)
241+
if !beforeHole.isEmpty {
242+
destStart.moveInitialize(from: firstElementAddress,
243+
count: beforeHole.count)
242244
}
243-
if rangeToNotCopy.upperBound < c {
244-
dest.moveInitialize(from: firstElementAddress + offset,
245-
count: (rangeToNotCopy.upperBound ..< c).count)
245+
if !afterHole.isEmpty {
246+
destStart.moveInitialize(from: destTailStart,
247+
count: afterHole.count)
246248
}
247249
_native.mutableCount = 0
248250
} else {
249-
if rangeToNotCopy.lowerBound > 0 {
250-
_copyContents(
251-
subRange: 0 ..< rangeToNotCopy.lowerBound,
252-
initializing: dest)
251+
if !beforeHole.isEmpty {
252+
_copyContents(subRange: beforeHole, initializing: destStart)
253253
}
254-
if rangeToNotCopy.upperBound < c {
255-
_copyContents(
256-
subRange: rangeToNotCopy.upperBound ..< c,
257-
initializing: dest + offset)
254+
if !afterHole.isEmpty {
255+
_copyContents(subRange: afterHole, initializing: destTailStart)
258256
}
259257
}
260258
return _ArrayBuffer(_buffer: newBuffer, shiftedToStartIndex: 0)

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,24 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
753753
_fixLifetime(owner)
754754
return target + initializedCount
755755
}
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+
756774

757775
@inlinable
758776
internal __consuming func _copyContents(
@@ -893,7 +911,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
893911
bufferIsUnique: Bool,
894912
minimumCapacity: Int,
895913
growForAppend: Bool,
896-
rangeToNotCopy: Range<Int>
914+
rangeToNotCopy hole: Range<Int>
897915
) -> _ContiguousArrayBuffer {
898916
let newCapacity = _growArrayCapacity(oldCapacity: capacity,
899917
minimumCapacity: minimumCapacity,
@@ -903,31 +921,29 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
903921

904922
let newBuffer = _ContiguousArrayBuffer<Element>(
905923
_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
908928

909929
if bufferIsUnique {
910930
// As an optimization, if the original buffer is unique, we can just move
911931
// 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)
915935
}
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)
919939
}
920940
mutableCount = 0
921941
} 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)
926944
}
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)
931947
}
932948
}
933949
return newBuffer

0 commit comments

Comments
 (0)