Skip to content

Commit c3138e6

Browse files
authored
Merge pull request swiftlang#14901 from milseman/bench_builder
[benchmark] Add interpolation; more stable builder
2 parents 66bb9ce + 959f45e commit c3138e6

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

benchmark/single-source/StringBuilder.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func buildString(_ i: String) -> String {
3939
@inline(never)
4040
public func run_StringBuilder(_ N: Int) {
4141
for _ in 1...5000*N {
42-
_ = buildString(getString("a"))
42+
blackHole(buildString(getString("a")))
4343
}
4444
}
4545

@@ -52,7 +52,7 @@ func addString(_ i: String) -> String {
5252
@inline(never)
5353
public func run_StringAdder(_ N: Int) {
5454
for _ in 1...5000*N {
55-
_ = addString(getString("a"))
55+
blackHole(addString(getString("a")))
5656
}
5757
}
5858

@@ -68,11 +68,10 @@ func buildStringUTF16(_ i: String) -> String {
6868
@inline(never)
6969
public func run_StringUTF16Builder(_ N: Int) {
7070
for _ in 1...5000*N {
71-
_ = buildStringUTF16("a")
71+
blackHole(buildStringUTF16(getString("a")))
7272
}
7373
}
7474

75-
7675
@inline(never)
7776
func buildStringLong(_ i: String) -> String {
7877
var sb = i
@@ -81,12 +80,10 @@ func buildStringLong(_ i: String) -> String {
8180
return sb
8281
}
8382

84-
85-
8683
@inline(never)
8784
public func run_StringBuilderLong(_ N: Int) {
8885
for _ in 1...5000*N {
89-
_ = buildStringLong("👻")
86+
blackHole(buildStringLong(getString("👻")))
9087
}
9188
}
9289

@@ -108,10 +105,16 @@ func buildString(
108105

109106
@inline(never)
110107
public func run_StringWordBuilder(_ N: Int) {
111-
_ = buildString(word: "bumfuzzle", count: 50_000 * N, reservingCapacity: false)
108+
blackHole(buildString(
109+
word: getString("bumfuzzle"),
110+
count: 50_000 * N,
111+
reservingCapacity: false))
112112
}
113113

114114
@inline(never)
115115
public func run_StringWordBuilderReservingCapacity(_ N: Int) {
116-
_ = buildString(word: "bumfuzzle", count: 50_000 * N, reservingCapacity: true)
116+
blackHole(buildString(
117+
word: getString("bumfuzzle"),
118+
count: 50_000 * N,
119+
reservingCapacity: true))
117120
}

benchmark/single-source/StringInterpolation.swift

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public let StringInterpolation = BenchmarkInfo(
1616
name: "StringInterpolation",
1717
runFunction: run_StringInterpolation,
1818
tags: [.validation, .api, .String])
19+
public let StringInterpolationSmall = BenchmarkInfo(
20+
name: "StringInterpolationSmall",
21+
runFunction: run_StringInterpolationSmall,
22+
tags: [.validation, .api, .String])
23+
public let StringInterpolationManySmallSegments = BenchmarkInfo(
24+
name: "StringInterpolationManySmallSegments",
25+
runFunction: run_StringInterpolationManySmallSegments,
26+
tags: [.validation, .api, .String])
1927

2028
class RefTypePrintable : CustomStringConvertible {
2129
var description: String {
@@ -33,15 +41,66 @@ public func run_StringInterpolation(_ N: Int) {
3341
for _ in 1...100*N {
3442
var result = 0
3543
for _ in 1...reps {
36-
let s = "\(anInt) abcdefdhijklmn \(aRefCountedObject) abcdefdhijklmn \u{01}"
44+
let s: String = getString(
45+
"\(anInt) abcdefdhijklmn \(aRefCountedObject) abcdefdhijklmn \u{01}")
3746
let utf16 = s.utf16
3847

3948
// FIXME: if String is not stored as UTF-16 on this platform, then the
4049
// following operation has a non-trivial cost and needs to be replaced
4150
// with an operation on the native storage type.
42-
result = result &+ Int(utf16[utf16.index(before: utf16.endIndex)])
51+
result = result &+ Int(utf16.last!)
52+
blackHole(s)
53+
}
54+
CheckResults(result == refResult)
55+
}
56+
}
57+
58+
@inline(never)
59+
public func run_StringInterpolationSmall(_ N: Int) {
60+
let reps = 100
61+
let refResult = reps
62+
let anInt: Int64 = 0x42
63+
64+
for _ in 1...100*N {
65+
var result = 0
66+
for _ in 1...reps {
67+
let s: String = getString(
68+
"\(getString("int")): \(anInt) \(getString("abc")) \u{01}")
69+
result = result &+ Int(s.utf8.last!)
70+
blackHole(s)
4371
}
4472
CheckResults(result == refResult)
4573
}
4674
}
4775

76+
@inline(never)
77+
public func run_StringInterpolationManySmallSegments(_ N: Int) {
78+
let numHex: UInt = min(UInt(N), 0x0FFF_FFFF_FFFF_FFFF)
79+
let numOct: UInt = min(UInt(N), 0x0000_03FF_FFFF_FFFF)
80+
let numBin: UInt = min(UInt(N), 0x7FFF)
81+
let segments = [
82+
"abc",
83+
String(numHex, radix: 16),
84+
"0123456789",
85+
String(Double.pi/2),
86+
"*barely* small!",
87+
String(numOct, radix: 8),
88+
"",
89+
String(numBin, radix: 2),
90+
]
91+
assert(segments.count == 8)
92+
93+
func getSegment(_ i: Int) -> String {
94+
return getString(segments[i])
95+
}
96+
97+
let reps = 100
98+
for _ in 1...100*N {
99+
for _ in 1...reps {
100+
blackHole("""
101+
\(getSegment(0)) \(getSegment(1))/\(getSegment(2))_\(getSegment(3))
102+
\(getSegment(4)) \(getSegment(5)), \(getSegment(6))~~\(getSegment(7))
103+
""")
104+
}
105+
}
106+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ registerBenchmark(StringComparison)
276276
registerBenchmark(StringEdits)
277277
registerBenchmark(StringEnum)
278278
registerBenchmark(StringInterpolation)
279+
registerBenchmark(StringInterpolationSmall)
280+
registerBenchmark(StringInterpolationManySmallSegments)
279281
registerBenchmark(StringMatch)
280282
registerBenchmark(StringRemoveDupes)
281283
registerBenchmark(StringTests)

0 commit comments

Comments
 (0)