Skip to content

Commit 1cbc0b3

Browse files
authored
Merge pull request #22648 from ianpartridge/benchmark-str2data
[benchmark] Add dataUsingUTF8Encoding()
2 parents e066d45 + e9ba30a commit 1cbc0b3

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

benchmark/single-source/DataBenchmarks.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,27 @@ public let DataBenchmarks = [
279279
legacyFactor: 50),
280280

281281
BenchmarkInfo(name: "StringToDataEmpty",
282-
runFunction: { data($0*200, from: emptyString) }, tags: d,
282+
runFunction: { dataFromUTF8View($0*200, from: emptyString) }, tags: d,
283283
legacyFactor: 50),
284284
BenchmarkInfo(name: "StringToDataSmall",
285-
runFunction: { data($0*200, from: smallString) }, tags: d,
285+
runFunction: { dataFromUTF8View($0*200, from: smallString) }, tags: d,
286286
legacyFactor: 50),
287287
BenchmarkInfo(name: "StringToDataMedium",
288-
runFunction: { data($0*200, from: mediumString) }, tags: d,
288+
runFunction: { dataFromUTF8View($0*200, from: mediumString) }, tags: d,
289289
legacyFactor: 50),
290290
BenchmarkInfo(name: "StringToDataLargeUnicode",
291-
runFunction: { data($0*200, from: largeUnicodeString) }, tags: d,
291+
runFunction: { dataFromUTF8View($0*200, from: largeUnicodeString) }, tags: d,
292292
legacyFactor: 50),
293293

294+
BenchmarkInfo(name: "String.data.Empty",
295+
runFunction: { dataUsingUTF8Encoding($0*200, from: emptyString) }, tags: d),
296+
BenchmarkInfo(name: "String.data.Small",
297+
runFunction: { dataUsingUTF8Encoding($0*200, from: smallString) }, tags: d),
298+
BenchmarkInfo(name: "String.data.Medium",
299+
runFunction: { dataUsingUTF8Encoding($0*200, from: mediumString) }, tags: d),
300+
BenchmarkInfo(name: "String.data.LargeUnicode",
301+
runFunction: { dataUsingUTF8Encoding($0*200, from: largeUnicodeString) }, tags: d),
302+
294303
BenchmarkInfo(name: "Data.hash.Empty",
295304
runFunction: { hash($0*10_000, data: Data()) }, tags: d),
296305
BenchmarkInfo(name: "Data.hash.Small",
@@ -530,12 +539,19 @@ public func string(_ N: Int, from data: Data) {
530539
}
531540

532541
@inline(never)
533-
public func data(_ N: Int, from string: String) {
542+
public func dataFromUTF8View(_ N: Int, from string: String) {
534543
for _ in 1...N {
535544
blackHole(Data(string.utf8))
536545
}
537546
}
538547

548+
@inline(never)
549+
public func dataUsingUTF8Encoding(_ N: Int, from string: String) {
550+
for _ in 1...N {
551+
autoreleasepool { blackHole(string.data(using: .utf8)) }
552+
}
553+
}
554+
539555
@inline(never)
540556
public func hash(_ N: Int, data: Data) {
541557
var hasher = Hasher()

0 commit comments

Comments
 (0)