Skip to content

Commit 1a59321

Browse files
Merge pull request #5510 from airspeedswift/moar-append-benchmarks
Expand append(contentsOf:) benchmarks
2 parents d58d097 + edbc41d commit 1a59321

File tree

2 files changed

+97
-7
lines changed

2 files changed

+97
-7
lines changed

benchmark/single-source/ArrayAppend.swift

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@ public func run_ArrayAppendReserved(_ N: Int) {
4242
}
4343

4444
// Append a sequence. Length of sequence unknown so
45-
// can't pre-reserve capacity. Should be comparable
46-
// to append single elements.
45+
// can't pre-reserve capacity.
4746
@inline(never)
4847
public func run_ArrayAppendSequence(_ N: Int) {
4948
let seq = stride(from: 0, to: 10_000, by: 1)
5049
for _ in 0..<N {
5150
for _ in 0..<10 {
5251
var nums = [Int]()
53-
for _ in 0..<4 {
52+
for _ in 0..<8 {
5453
nums.append(contentsOf: seq)
5554
}
5655
}
@@ -60,16 +59,102 @@ public func run_ArrayAppendSequence(_ N: Int) {
6059
// Append another array. Length of sequence known so
6160
// can pre-reserve capacity.
6261
@inline(never)
63-
public func run_ArrayAppendArray(_ N: Int) {
62+
public func run_ArrayAppendArrayOfInt(_ N: Int) {
6463
let other = Array(repeating: 1, count: 10_000)
6564
for _ in 0..<N {
6665
for _ in 0..<10 {
6766
var nums = [Int]()
67+
for _ in 0..<8 {
68+
nums += other
69+
}
70+
}
71+
}
72+
}
73+
74+
// Append another array. Length of sequence known so
75+
// can pre-reserve capacity.
76+
@inline(never)
77+
public func run_ArrayAppendStrings(_ N: Int) {
78+
let other = stride(from: 0, to: 10_000, by: 1).map { "\($0)" }
79+
for _ in 0..<N {
80+
for _ in 0..<10 {
81+
var nums = [String]()
82+
// lower inner count due to string slowness
6883
for _ in 0..<4 {
69-
// note, this uses += rather than append(contentsOf:),
70-
// to ensure operator doesn't introduce inefficiency
7184
nums += other
7285
}
7386
}
7487
}
7588
}
89+
90+
struct S<T,U> {
91+
var x: T
92+
var y: U
93+
}
94+
95+
// Append another array. Length of sequence known so
96+
// can pre-reserve capacity.
97+
@inline(never)
98+
public func run_ArrayAppendGenericStructs(_ N: Int) {
99+
let other = Array(repeating: S(x: 3, y: 4.2), count: 10_000)
100+
for _ in 0..<N {
101+
for _ in 0..<10 {
102+
var nums = [S<Int,Double>]()
103+
for _ in 0..<8 {
104+
nums += other
105+
}
106+
}
107+
}
108+
}
109+
110+
// Append another array. Length of sequence known so
111+
// can pre-reserve capacity.
112+
@inline(never)
113+
public func run_ArrayAppendOptionals(_ N: Int) {
114+
let other: [Int?] = Array(repeating: 1, count: 10_000)
115+
116+
for _ in 0..<N {
117+
for _ in 0..<10 {
118+
var nums = [Int?]()
119+
for _ in 0..<8 {
120+
nums += other
121+
}
122+
}
123+
}
124+
}
125+
126+
127+
// Append a lazily-mapped array. Length of sequence known so
128+
// can pre-reserve capacity, but no optimization points used.
129+
@inline(never)
130+
public func run_ArrayAppendLazyMap(_ N: Int) {
131+
let other = Array(0..<10_000).lazy.map { $0 * 2 }
132+
133+
for _ in 0..<N {
134+
for _ in 0..<10 {
135+
var nums = [Int]()
136+
for _ in 0..<8 {
137+
nums += other
138+
}
139+
}
140+
}
141+
}
142+
143+
144+
// Append a Repeat collection. Length of sequence known so
145+
// can pre-reserve capacity, but no optimization points used.
146+
@inline(never)
147+
public func run_ArrayAppendRepeatCol(_ N: Int) {
148+
let other = repeatElement(1, count: 10_000)
149+
150+
for _ in 0..<N {
151+
for _ in 0..<10 {
152+
var nums = [Int]()
153+
for _ in 0..<8 {
154+
nums += other
155+
}
156+
}
157+
}
158+
}
159+
160+

benchmark/utils/main.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ precommitTests = [
107107
"ArrayAppend": run_ArrayAppend,
108108
"ArrayAppendReserved": run_ArrayAppendReserved,
109109
"ArrayAppendSequence": run_ArrayAppendSequence,
110-
"ArrayAppendArray": run_ArrayAppendArray,
110+
"ArrayAppendArrayOfInt": run_ArrayAppendArrayOfInt,
111+
"ArrayAppendStrings": run_ArrayAppendStrings,
112+
"ArrayAppendGenericStructs": run_ArrayAppendGenericStructs,
113+
"ArrayAppendOptionals": run_ArrayAppendOptionals,
114+
"ArrayAppendLazyMap": run_ArrayAppendLazyMap,
115+
"ArrayAppendRepeatCol": run_ArrayAppendRepeatCol,
111116
"ArrayInClass": run_ArrayInClass,
112117
"ArrayLiteral": run_ArrayLiteral,
113118
"ArrayOfGenericPOD": run_ArrayOfGenericPOD,

0 commit comments

Comments
 (0)