Skip to content

Expand append(contentsOf:) benchmarks #5510

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
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
97 changes: 91 additions & 6 deletions benchmark/single-source/ArrayAppend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ public func run_ArrayAppendReserved(_ N: Int) {
}

// Append a sequence. Length of sequence unknown so
// can't pre-reserve capacity. Should be comparable
// to append single elements.
// can't pre-reserve capacity.
@inline(never)
public func run_ArrayAppendSequence(_ N: Int) {
let seq = stride(from: 0, to: 10_000, by: 1)
for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
for _ in 0..<4 {
for _ in 0..<8 {
nums.append(contentsOf: seq)
}
}
Expand All @@ -60,16 +59,102 @@ public func run_ArrayAppendSequence(_ N: Int) {
// Append another array. Length of sequence known so
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendArray(_ N: Int) {
public func run_ArrayAppendArrayOfInt(_ N: Int) {
let other = Array(repeating: 1, count: 10_000)
for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
for _ in 0..<8 {
nums += other
}
}
}
}

// Append another array. Length of sequence known so
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendStrings(_ N: Int) {
let other = stride(from: 0, to: 10_000, by: 1).map { "\($0)" }
for _ in 0..<N {
for _ in 0..<10 {
var nums = [String]()
// lower inner count due to string slowness
for _ in 0..<4 {
// note, this uses += rather than append(contentsOf:),
// to ensure operator doesn't introduce inefficiency
nums += other
}
}
}
}

struct S<T,U> {
var x: T
var y: U
}

// Append another array. Length of sequence known so
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendGenericStructs(_ N: Int) {
let other = Array(repeating: S(x: 3, y: 4.2), count: 10_000)
for _ in 0..<N {
for _ in 0..<10 {
var nums = [S<Int,Double>]()
for _ in 0..<8 {
nums += other
}
}
}
}

// Append another array. Length of sequence known so
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendOptionals(_ N: Int) {
let other: [Int?] = Array(repeating: 1, count: 10_000)

for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int?]()
for _ in 0..<8 {
nums += other
}
}
}
}


// Append a lazily-mapped array. Length of sequence known so
// can pre-reserve capacity, but no optimization points used.
@inline(never)
public func run_ArrayAppendLazyMap(_ N: Int) {
let other = Array(0..<10_000).lazy.map { $0 * 2 }

for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
for _ in 0..<8 {
nums += other
}
}
}
}


// Append a Repeat collection. Length of sequence known so
// can pre-reserve capacity, but no optimization points used.
@inline(never)
public func run_ArrayAppendRepeatCol(_ N: Int) {
let other = repeatElement(1, count: 10_000)

for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
for _ in 0..<8 {
nums += other
}
}
}
}


7 changes: 6 additions & 1 deletion benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ precommitTests = [
"ArrayAppend": run_ArrayAppend,
"ArrayAppendReserved": run_ArrayAppendReserved,
"ArrayAppendSequence": run_ArrayAppendSequence,
"ArrayAppendArray": run_ArrayAppendArray,
"ArrayAppendArrayOfInt": run_ArrayAppendArrayOfInt,
"ArrayAppendStrings": run_ArrayAppendStrings,
"ArrayAppendGenericStructs": run_ArrayAppendGenericStructs,
"ArrayAppendOptionals": run_ArrayAppendOptionals,
"ArrayAppendLazyMap": run_ArrayAppendLazyMap,
"ArrayAppendRepeatCol": run_ArrayAppendRepeatCol,
"ArrayInClass": run_ArrayInClass,
"ArrayLiteral": run_ArrayLiteral,
"ArrayOfGenericPOD": run_ArrayOfGenericPOD,
Expand Down