Skip to content

Commit afe43a4

Browse files
authored
Merge pull request #63106 from valeriyvan/Benchmark-UnsafeRawBufferPointer-first
[benchmark] Add benchmark for UnsafeMutableRawBufferPointer firstIndex(of:) and lastIndex(of:)
2 parents 9cf008c + 93c9a64 commit afe43a4

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ set(SWIFT_BENCH_MODULES
4545
single-source/Breadcrumbs
4646
single-source/BucketSort
4747
single-source/BufferFill
48+
single-source/BufferFind
4849
single-source/ByteSwap
4950
single-source/COWTree
5051
single-source/COWArrayGuaranteedParameterOverhead
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===--- BufferFind.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021 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 var benchmarks: [BenchmarkInfo] = [
16+
// size 1000, alignment 0
17+
BenchmarkInfo(
18+
name: "RawBuffer.1000.findFirst",
19+
runFunction: run_BufferFindFirst,
20+
tags: [.validation, .api],
21+
setUpFunction: buffer1000Setup,
22+
tearDownFunction: bufferTeardown
23+
),
24+
BenchmarkInfo(
25+
name: "RawBuffer.1000.findLast",
26+
runFunction: run_BufferFindLast,
27+
tags: [.validation, .api],
28+
setUpFunction: buffer1000Setup,
29+
tearDownFunction: bufferTeardown
30+
),
31+
// size 15, alignment 0
32+
BenchmarkInfo(
33+
name: "RawBuffer.15.findFirst",
34+
runFunction: run_BufferFindFirst,
35+
tags: [.validation, .api],
36+
setUpFunction: buffer15Setup,
37+
tearDownFunction: bufferTeardown
38+
),
39+
BenchmarkInfo(
40+
name: "RawBuffer.15.findLast",
41+
runFunction: run_BufferFindLast,
42+
tags: [.validation, .api],
43+
setUpFunction: buffer15Setup,
44+
tearDownFunction: bufferTeardown
45+
),
46+
// size 7, alignment 0
47+
BenchmarkInfo(
48+
name: "RawBuffer.7.findFirst",
49+
runFunction: run_BufferFindFirst,
50+
tags: [.validation, .api],
51+
setUpFunction: buffer7Setup,
52+
tearDownFunction: bufferTeardown
53+
),
54+
BenchmarkInfo(
55+
name: "RawBuffer.7.findLast",
56+
runFunction: run_BufferFindLast,
57+
tags: [.validation, .api],
58+
setUpFunction: buffer7Setup,
59+
tearDownFunction: bufferTeardown
60+
)
61+
]
62+
63+
var buffer: UnsafeMutableRawBufferPointer = .init(start: nil, count: 0)
64+
65+
func buffer1000Setup() {
66+
bufferSetup(size: 1000, alignment: 0)
67+
}
68+
69+
func buffer15Setup() {
70+
bufferSetup(size: 15, alignment: 0)
71+
}
72+
73+
func buffer7Setup() {
74+
bufferSetup(size: 7, alignment: 0)
75+
}
76+
77+
func bufferTeardown() {
78+
buffer.deallocate()
79+
buffer = .init(start: nil, count: 0)
80+
}
81+
82+
func bufferSetup(size: Int, alignment: Int) {
83+
buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: size, alignment: alignment)
84+
buffer.initializeMemory(as: UInt8.self, repeating: UInt8.min)
85+
buffer[size / 2] = UInt8.max
86+
}
87+
88+
@inline(never)
89+
public func run_BufferFindFirst(n: Int) {
90+
var offset = 0
91+
for _ in 0 ..< n * 10_000 {
92+
if let index = buffer.firstIndex(of: UInt8.max) {
93+
offset += index
94+
}
95+
}
96+
blackHole(offset)
97+
}
98+
99+
@inline(never)
100+
public func run_BufferFindLast(n: Int) {
101+
var offset = 0
102+
for _ in 0 ..< n * 10_000 {
103+
if let index = buffer.lastIndex(of: UInt8.max) {
104+
offset += index
105+
}
106+
}
107+
blackHole(offset)
108+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import BitCount
3333
import Breadcrumbs
3434
import BucketSort
3535
import BufferFill
36+
import BufferFind
3637
import ByteSwap
3738
import COWTree
3839
import COWArrayGuaranteedParameterOverhead
@@ -221,6 +222,7 @@ register(BitCount.benchmarks)
221222
register(Breadcrumbs.benchmarks)
222223
register(BucketSort.benchmarks)
223224
register(BufferFill.benchmarks)
225+
register(BufferFind.benchmarks)
224226
register(ByteSwap.benchmarks)
225227
register(COWTree.benchmarks)
226228
register(COWArrayGuaranteedParameterOverhead.benchmarks)

0 commit comments

Comments
 (0)