@@ -90,7 +90,7 @@ public let DataBenchmarks = [
90
90
legacyFactor: 20 ) ,
91
91
92
92
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,
94
94
legacyFactor: 100 ) ,
95
95
96
96
BenchmarkInfo ( name: " DataReset " ,
@@ -194,6 +194,8 @@ let small = sampleData(.small)
194
194
let medium = sampleData ( . medium)
195
195
let large = sampleData ( . large)
196
196
197
+ let array809 = byteArray ( size: 809 )
198
+
197
199
let repeatElementSeq = { count in
198
200
return sequence ( state: count) { ( i: inout Int ) -> UInt8 ? in
199
201
defer { i = i &- 1 } ; return i > 0 ? UInt8 ( 0xA0 ) : nil
@@ -208,10 +210,14 @@ enum SampleKind {
208
210
case immutableBacking
209
211
}
210
212
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
+ }
214
219
}
220
+ return bytes
215
221
}
216
222
217
223
func sampleData( size: Int ) -> Data {
@@ -226,11 +232,7 @@ func sampleData(size: Int) -> Data {
226
232
227
233
func sampleBridgedNSData( ) -> Data {
228
234
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)
234
236
return Data ( referencing: data)
235
237
}
236
238
@@ -286,11 +288,7 @@ func append(_ N: Int, bytes count: Int, to data: Data) {
286
288
}
287
289
288
290
@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 ) {
294
292
for _ in 1 ... N {
295
293
var copy = data
296
294
copy. append ( contentsOf: bytes)
0 commit comments