Skip to content

Commit e06d03e

Browse files
committed
[benchmark] initialize an UMBP<UInt8> from an URBP
1 parent d6c9bd3 commit e06d03e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

benchmark/single-source/BufferFill.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public let BufferFill = [
2828
tags: [.validation, .api],
2929
setUpFunction: rawBufferInitializeMemorySetup,
3030
tearDownFunction: rawBufferInitializeMemoryTeardown),
31+
BenchmarkInfo(name: "RawBuffer.copyContents",
32+
runFunction: rawBufferCopyContentsExecute,
33+
tags: [.validation, .api],
34+
setUpFunction: rawBufferCopyContentsSetup,
35+
tearDownFunction: rawBufferCopyContentsTeardown),
3136
]
3237

3338
let c = 100_000
@@ -119,3 +124,41 @@ public func rawBufferInitializeMemoryExecute(n: Int) {
119124
let value = offset.load(as: Int.self)
120125
CheckResults(value == a[r])
121126
}
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(byteCount: count, alignment: 8)
135+
a.withUnsafeBytes {
136+
rb.copyMemory(from: $0)
137+
}
138+
r8 = UnsafeRawBufferPointer(rb)
139+
assert(b8.baseAddress == nil)
140+
b8 = .allocate(capacity: rb.count)
141+
r = rb.indices.randomElement()!
142+
}
143+
144+
public func rawBufferCopyContentsTeardown() {
145+
r8.deallocate()
146+
r8 = .init(start: nil, count: 0)
147+
b8.deallocate()
148+
b8 = .init(start: nil, count: 0)
149+
}
150+
151+
@inline(never)
152+
public func rawBufferCopyContentsExecute(n: Int) {
153+
// Measure performance of copying bytes from an
154+
// `UnsafeRawBufferPointer` to an `UnsafeMutableBufferPointer<UInt8>`.
155+
// See: https://bugs.swift.org/browse/SR-9604
156+
157+
for _ in 0..<n {
158+
var (iterator, initialized) = b8.initialize(from: r8)
159+
blackHole(b8)
160+
CheckResults(initialized == r8.count && iterator.next() == nil)
161+
}
162+
163+
CheckResults(b8[r] == r8[r])
164+
}

0 commit comments

Comments
 (0)