Skip to content

Commit bfa0be9

Browse files
committed
Use blackHole to ensure inner loops are not optimized away; switch subscript benchmark to 10000*N style
1 parent 19c6ccf commit bfa0be9

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

benchmark/single-source/DataBenchmarks.swift

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,11 @@ func sampleData(_ type: SampleKind) -> Data {
111111

112112
}
113113

114-
func benchmark_Subscript(_ N: Int, _ data: Data, _ index: Data.Index) {
115-
for _ in 1...N {
116-
_ = data[index]
117-
}
118-
}
119-
120-
func benchmark_Count(_ N: Int, _ data: Data) {
121-
for _ in 1...10000*N {
122-
_ = data.count
123-
}
124-
}
125-
126-
func benchmark_SetCount(_ N: Int, _ data_: Data, _ count: Int) {
127-
var data = data_
128-
let orig = data.count
129-
for _ in 1...10000*N {
130-
data.count = count
131-
data.count = orig
132-
}
133-
}
134-
135114
func benchmark_AccessBytes(_ N: Int, _ data: Data) {
136115
for _ in 1...10000*N {
137116
data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) in
138-
// do nothing here
117+
// Ensure that the compiler does not optimize away this call
118+
blackHole(ptr.pointee)
139119
}
140120
}
141121
}
@@ -218,22 +198,33 @@ func benchmark_AppendData(_ N: Int, _ lhs: Data, _ rhs: Data) {
218198

219199
@inline(never)
220200
public func run_Subscript(_ N: Int) {
221-
let data = sampleData(.veryLarge)
201+
let data = sampleData(.medium)
222202
let index = 521
223-
benchmark_Subscript(N, data, index)
203+
for _ in 1...10000*N {
204+
// Ensure that the compiler does not optimize away this call
205+
blackHole(data[index])
206+
}
224207
}
225208

226209
@inline(never)
227210
public func run_Count(_ N: Int) {
228211
let data = sampleData(.medium)
229-
benchmark_Count(N, data)
212+
for _ in 1...10000*N {
213+
// Ensure that the compiler does not optimize away this call
214+
blackHole(data.count)
215+
}
230216
}
231217

232218
@inline(never)
233219
public func run_SetCount(_ N: Int) {
234220
let data = sampleData(.medium)
235221
let count = data.count + 100
236-
benchmark_SetCount(N, data, count)
222+
var otherData = data
223+
let orig = data.count
224+
for _ in 1...10000*N {
225+
otherData.count = count
226+
otherData.count = orig
227+
}
237228
}
238229

239230
@inline(never)

0 commit comments

Comments
 (0)