Skip to content

[stdlib] fast path for UMBP.initialize<C>(from: C) when C is a Slice #38677

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 5 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions stdlib/public/core/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,13 @@ extension Sequence {
@inlinable
public __consuming func _copyContents(
initializing buffer: UnsafeMutableBufferPointer<Element>
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {
return _copySequenceContents(initializing: buffer)
}

@inlinable
internal __consuming func _copySequenceContents(
initializing buffer: UnsafeMutableBufferPointer<Element>
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {
var it = self.makeIterator()
guard var ptr = buffer.baseAddress else { return (it,buffer.startIndex) }
Expand Down
17 changes: 17 additions & 0 deletions stdlib/public/core/Slice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ extension Slice: Collection {
}
}

extension Slice {
@inlinable
public __consuming func _copyContents(
initializing buffer: UnsafeMutableBufferPointer<Element>
) -> (Iterator, UnsafeMutableBufferPointer<Element>.Index) {
if let (_, copied) = self.withContiguousStorageIfAvailable({
$0._copyContents(initializing: buffer)
}) {
let distance = buffer.distance(from: buffer.startIndex, to: copied)
let i = index(startIndex, offsetBy: distance)
return (Iterator(_elements: self, _position: i), copied)
}

return _copySequenceContents(initializing: buffer)
}
}

extension Slice: BidirectionalCollection where Base: BidirectionalCollection {
@inlinable // generic-performance
public func index(before i: Index) -> Index {
Expand Down
16 changes: 16 additions & 0 deletions validation-test/stdlib/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,22 @@ UnsafeMutableBufferPointerTestSuite.test("withContiguous(Mutable)StorageIfAvaila
expectEqual(result1, result2)
}

UnsafeMutableBufferPointerTestSuite.test("initialize(from: Slice)") {
let o = 91
let c = 1_000
let a = Slice(base: Array(0..<c), bounds: o..<c)

let buffer = UnsafeMutableBufferPointer<Int>.allocate(capacity: c-o)
defer { buffer.deallocate() }

var (iterator, copied) = buffer.initialize(from: a)
expectEqual(iterator.next(), nil)
expectEqual(copied, c-o)

let t = buffer.indices.randomElement()!
expectEqual(a[t+o], buffer[t])
}

UnsafeMutableBufferPointerTestSuite.test("Slice.withContiguousStorageIfAvailable") {
guard #available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) else {
return
Expand Down