Skip to content

Commit a92acc7

Browse files
authored
Merge pull request #38621 from glessard/buffer-benchmarks
2 parents e2985b6 + 210ade3 commit a92acc7

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(SWIFT_BENCH_MODULES
4444
single-source/BitCount
4545
single-source/Breadcrumbs
4646
single-source/BucketSort
47+
single-source/BufferFill
4748
single-source/ByteSwap
4849
single-source/COWTree
4950
single-source/COWArrayGuaranteedParameterOverhead
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//===--- BufferFill.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
public let BufferFill = [
16+
BenchmarkInfo(name: "BufferFillFromSlice",
17+
runFunction: bufferFillFromSliceExecute,
18+
tags: [.validation, .api],
19+
setUpFunction: bufferFillFromSliceSetup,
20+
tearDownFunction: bufferFillFromSliceTeardown),
21+
BenchmarkInfo(name: "RawBufferCopyBytes",
22+
runFunction: rawBufferCopyBytesExecute,
23+
tags: [.validation, .api],
24+
setUpFunction: rawBufferCopyBytesSetup,
25+
tearDownFunction: rawBufferCopyBytesTeardown),
26+
BenchmarkInfo(name: "RawBufferInitializeMemory",
27+
runFunction: rawBufferInitializeMemoryExecute,
28+
tags: [.validation, .api],
29+
setUpFunction: rawBufferInitializeMemorySetup,
30+
tearDownFunction: rawBufferInitializeMemoryTeardown),
31+
]
32+
33+
let c = 100_000
34+
let a = Array(0..<c)
35+
var b: UnsafeMutableBufferPointer<Int> = .init(start: nil, count: 0)
36+
var r = Int.zero
37+
38+
public func bufferFillFromSliceSetup() {
39+
assert(b.baseAddress == nil)
40+
b = .allocate(capacity: c)
41+
r = a.indices.randomElement()!
42+
}
43+
44+
public func bufferFillFromSliceTeardown() {
45+
b.deallocate()
46+
b = .init(start: nil, count: 0)
47+
}
48+
49+
@inline(never)
50+
public func bufferFillFromSliceExecute(n: Int) {
51+
// Measure performance when filling an UnsafeBuffer from a Slice
52+
// of a Collection that supports `withContiguousStorageIfAvailable`
53+
// See: https://bugs.swift.org/browse/SR-14491
54+
55+
for _ in 0..<n {
56+
let slice = Slice(base: a, bounds: a.indices)
57+
var (iterator, copied) = b.initialize(from: slice)
58+
blackHole(b)
59+
CheckResults(copied == a.count && iterator.next() == nil)
60+
}
61+
62+
CheckResults(a[r] == b[r])
63+
}
64+
65+
var ra: [UInt8] = []
66+
var rb = UnsafeMutableRawBufferPointer(start: nil, count: 0)
67+
68+
public func rawBufferCopyBytesSetup() {
69+
assert(rb.baseAddress == nil)
70+
ra = a.withUnsafeBytes(Array.init)
71+
r = ra.indices.randomElement()!
72+
rb = .allocate(byteCount: ra.count, alignment: 1)
73+
}
74+
75+
public func rawBufferCopyBytesTeardown() {
76+
rb.deallocate()
77+
rb = .init(start: nil, count: 0)
78+
ra = []
79+
}
80+
81+
@inline(never)
82+
public func rawBufferCopyBytesExecute(n: Int) {
83+
// Measure performance when copying bytes into an UnsafeRawBuffer
84+
// from a Collection that supports `withContiguousStorageIfAvailable`
85+
// See: https://bugs.swift.org/browse/SR-14886
86+
87+
for _ in 0..<n {
88+
rb.copyBytes(from: ra)
89+
blackHole(rb)
90+
}
91+
92+
CheckResults(ra[r] == rb[r])
93+
}
94+
95+
public func rawBufferInitializeMemorySetup() {
96+
assert(rb.baseAddress == nil)
97+
rb = .allocate(byteCount: a.count * MemoryLayout<Int>.stride, alignment: 1)
98+
r = a.indices.randomElement()!
99+
}
100+
101+
public func rawBufferInitializeMemoryTeardown() {
102+
rb.deallocate()
103+
rb = .init(start: nil, count: 0)
104+
}
105+
106+
@inline(never)
107+
public func rawBufferInitializeMemoryExecute(n: Int) {
108+
// Measure performance when initializing an UnsafeRawBuffer
109+
// from a Collection that supports `withContiguousStorageIfAvailable`
110+
// See: https://bugs.swift.org/browse/SR-14982
111+
112+
for _ in 0..<n {
113+
var (iterator, initialized) = rb.initializeMemory(as: Int.self, from: a)
114+
blackHole(rb)
115+
CheckResults(initialized.count == a.count && iterator.next() == nil)
116+
}
117+
118+
let offset = rb.baseAddress!.advanced(by: r*MemoryLayout<Int>.stride)
119+
let value = offset.load(as: Int.self)
120+
CheckResults(value == a[r])
121+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import BinaryFloatingPointProperties
3232
import BitCount
3333
import Breadcrumbs
3434
import BucketSort
35+
import BufferFill
3536
import ByteSwap
3637
import COWTree
3738
import COWArrayGuaranteedParameterOverhead
@@ -225,6 +226,7 @@ registerBenchmark(BinaryFloatingPointPropertiesUlp)
225226
registerBenchmark(BitCount)
226227
registerBenchmark(Breadcrumbs)
227228
registerBenchmark(BucketSort)
229+
registerBenchmark(BufferFill)
228230
registerBenchmark(ByteSwap)
229231
registerBenchmark(COWTree)
230232
registerBenchmark(COWArrayGuaranteedParameterOverhead)

0 commit comments

Comments
 (0)