Skip to content

[stdlib] don't copy array contents on removeAll(keepingCapacity: true) #66085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1378,9 +1378,16 @@ extension Array: RangeReplaceableCollection {
if !keepCapacity {
_buffer = _Buffer()
}
else {
else if _buffer.isMutableAndUniquelyReferenced() {
self.replaceSubrange(indices, with: EmptyCollection())
}
else {
let buffer = _ContiguousArrayBuffer<Element>(
_uninitializedCount: 0,
minimumCapacity: capacity
)
_buffer = _Buffer(_buffer: buffer, shiftedToStartIndex: startIndex)
}
}

//===--- algorithms -----------------------------------------------------===//
Expand Down
13 changes: 8 additions & 5 deletions stdlib/public/core/ArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,7 @@ extension ArraySlice {
func _checkSubscript(
_ index: Int, wasNativeTypeChecked: Bool
) -> _DependenceToken {
#if _runtime(_ObjC)
_buffer._checkValidSubscript(index)
#else
_buffer._checkValidSubscript(index)
#endif
return _DependenceToken()
}

Expand Down Expand Up @@ -1071,9 +1067,16 @@ extension ArraySlice: RangeReplaceableCollection {
if !keepCapacity {
_buffer = _Buffer()
}
else {
else if _buffer.isMutableAndUniquelyReferenced() {
self.replaceSubrange(indices, with: EmptyCollection())
}
else {
let buffer = _ContiguousArrayBuffer<Element>(
_uninitializedCount: 0,
minimumCapacity: capacity
)
_buffer = _Buffer(_buffer: buffer, shiftedToStartIndex: startIndex)
}
}

//===--- algorithms -----------------------------------------------------===//
Expand Down
5 changes: 4 additions & 1 deletion stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,12 @@ extension ContiguousArray: RangeReplaceableCollection {
if !keepCapacity {
_buffer = _Buffer()
}
else {
else if _buffer.isMutableAndUniquelyReferenced() {
self.replaceSubrange(indices, with: EmptyCollection())
}
else {
_buffer = _Buffer(_uninitializedCount: 0, minimumCapacity: capacity)
}
}

//===--- algorithms -----------------------------------------------------===//
Expand Down