Skip to content

Commit b813b0e

Browse files
authored
Merge pull request swiftlang#65802 from Catfish-Man/contiguous-conundrum-tests
Add a few basic smoke test benchmarks for the default RangeReplaceableCollection append methods
2 parents 187f823 + 4d3c756 commit b813b0e

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ set(SWIFT_BENCH_MODULES
121121
single-source/NSDictionaryCastToSwift
122122
single-source/NSErrorTest
123123
single-source/NSStringConversion
124+
single-source/NaiveRangeReplaceableCollectionConformance
124125
single-source/NibbleSort
125126
single-source/NIOChannelPipeline
126127
single-source/NopDeinit
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//===--- NaiveRangeReplaceableCollectionConformance.swift -----------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 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+
var contiguous:[UInt8] = []
16+
17+
public let benchmarks = [
18+
BenchmarkInfo(name: "NaiveRRC.append.largeContiguous",
19+
runFunction: runAppendLargeContiguous,
20+
tags: [.validation, .api],
21+
setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) }),
22+
BenchmarkInfo(name: "NaiveRRC.append.smallContiguousRepeated",
23+
runFunction: runAppendSmallContiguousRepeatedly,
24+
tags: [.validation, .api],
25+
setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1) }),
26+
BenchmarkInfo(name: "NaiveRRC.init.largeContiguous",
27+
runFunction: runInitLargeContiguous,
28+
tags: [.validation, .api],
29+
setUpFunction: { contiguous = [UInt8](repeating: 7, count: 1_000) })
30+
]
31+
32+
struct NaiveRRC : RangeReplaceableCollection {
33+
34+
var storage:[UInt8] = []
35+
36+
init() {}
37+
38+
func index(after i: Int) -> Int {
39+
i + 1
40+
}
41+
42+
func index(before i: Int) -> Int {
43+
i - 1
44+
}
45+
46+
var startIndex: Int {
47+
0
48+
}
49+
50+
var endIndex: Int {
51+
count
52+
}
53+
54+
var count: Int {
55+
storage.count
56+
}
57+
58+
subscript(position: Int) -> UInt8 {
59+
get {
60+
storage[position]
61+
}
62+
set(newValue) {
63+
storage[position] = newValue
64+
}
65+
}
66+
67+
mutating func replaceSubrange(_ subrange: Range<Int>, with newElements: some Collection<UInt8>) {
68+
storage.replaceSubrange(subrange, with: newElements)
69+
}
70+
71+
mutating func reserveCapacity(_ n: Int) {
72+
storage.reserveCapacity(n)
73+
}
74+
}
75+
76+
@inline(never)
77+
public func runAppendLargeContiguous(N: Int) {
78+
for _ in 1...N {
79+
var rrc = NaiveRRC()
80+
rrc.append(contentsOf: contiguous)
81+
blackHole(rrc)
82+
}
83+
}
84+
85+
@inline(never)
86+
public func runAppendSmallContiguousRepeatedly(N: Int) {
87+
for _ in 1...N {
88+
var rrc = NaiveRRC()
89+
for _ in 1...5_000 {
90+
rrc.append(contentsOf: contiguous)
91+
}
92+
blackHole(rrc)
93+
}
94+
}
95+
96+
@inline(never)
97+
public func runInitLargeContiguous(N: Int) {
98+
for _ in 1...N {
99+
blackHole(NaiveRRC(contiguous))
100+
}
101+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ import Memset
112112
import MirrorTest
113113
import MonteCarloE
114114
import MonteCarloPi
115+
import NaiveRangeReplaceableCollectionConformance
115116
import NibbleSort
116117
import NIOChannelPipeline
117118
import NSDictionaryCastToSwift
@@ -303,6 +304,7 @@ register(Memset.benchmarks)
303304
register(MirrorTest.benchmarks)
304305
register(MonteCarloE.benchmarks)
305306
register(MonteCarloPi.benchmarks)
307+
register(NaiveRangeReplaceableCollectionConformance.benchmarks)
306308
register(NSDictionaryCastToSwift.benchmarks)
307309
register(NSErrorTest.benchmarks)
308310
#if canImport(Darwin)

0 commit comments

Comments
 (0)