Skip to content

Commit 93adef9

Browse files
committed
[benchmark] DataAppendArray: Fix setup overhead
Extract the array creation out of the main workload function of `DataAppendArray` to stabilize it’s performance in -Onone.
1 parent 796cb44 commit 93adef9

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

benchmark/single-source/DataBenchmarks.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public let DataBenchmarks = [
9090
legacyFactor: 20),
9191

9292
BenchmarkInfo(name: "DataAppendArray",
93-
runFunction: { append($0*100, arraySize: 809, to: medium) }, tags: d,
93+
runFunction: { append($0*100, array: array809, to: medium) }, tags: d,
9494
legacyFactor: 100),
9595

9696
BenchmarkInfo(name: "DataReset",
@@ -194,6 +194,8 @@ let small = sampleData(.small)
194194
let medium = sampleData(.medium)
195195
let large = sampleData(.large)
196196

197+
let array809 = byteArray(size: 809)
198+
197199
let repeatElementSeq = { count in
198200
return sequence(state: count) { (i: inout Int) -> UInt8? in
199201
defer { i = i &- 1 }; return i > 0 ? UInt8(0xA0) : nil
@@ -208,10 +210,14 @@ enum SampleKind {
208210
case immutableBacking
209211
}
210212

211-
func fillBuffer(_ buffer: UnsafeMutableBufferPointer<UInt8>) {
212-
for i in buffer.indices {
213-
buffer[i] = UInt8(truncatingIfNeeded: i)
213+
func byteArray(size: Int) -> [UInt8] {
214+
var bytes = [UInt8](repeating: 0, count: size)
215+
bytes.withUnsafeMutableBufferPointer { buffer in
216+
for i in buffer.indices {
217+
buffer[i] = UInt8(truncatingIfNeeded: i)
218+
}
214219
}
220+
return bytes
215221
}
216222

217223
func sampleData(size: Int) -> Data {
@@ -226,11 +232,7 @@ func sampleData(size: Int) -> Data {
226232

227233
func sampleBridgedNSData() -> Data {
228234
let count = 1033
229-
var bytes = [UInt8](repeating: 0, count: count)
230-
bytes.withUnsafeMutableBufferPointer {
231-
fillBuffer($0)
232-
}
233-
let data = NSData(bytes: bytes, length: count)
235+
let data = NSData(bytes: byteArray(size: count), length: count)
234236
return Data(referencing: data)
235237
}
236238

@@ -286,11 +288,7 @@ func append(_ N: Int, bytes count: Int, to data: Data) {
286288
}
287289

288290
@inline(never)
289-
func append(_ N: Int, arraySize: Int, to data: Data) {
290-
var bytes = [UInt8](repeating: 0, count: arraySize)
291-
bytes.withUnsafeMutableBufferPointer {
292-
fillBuffer($0)
293-
}
291+
func append(_ N: Int, array bytes: [UInt8], to data: Data) {
294292
for _ in 1...N {
295293
var copy = data
296294
copy.append(contentsOf: bytes)

0 commit comments

Comments
 (0)