@@ -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 " ,
@@ -203,6 +203,8 @@ let small = sampleData(.small)
203
203
let medium = sampleData ( . medium)
204
204
let large = sampleData ( . large)
205
205
206
+ let array809 = byteArray ( size: 809 )
207
+
206
208
let repeatElementSeq = { count in
207
209
return sequence ( state: count) { ( i: inout Int ) -> UInt8 ? in
208
210
defer { i = i &- 1 } ; return i > 0 ? UInt8 ( 0xA0 ) : nil
@@ -217,10 +219,14 @@ enum SampleKind {
217
219
case immutableBacking
218
220
}
219
221
220
- func fillBuffer( _ buffer: UnsafeMutableBufferPointer < UInt8 > ) {
221
- for i in buffer. indices {
222
- buffer [ i] = UInt8 ( truncatingIfNeeded: i)
222
+ func byteArray( size: Int ) -> [ UInt8 ] {
223
+ var bytes = [ UInt8] ( repeating: 0 , count: size)
224
+ bytes. withUnsafeMutableBufferPointer { buffer in
225
+ for i in buffer. indices {
226
+ buffer [ i] = UInt8 ( truncatingIfNeeded: i)
227
+ }
223
228
}
229
+ return bytes
224
230
}
225
231
226
232
func sampleData( size: Int ) -> Data {
@@ -235,11 +241,7 @@ func sampleData(size: Int) -> Data {
235
241
236
242
func sampleBridgedNSData( ) -> Data {
237
243
let count = 1033
238
- var bytes = [ UInt8] ( repeating: 0 , count: count)
239
- bytes. withUnsafeMutableBufferPointer {
240
- fillBuffer ( $0)
241
- }
242
- let data = NSData ( bytes: bytes, length: count)
244
+ let data = NSData ( bytes: byteArray ( size: count) , length: count)
243
245
return Data ( referencing: data)
244
246
}
245
247
@@ -295,11 +297,7 @@ func append(_ N: Int, bytes count: Int, to data: Data) {
295
297
}
296
298
297
299
@inline ( never)
298
- func append( _ N: Int , arraySize: Int , to data: Data ) {
299
- var bytes = [ UInt8] ( repeating: 0 , count: arraySize)
300
- bytes. withUnsafeMutableBufferPointer {
301
- fillBuffer ( $0)
302
- }
300
+ func append( _ N: Int , array bytes: [ UInt8 ] , to data: Data ) {
303
301
for _ in 1 ... N {
304
302
var copy = data
305
303
copy. append ( contentsOf: bytes)
0 commit comments