Skip to content
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

Add an overload of append(contentsOf:) on Array that takes a Collection instead of a Sequence, and try using it to accelerate wCSIA-compatible Sequences #77487

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 50 additions & 0 deletions stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,48 @@ extension Array: RangeReplaceableCollection {
_appendElementAssumeUniqueAndCapacity(oldCount, newElement: newElement)
_endMutation()
}

/// Adds the elements of a collection to the end of the array.
///
/// Use this method to append the elements of a collection to the end of this
/// array. This example appends the elements of a `Range<Int>` instance
/// to an array of integers.
///
/// var numbers = [1, 2, 3, 4, 5]
/// numbers.append(contentsOf: 10...15)
/// print(numbers)
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
///
/// - Parameter newElements: The elements to append to the array.
///
/// - Complexity: O(*m*) on average, where *m* is the length of
/// `newElements`, over many calls to `append(contentsOf:)` on the same
/// array.
@_alwaysEmitIntoClient
@_semantics("array.append_contentsOf")
@_effects(notEscaping self.value**)
public mutating func append(contentsOf newElements: __owned some Collection<Element>) {
lorentey marked this conversation as resolved.
Show resolved Hide resolved
let newElementsCount = newElements.count
// This check prevents a data race writing to _swiftEmptyArrayStorage
if newElementsCount == 0 {
return
}
defer {
_endMutation()
}
_reserveCapacityImpl(minimumCapacity: self.count + newElementsCount,
growForAppend: true)

let oldCount = _buffer.mutableCount
let startNewElements = _buffer.mutableFirstElementAddress + oldCount
let buf = UnsafeMutableBufferPointer(
start: startNewElements,
count: newElementsCount)
_debugPrecondition(buf.endIndex <= _buffer.mutableCapacity)
let end = buf.initialize(fromContentsOf: newElements)
_precondition(end == buf.endIndex)
_buffer.mutableCount = _buffer.mutableCount + newElementsCount
}

/// Adds the elements of a sequence to the end of the array.
///
Expand All @@ -1225,6 +1267,14 @@ extension Array: RangeReplaceableCollection {
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
where S.Element == Element {

let wasContiguous = newElements.withContiguousStorageIfAvailable {
Catfish-Man marked this conversation as resolved.
Show resolved Hide resolved
append(contentsOf: $0)
return true
}
if wasContiguous != nil {
return
}

defer {
_endMutation()
}
Expand Down
53 changes: 52 additions & 1 deletion stdlib/public/core/ArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,47 @@ extension ArraySlice: RangeReplaceableCollection {
_appendElementAssumeUniqueAndCapacity(oldCount, newElement: newElement)
_endMutation()
}

/// Adds the elements of a collection to the end of the array.
///
/// Use this method to append the elements of a collection to the end of this
/// array. This example appends the elements of a `Range<Int>` instance
/// to an array of integers.
///
/// var numbers = [1, 2, 3, 4, 5]
/// numbers.append(contentsOf: 10...15)
/// print(numbers)
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
///
/// - Parameter newElements: The elements to append to the array.
///
/// - Complexity: O(*m*) on average, where *m* is the length of
/// `newElements`, over many calls to `append(contentsOf:)` on the same
/// array.
@_alwaysEmitIntoClient
@_semantics("array.append_contentsOf")
public mutating func append(contentsOf newElements: __owned some Collection<Element>) {
let newElementsCount = newElements.count
// This check prevents a data race writing to _swiftEmptyArrayStorage
if newElementsCount == 0 {
return
}
defer {
_endMutation()
}
reserveCapacityForAppend(newElementsCount: newElementsCount)
_ = _buffer.beginCOWMutation()

let oldCount = self.count
let startNewElements = _buffer.firstElementAddress + oldCount
let buf = UnsafeMutableBufferPointer(
start: startNewElements,
count: newElementsCount)
_debugPrecondition(buf.endIndex <= self.capacity)
let end = buf.initialize(fromContentsOf: newElements)
_precondition(end == buf.endIndex)
_buffer.count += newElementsCount
}

/// Adds the elements of a sequence to the end of the array.
///
Expand All @@ -947,6 +988,17 @@ extension ArraySlice: RangeReplaceableCollection {
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
where S.Element == Element {

let wasContiguous = newElements.withContiguousStorageIfAvailable {
append(contentsOf: $0)
return true
}
if wasContiguous != nil {
return
}

defer {
_endMutation()
}
let newElementsCount = newElements.underestimatedCount
reserveCapacityForAppend(newElementsCount: newElementsCount)
_ = _buffer.beginCOWMutation()
Expand Down Expand Up @@ -975,7 +1027,6 @@ extension ArraySlice: RangeReplaceableCollection {
// append them in slow sequence-only mode
_buffer._arrayAppendSequence(IteratorSequence(remainder))
}
_endMutation()
}

@inlinable
Expand Down
52 changes: 52 additions & 0 deletions stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,50 @@ extension ContiguousArray: RangeReplaceableCollection {
_appendElementAssumeUniqueAndCapacity(oldCount, newElement: newElement)
_endMutation()
}

/// Adds the elements of a sequence to the end of the array.
///
/// Use this method to append the elements of a sequence to the end of this
/// array. This example appends the elements of a `Range<Int>` instance
/// to an array of integers.
///
/// var numbers = [1, 2, 3, 4, 5]
/// numbers.append(contentsOf: 10...15)
/// print(numbers)
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
///
/// - Parameter newElements: The elements to append to the array.
///
/// - Complexity: O(*m*) on average, where *m* is the length of
/// `newElements`, over many calls to `append(contentsOf:)` on the same
/// array.
@_alwaysEmitIntoClient
@_semantics("array.append_contentsOf")
public mutating func append(
contentsOf newElements: __owned some Collection<Element>
) {
let newElementsCount = newElements.count
// This check prevents a data race writing to _swiftEmptyArrayStorage
if newElementsCount == 0 {
return
}
defer {
_endMutation()
}

_reserveCapacityImpl(minimumCapacity: self.count + newElementsCount,
growForAppend: true)

let oldCount = _buffer.mutableCount
let startNewElements = _buffer.mutableFirstElementAddress + oldCount
let buf = UnsafeMutableBufferPointer(
start: startNewElements,
count: newElementsCount)
_debugPrecondition(buf.endIndex <= _buffer.mutableCapacity)
let end = buf.initialize(fromContentsOf: newElements)
_precondition(end == buf.endIndex)
_buffer.count += newElementsCount
}

/// Adds the elements of a sequence to the end of the array.
///
Expand All @@ -836,6 +880,14 @@ extension ContiguousArray: RangeReplaceableCollection {
@_semantics("array.append_contentsOf")
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
where S.Element == Element {

let wasContiguous = newElements.withContiguousStorageIfAvailable {
append(contentsOf: $0)
return true
}
if wasContiguous != nil {
return
}

defer {
_endMutation()
Expand Down
6 changes: 6 additions & 0 deletions stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,12 @@ extension Sequence {
internal func _copySequenceToContiguousArray<
S: Sequence
>(_ source: S) -> ContiguousArray<S.Element> {
let contigArray = source.withContiguousStorageIfAvailable {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is new btw

_copyCollectionToContiguousArray($0)
}
if let contigArray {
return contigArray
}
let initialCapacity = source.underestimatedCount
var builder =
_UnsafePartiallyInitializedContiguousArrayBuffer<S.Element>(
Expand Down
6 changes: 3 additions & 3 deletions test/SILOptimizer/array_contentof_opt.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -O -sil-verify-all -emit-sil -Xllvm '-sil-inline-never-functions=$sSa6appendyy' -Xllvm -sil-inline-never-function='$sSa6append10contentsOfyqd__n_t7ElementQyd__RszSTRd__lFSi_SaySiGTg5' %s | %FileCheck %s
// RUN: %target-swift-frontend -O -sil-verify-all -emit-sil -Xllvm '-sil-inline-never-functions=$sSa6appendyy' -Xllvm -sil-inline-never-function='$sSa6append10contentsOfyqd__n_t7ElementQyd__RszSlRd__lFSi_SaySiGTg5' %s | %FileCheck %s
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib

// This is an end-to-end test of the Array.append(contentsOf:) ->
Expand Down Expand Up @@ -63,7 +63,7 @@ public func dontPropagateContiguousArray(_ a: inout ContiguousArray<UInt8>) {

// Check if the specialized Array.append<A>(contentsOf:) is reasonably optimized for Array<Int>.

// CHECK-LABEL: sil shared {{.*}}@$sSa6append10contentsOfyqd__n_t7ElementQyd__RszSTRd__lFSi_SaySiGTg5
// CHECK-LABEL: sil shared {{.*}}@$sSa6append10contentsOfyqd__n_t7ElementQyd__RszSlRd__lFSi_SaySiGTg5

// There should only be a single call to _createNewBuffer or reserveCapacityForAppend/reserveCapacityImpl.

Expand All @@ -72,5 +72,5 @@ public func dontPropagateContiguousArray(_ a: inout ContiguousArray<UInt8>) {
// CHECK: apply [[F]]
// CHECK-NOT: apply

// CHECK-LABEL: } // end sil function '$sSa6append10contentsOfyqd__n_t7ElementQyd__RszSTRd__lFSi_SaySiGTg5
// CHECK-LABEL: } // end sil function '$sSa6append10contentsOfyqd__n_t7ElementQyd__RszSlRd__lFSi_SaySiGTg5