Skip to content

Commit 65244f0

Browse files
authored
Merge pull request swiftlang#38827 from glessard/buffer-benchmarks
2 parents 4a86174 + 49ae2dd commit 65244f0

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

benchmark/single-source/BufferFill.swift

Lines changed: 45 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,43 @@ 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(
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

Comments
 (0)