Skip to content

Commit e3cbb41

Browse files
committed
[benchmark] Add custom-collection String(decoding:as:)
Add a custom collection String(decoding:as:) benchmark for contiguous and non-contiguous collections (overriding withContiguousStorageIfAvailable)
1 parent 95f0651 commit e3cbb41

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

benchmark/single-source/UTF8Decode.swift

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ public let UTF8Decode = [
1818
name: "UTF8Decode",
1919
runFunction: run_UTF8Decode,
2020
tags: [.validation, .api, .String]),
21+
BenchmarkInfo(
22+
name: "UTF8Decode_InitFromCustom_contiguous",
23+
runFunction: run_UTF8Decode_InitFromCustom_contiguous,
24+
tags: [.validation, .api, .String]),
25+
BenchmarkInfo(
26+
name: "UTF8Decode_InitFromCustom_contiguous_ascii",
27+
runFunction: run_UTF8Decode_InitFromCustom_contiguous_ascii,
28+
tags: [.validation, .api, .String]),
29+
BenchmarkInfo(
30+
name: "UTF8Decode_InitFromCustom_contiguous_ascii_as_ascii",
31+
runFunction: run_UTF8Decode_InitFromCustom_contiguous_ascii_as_ascii,
32+
tags: [.validation, .api, .String]),
33+
BenchmarkInfo(
34+
name: "UTF8Decode_InitFromCustom_noncontiguous",
35+
runFunction: run_UTF8Decode_InitFromCustom_noncontiguous,
36+
tags: [.validation, .api, .String]),
37+
BenchmarkInfo(
38+
name: "UTF8Decode_InitFromCustom_noncontiguous_ascii",
39+
runFunction: run_UTF8Decode_InitFromCustom_noncontiguous_ascii,
40+
tags: [.validation, .api, .String]),
41+
BenchmarkInfo(
42+
name: "UTF8Decode_InitFromCustom_noncontiguous_ascii_as_ascii",
43+
runFunction: run_UTF8Decode_InitFromCustom_noncontiguous_ascii_as_ascii,
44+
tags: [.validation, .api, .String]),
2145
BenchmarkInfo(
2246
name: "UTF8Decode_InitFromData",
2347
runFunction: run_UTF8Decode_InitFromData,
@@ -163,4 +187,80 @@ public func run_UTF8Decode_InitFromBytes_ascii_as_ascii(_ N: Int) {
163187
}
164188
}
165189

190+
struct CustomContiguousCollection: Collection {
191+
let storage: [UInt8]
192+
typealias Index = Int
193+
typealias Element = UInt8
194+
195+
init(_ bytes: [UInt8]) { self.storage = bytes }
196+
subscript(position: Int) -> Element { self.storage[position] }
197+
var startIndex: Index { 0 }
198+
var endIndex: Index { storage.count }
199+
func index(after i: Index) -> Index { i+1 }
200+
201+
func withContiguousStorageIfAvailable<R>(
202+
_ body: (UnsafeBufferPointer<UInt8>) throws -> R
203+
) rethrows -> R? {
204+
try storage.withContiguousStorageIfAvailable(body)
205+
}
206+
}
207+
struct CustomNoncontiguousCollection: Collection {
208+
let storage: [UInt8]
209+
typealias Index = Int
210+
typealias Element = UInt8
211+
212+
init(_ bytes: [UInt8]) { self.storage = bytes }
213+
subscript(position: Int) -> Element { self.storage[position] }
214+
var startIndex: Index { 0 }
215+
var endIndex: Index { storage.count }
216+
func index(after i: Index) -> Index { i+1 }
217+
}
218+
let allStringsCustomContiguous = CustomContiguousCollection(allStringsBytes)
219+
let asciiCustomContiguous = CustomContiguousCollection(Array(ascii.utf8))
220+
let allStringsCustomNoncontiguous = CustomNoncontiguousCollection(allStringsBytes)
221+
let asciiCustomNoncontiguous = CustomNoncontiguousCollection(Array(ascii.utf8))
222+
223+
@inline(never)
224+
public func run_UTF8Decode_InitFromCustom_contiguous(_ N: Int) {
225+
let input = allStringsCustomContiguous
226+
for _ in 0..<200*N {
227+
blackHole(String(decoding: input, as: UTF8.self))
228+
}
229+
}
230+
@inline(never)
231+
public func run_UTF8Decode_InitFromCustom_contiguous_ascii(_ N: Int) {
232+
let input = asciiCustomContiguous
233+
for _ in 0..<1_000*N {
234+
blackHole(String(decoding: input, as: UTF8.self))
235+
}
236+
}
237+
@inline(never)
238+
public func run_UTF8Decode_InitFromCustom_contiguous_ascii_as_ascii(_ N: Int) {
239+
let input = asciiCustomContiguous
240+
for _ in 0..<1_000*N {
241+
blackHole(String(decoding: input, as: Unicode.ASCII.self))
242+
}
243+
}
244+
245+
@inline(never)
246+
public func run_UTF8Decode_InitFromCustom_noncontiguous(_ N: Int) {
247+
let input = allStringsCustomNoncontiguous
248+
for _ in 0..<200*N {
249+
blackHole(String(decoding: input, as: UTF8.self))
250+
}
251+
}
252+
@inline(never)
253+
public func run_UTF8Decode_InitFromCustom_noncontiguous_ascii(_ N: Int) {
254+
let input = asciiCustomNoncontiguous
255+
for _ in 0..<1_000*N {
256+
blackHole(String(decoding: input, as: UTF8.self))
257+
}
258+
}
259+
@inline(never)
260+
public func run_UTF8Decode_InitFromCustom_noncontiguous_ascii_as_ascii(_ N: Int) {
261+
let input = asciiCustomNoncontiguous
262+
for _ in 0..<1_000*N {
263+
blackHole(String(decoding: input, as: Unicode.ASCII.self))
264+
}
265+
}
166266

0 commit comments

Comments
 (0)