stdlib: Use builtin tail-allocated arrays instead of ManagedBuffer to allocate contiguous array buffers.

This reduces the amount of SIL generated for array operations significantly.
The generated code should be mostly the same (modulo different inlining decisions).
This commit is contained in:
Erik Eckstein
2016-09-09 15:10:52 -07:00
parent f83ffe6a8f
commit 1201d96cb2
7 changed files with 115 additions and 138 deletions

View File

@@ -950,12 +950,9 @@ func _allocateUninitializedArray<Element>(_ builtinCount: Builtin.Word)
if count > 0 {
// Doing the actual buffer allocation outside of the array.uninitialized
// semantics function enables stack propagation of the buffer.
let bufferObject = ManagedBufferPointer<_ArrayBody, Element>(
_uncheckedBufferClass: _ContiguousArrayStorage<Element>.self,
minimumCapacity: count)
let bufferObject = Builtin.allocWithTailElems_1(_ContiguousArrayStorage<Element>.self, builtinCount, Element.self)
let (array, ptr) = Array<Element>._adoptStorage(
bufferObject.buffer, count: count)
let (array, ptr) = Array<Element>._adoptStorage(bufferObject, count: count)
return (array, ptr._rawValue)
}
// For an empty array no buffer allocation is needed.
@@ -1106,16 +1103,12 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
@_versioned
@_semantics("array.uninitialized")
internal static func _adoptStorage(
_ storage: AnyObject, count: Int
_ storage: _ContiguousArrayStorage<Element>, count: Int
) -> (Array, UnsafeMutablePointer<Element>) {
_sanityCheck(
storage is _ContiguousArrayStorage<Element>, "Invalid array storage type")
let innerBuffer = _ContiguousArrayBuffer<Element>(
count: count,
storage: unsafeDowncast(
storage, to: _ContiguousArrayStorage<Element>.self))
storage: storage)
return (
Array(