here’s an efficient sort, that ought to avoid reallocation if the argument was uniquely referenced.
@frozen public
struct Main
{
public
static func sort1(items:consuming [Int]) -> [Int]
{
items.sort()
return items
}
here’s the exact same operation, written functionally.
public
static func sort2(items:consuming [Int]) -> [Int]
{
items.sorted()
}
}
probably, you would rather write the second one, and expect the compiler to optimize it into the first one. but that’s not what seems to be taking place.
static output.Main.sort1(items: __owned [Swift.Int]) -> [Swift.Int]:
push r12
sub rsp, 16
mov qword ptr [rsp], rdi
mov rdi, rsp
xor r12d, r12d
call (merged function signature specialization <Arg[0] = [Closure Propagated : generic not re-abstracted specialization <Swift.Array<Swift.Int>> of implicit closure #1 (A.Swift.Sequence.Element, A.Swift.Sequence.Element) -> Swift.Bool in (extension in Swift):Swift.MutableCollection< where A: Swift.RandomAccessCollection, A.Swift.Sequence.Element: Swift.Comparable>.sort() -> (), Argument Types : []> of generic specialization <[Swift.Int]> of (extension in Swift):Swift.MutableCollection< where A: Swift.RandomAccessCollection>.sort(by: (A.Swift.Sequence.Element, A.Swift.Sequence.Element) throws -> Swift.Bool) throws -> ())
mov rax, qword ptr [rsp]
add rsp, 16
pop r12
ret
static output.Main.sort2(items: __owned [Swift.Int]) -> [Swift.Int]:
push r12
push rbx
sub rsp, 24
mov rbx, rdi
mov qword ptr [rsp + 8], rdi
call swift_retain@PLT
lea rdi, [rsp + 8]
xor r12d, r12d
call (merged function signature specialization <Arg[0] = [Closure Propagated : generic not re-abstracted specialization <Swift.Array<Swift.Int>> of implicit closure #1 (A.Swift.Sequence.Element, A.Swift.Sequence.Element) -> Swift.Bool in (extension in Swift):Swift.MutableCollection< where A: Swift.RandomAccessCollection, A.Swift.Sequence.Element: Swift.Comparable>.sort() -> (), Argument Types : []> of generic specialization <[Swift.Int]> of (extension in Swift):Swift.MutableCollection< where A: Swift.RandomAccessCollection>.sort(by: (A.Swift.Sequence.Element, A.Swift.Sequence.Element) throws -> Swift.Bool) throws -> ())
test r12, r12
jne .LBB2_2
mov rdi, rbx
call swift_release@PLT
mov rax, qword ptr [rsp + 8]
add rsp, 24
pop rbx
pop r12
ret