Skip to content

[stdlib] Move _copyBuffer to be an init on _ArrayBufferProtocol #14222

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 4 commits into from
Feb 2, 2018
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
17 changes: 16 additions & 1 deletion stdlib/public/core/ArrayBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ internal protocol _ArrayBufferProtocol
/// Adopt the entire buffer, presenting it at the provided `startIndex`.
init(_buffer: _ContiguousArrayBuffer<Element>, shiftedToStartIndex: Int)

init(copying buffer: Self)

/// Copy the elements in `bounds` from this buffer into uninitialized
/// memory starting at `target`. Return a pointer "past the end" of the
/// just-initialized memory.
Expand Down Expand Up @@ -124,14 +126,27 @@ internal protocol _ArrayBufferProtocol
var endIndex: Int { get }
}

extension _ArrayBufferProtocol {
extension _ArrayBufferProtocol where Indices == CountableRange<Int>{

@_inlineable
@_versioned
internal var subscriptBaseAddress: UnsafeMutablePointer<Element> {
return firstElementAddress
}

// Make sure the compiler does not inline _copyBuffer to reduce code size.
@_inlineable
@inline(never)
@_versioned
internal init(copying buffer: Self) {
let newBuffer = _ContiguousArrayBuffer<Element>(
_uninitializedCount: buffer.count, minimumCapacity: buffer.count)
buffer._copyContents(
subRange: Range(buffer.indices),
initializing: newBuffer.firstElementAddress)
self = Self( _buffer: newBuffer, shiftedToStartIndex: buffer.startIndex)
}

@_inlineable
@_versioned
internal mutating func replaceSubrange<C>(
Expand Down
23 changes: 4 additions & 19 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -835,27 +835,12 @@ extension ${Self} {
return Builtin.unsafeCastToNativeObject(_buffer.owner)
}

// FIXME(ABI)#12 : move to an initializer on _Buffer.
// Make sure the compiler does not inline _copyBuffer to reduce code size.
@inline(never)
@_inlineable
@_versioned
static internal func _copyBuffer(_ buffer: inout _Buffer) {
let newBuffer = _ContiguousArrayBuffer<Element>(
_uninitializedCount: buffer.count, minimumCapacity: buffer.count)
buffer._copyContents(
subRange: buffer.indices,
initializing: newBuffer.firstElementAddress)
buffer = _Buffer(
_buffer: newBuffer, shiftedToStartIndex: buffer.startIndex)
}

@_inlineable
@_versioned
@_semantics("array.make_mutable")
internal mutating func _makeMutableAndUnique() {
if _slowPath(!_buffer.isMutableAndUniquelyReferenced()) {
${Self}._copyBuffer(&_buffer)
_buffer = _Buffer(copying: _buffer)
}
}

Expand All @@ -864,7 +849,7 @@ extension ${Self} {
@_semantics("array.make_mutable")
internal mutating func _makeMutableAndUniqueOrPinned() {
if _slowPath(!_buffer.isMutableAndUniquelyReferencedOrPinned()) {
${Self}._copyBuffer(&_buffer)
_buffer = _Buffer(copying: _buffer)
}
}

Expand Down Expand Up @@ -1543,8 +1528,8 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
@_inlineable
public mutating func popLast() -> Element? {
let newCount = _getCount() - 1
guard newCount >= 0 else { return nil }
_makeUniqueAndReserveCapacityIfNotUnique()
guard !isEmpty else { return nil }
return _customRemoveLast().unsafelyUnwrapped
let pointer = (_buffer.firstElementAddress + newCount)
let element = pointer.move()
_buffer.count = newCount
Expand Down
12 changes: 0 additions & 12 deletions test/SILOptimizer/swap_refcnt.swift

This file was deleted.