Skip to content

Commit 1d3b0da

Browse files
authored
Merge pull request swiftlang#63078 from apple/egorzhdan/cxx-bench-strings
[cxx-interop] Add benchmark for conversion between C++ and Swift strings
2 parents d2ad33a + d288b16 commit 1d3b0da

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ set(SWIFT_BENCH_MODULES
202202
single-source/XorLoop
203203
cxx-source/CreateObjects
204204
cxx-source/CxxSetToCollection
205+
cxx-source/CxxStringConversion
205206
cxx-source/CxxVectorSum
206207
cxx-source/ReadAccessor
207208
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===--- CxxStringConversion.swift ----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 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+
import CxxStdlibPerformance
15+
import CxxStdlib
16+
17+
let cxxStringSize = 1_000_000
18+
let swiftStringSize = 25_000
19+
20+
var cxxString: std.string? = nil
21+
var swiftString: String? = nil
22+
23+
public let benchmarks = [
24+
BenchmarkInfo(
25+
name: "CxxStringConversion.swift.to.cxx",
26+
runFunction: run_swiftToCxx,
27+
tags: [.validation, .bridging, .cxxInterop],
28+
setUpFunction: {
29+
swiftString = String(repeating: "abc012", count: swiftStringSize / 6)
30+
}),
31+
BenchmarkInfo(
32+
name: "CxxStringConversion.cxx.to.swift",
33+
runFunction: run_cxxToSwift,
34+
tags: [.validation, .bridging, .cxxInterop],
35+
setUpFunction: {
36+
cxxString = std.string()
37+
for i in 0..<cxxStringSize {
38+
let char = std.string.value_type(65 + i % 10) // latin letters A-J
39+
cxxString!.push_back(char)
40+
}
41+
}),
42+
]
43+
44+
@inline(never)
45+
public func run_swiftToCxx(_ n: Int) {
46+
let str = swiftString!
47+
for _ in 0..<n {
48+
let x = std.string(str)
49+
blackHole(x)
50+
}
51+
}
52+
53+
@inline(never)
54+
public func run_cxxToSwift(_ n: Int) {
55+
let str = cxxString!
56+
for _ in 0..<n {
57+
let x = String(cxxString: str)
58+
blackHole(x)
59+
}
60+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import CodableTest
5252
import Combos
5353
import CreateObjects
5454
import CxxSetToCollection
55+
import CxxStringConversion
5556
import CxxVectorSum
5657
import DataBenchmarks
5758
import DeadArray
@@ -239,6 +240,7 @@ register(Combos.benchmarks)
239240
register(ClassArrayGetter.benchmarks)
240241
register(CreateObjects.benchmarks)
241242
register(CxxSetToCollection.benchmarks)
243+
register(CxxStringConversion.benchmarks)
242244
register(CxxVectorSum.benchmarks)
243245
register(DataBenchmarks.benchmarks)
244246
register(DeadArray.benchmarks)

0 commit comments

Comments
 (0)