@@ -28,6 +28,11 @@ public let BufferFill = [
28
28
tags: [ . validation, . api] ,
29
29
setUpFunction: rawBufferInitializeMemorySetup,
30
30
tearDownFunction: rawBufferInitializeMemoryTeardown) ,
31
+ BenchmarkInfo ( name: " RawBuffer.copyContents " ,
32
+ runFunction: rawBufferCopyContentsExecute,
33
+ tags: [ . validation, . api] ,
34
+ setUpFunction: rawBufferCopyContentsSetup,
35
+ tearDownFunction: rawBufferCopyContentsTeardown) ,
31
36
]
32
37
33
38
let c = 100_000
@@ -119,3 +124,43 @@ public func rawBufferInitializeMemoryExecute(n: Int) {
119
124
let value = offset. load ( as: Int . self)
120
125
CheckResults ( value == a [ r] )
121
126
}
127
+
128
+ var r8 : UnsafeRawBufferPointer = . init( start: nil , count: 0 )
129
+ var b8 : UnsafeMutableBufferPointer < UInt8 > = . init( start: nil , count: 0 )
130
+
131
+ public func rawBufferCopyContentsSetup( ) {
132
+ assert ( r8. baseAddress == nil )
133
+ let count = a. count * MemoryLayout< Int> . stride
134
+ let rb = UnsafeMutableRawBufferPointer . allocate (
135
+ byteCount: count,
136
+ alignment: MemoryLayout< Int> . alignment)
137
+ a. withUnsafeBytes {
138
+ rb. copyMemory ( from: $0)
139
+ }
140
+ r8 = UnsafeRawBufferPointer ( rb)
141
+ assert ( b8. baseAddress == nil )
142
+ b8 = . allocate( capacity: rb. count)
143
+ r = rb. indices. randomElement ( ) !
144
+ }
145
+
146
+ public func rawBufferCopyContentsTeardown( ) {
147
+ r8. deallocate ( )
148
+ r8 = . init( start: nil , count: 0 )
149
+ b8. deallocate ( )
150
+ b8 = . init( start: nil , count: 0 )
151
+ }
152
+
153
+ @inline ( never)
154
+ public func rawBufferCopyContentsExecute( n: Int ) {
155
+ // Measure performance of copying bytes from an
156
+ // `UnsafeRawBufferPointer` to an `UnsafeMutableBufferPointer<UInt8>`.
157
+ // See: https://bugs.swift.org/browse/SR-9604
158
+
159
+ for _ in 0 ..< n {
160
+ var ( iterator, initialized) = b8. initialize ( from: r8)
161
+ blackHole ( b8)
162
+ CheckResults ( initialized == r8. count && iterator. next ( ) == nil )
163
+ }
164
+
165
+ CheckResults ( b8 [ r] == r8 [ r] )
166
+ }
0 commit comments