Skip to content

Commit 73af20f

Browse files
committed
[benchmark] Add StringSplitting and lines benchmarks
Add a new benchmark module StringSplitting for split-like benchmarking. Add lineSink benchmarks, which separates Unicode content by lines and feeds Strings into a sink.
1 parent 362954b commit 73af20f

File tree

3 files changed

+321
-0
lines changed

3 files changed

+321
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ set(SWIFT_BENCH_MODULES
179179
single-source/StringMatch
180180
single-source/StringRemoveDupes
181181
single-source/StringReplaceSubrange
182+
single-source/StringSplitting
182183
single-source/StringSwitch
183184
single-source/StringTests
184185
single-source/StringWalk
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
//===--- StringSplitting.swift --------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 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+
public let StringSplitting = [
16+
BenchmarkInfo(
17+
name: "StringSplitting_LineSink_bytes_alpha",
18+
runFunction: run_LinkSink_bytes_alpha,
19+
tags: [.validation, .String],
20+
setUpFunction: setup),
21+
BenchmarkInfo(
22+
name: "StringSplitting_LineSink_bytes_complex",
23+
runFunction: run_LinkSink_bytes_complex,
24+
tags: [.validation, .String],
25+
setUpFunction: setup),
26+
BenchmarkInfo(
27+
name: "StringSplitting_LineSink_scalars_alpha",
28+
runFunction: run_LinkSink_scalars_alpha,
29+
tags: [.validation, .String],
30+
setUpFunction: setup),
31+
BenchmarkInfo(
32+
name: "StringSplitting_LineSink_scalars_complex",
33+
runFunction: run_LinkSink_scalars_complex,
34+
tags: [.validation, .String],
35+
setUpFunction: setup),
36+
BenchmarkInfo(
37+
name: "StringSplitting_LineSink_characters_alpha",
38+
runFunction: run_LinkSink_characters_alpha,
39+
tags: [.validation, .String],
40+
setUpFunction: setup),
41+
BenchmarkInfo(
42+
name: "StringSplitting_LineSink_characters_complex",
43+
runFunction: run_LinkSink_characters_complex,
44+
tags: [.validation, .String],
45+
setUpFunction: setup),
46+
]
47+
48+
private var c = 0
49+
50+
@inline(never)
51+
public func blackHole(_ x: String) {
52+
c += x.isEmpty ? 1 : 0
53+
}
54+
55+
56+
57+
// Line-sink benchmarks: Implement `lines`-like functionality
58+
enum View {
59+
case character
60+
case scalar
61+
case utf8
62+
}
63+
64+
@inline(__always) // Constant fold the switch away, inline closures
65+
fileprivate func lineSink(_ workload: String, view: View, sink: (String) -> ()) {
66+
switch view {
67+
case .character:
68+
var iter = workload.makeIterator()
69+
_linesByCharacters(source: { iter.next() }, sink: sink)
70+
case .scalar:
71+
var iter = workload.unicodeScalars.makeIterator()
72+
_linesByScalars(source: { iter.next() }, sink: sink)
73+
case .utf8:
74+
var iter = workload.utf8.makeIterator()
75+
_linesByBytes(source: { iter.next() }, sink: sink)
76+
}
77+
}
78+
79+
80+
// Inline always to try to ignore any closure stuff
81+
@inline(__always)
82+
fileprivate func _linesByCharacters(
83+
source characterSource: () throws -> Character?,
84+
sink lineSink: (String) -> ()
85+
) rethrows {
86+
var buffer = ""
87+
func yield() {
88+
lineSink(buffer)
89+
buffer.removeAll(keepingCapacity: true)
90+
}
91+
while let c = try characterSource() {
92+
if c.isNewline {
93+
yield()
94+
} else {
95+
buffer.append(c)
96+
}
97+
}
98+
99+
// Don't emit an empty newline when there is no more content (e.g. end of file)
100+
if !buffer.isEmpty {
101+
yield()
102+
}
103+
}
104+
105+
// Inline always to try to ignore any closure stuff
106+
@inline(__always)
107+
fileprivate func _linesByScalars(
108+
source scalarSource: () throws -> Unicode.Scalar?,
109+
sink lineSink: (String) -> ()
110+
) rethrows {
111+
guard #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) else {
112+
fatalError("unavailable")
113+
}
114+
115+
var buffer: Array<UInt8> = []
116+
func yield() {
117+
lineSink(String(decoding: buffer, as: UTF8.self))
118+
buffer.removeAll(keepingCapacity: true)
119+
}
120+
121+
let _CR = Unicode.Scalar(0x0D)!
122+
let _LF = Unicode.Scalar(0x0A)!
123+
124+
while let first = try scalarSource() {
125+
switch first.value {
126+
case _CR.value:
127+
yield()
128+
guard let second = try scalarSource() else { return }
129+
if second != _LF { buffer.append(contentsOf: second.utf8) }
130+
case 0x000A..<0x000D /* LF ..< CR */: yield()
131+
case 0x0085 /* NEXT LINE (NEL) */: yield()
132+
case 0x2028 /* LINE SEPARATOR */: yield()
133+
case 0x2029 /* PARAGRAPH SEPARATOR */: yield()
134+
default: buffer.append(contentsOf: first.utf8)
135+
}
136+
}
137+
138+
// Don't emit an empty newline when there is no more content (e.g. end of file)
139+
if !buffer.isEmpty {
140+
yield()
141+
}
142+
}
143+
144+
// Inline always to try to ignore any closure stuff
145+
@inline(__always)
146+
fileprivate func _linesByBytes(
147+
source byteSource: () throws -> UInt8?,
148+
sink lineSink: (String) -> ()
149+
) rethrows {
150+
151+
let _CR: UInt8 = 0x0D
152+
let _LF: UInt8 = 0x0A
153+
154+
var buffer: Array<UInt8> = []
155+
func yield() {
156+
lineSink(String(decoding: buffer, as: UTF8.self))
157+
buffer.removeAll(keepingCapacity: true)
158+
}
159+
160+
/*
161+
0D 0A: CR-LF
162+
0A | 0B | 0C | 0D: LF, VT, FF, CR
163+
E2 80 A8: U+2028 (LINE SEPARATOR)
164+
E2 80 A9: U+2029 (PARAGRAPH SEPARATOR)
165+
*/
166+
while let first = try byteSource() {
167+
switch first {
168+
case _CR:
169+
yield()
170+
// Swallow up any subsequent LF
171+
guard let next = try byteSource() else { return }
172+
if next != _LF { buffer.append(next) }
173+
case 0x0A..<0x0D: yield()
174+
case 0xE2:
175+
// Try to read: 80 [A8 | A9].
176+
// If we can't, then we put the byte in the buffer for error correction
177+
guard let next = try byteSource() else {
178+
buffer.append(first)
179+
yield()
180+
return
181+
}
182+
guard next == 0x80 else {
183+
buffer.append(first)
184+
buffer.append(next)
185+
continue
186+
}
187+
guard let fin = try byteSource() else {
188+
buffer.append(first)
189+
buffer.append(next)
190+
yield()
191+
return
192+
}
193+
guard fin == 0xA8 || fin == 0xA9 else {
194+
buffer.append(first)
195+
buffer.append(next)
196+
buffer.append(fin)
197+
continue
198+
}
199+
yield()
200+
default:
201+
buffer.append(first)
202+
}
203+
}
204+
// Don't emit an empty newline when there is no more content (e.g. end of file)
205+
if !buffer.isEmpty {
206+
yield()
207+
}
208+
}
209+
210+
// 1-byte sequences
211+
private let ascii = "Swift is a multi-paradigm, compiled programming language created for iOS, OS X, watchOS, tvOS and Linux development by Apple Inc. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient to erroneous code (\"safer\") than Objective-C and also more concise. It is built with the LLVM compiler framework included in Xcode 6 and later and uses the Objective-C runtime, which allows C, Objective-C, C++ and Swift code to run within a single program."
212+
213+
// 2-byte sequences
214+
private let russian = "Ру́сский язы́к один из восточнославянских языков, национальный язык русского народа."
215+
// 3-byte sequences
216+
private let japanese = "日本語(にほんご、にっぽんご)は、主に日本国内や日本人同士の間で使われている言語である。"
217+
// 4-byte sequences
218+
// Most commonly emoji, which are usually mixed with other text.
219+
private let emoji = "Panda 🐼, Dog 🐶, Cat 🐱, Mouse 🐭."
220+
221+
private let longComplexNewlines: String = {
222+
var longStr = ascii + russian + japanese + emoji + "\n"
223+
var str = longStr
224+
str += longStr.split(separator: " ").joined(separator: "\n")
225+
str += longStr.split(separator: " ").joined(separator: "\r\n")
226+
str += longStr.split(separator: " ").joined(separator: "\r")
227+
str += longStr.split(separator: " ").joined(separator: "\u{0B}")
228+
str += longStr.split(separator: " ").joined(separator: "\u{0C}")
229+
str += longStr.split(separator: " ").joined(separator: "\u{2028}")
230+
str += longStr.split(separator: " ").joined(separator: "\u{2029}")
231+
str += "\n\n"
232+
return str
233+
}()
234+
235+
public func run_LinkSink_bytes_alpha(_ N: Int) {
236+
let str = alphaInteriorNewlines
237+
for _ in 0..<N {
238+
lineSink(str, view: .utf8, sink: blackHole)
239+
}
240+
}
241+
public func run_LinkSink_bytes_complex(_ N: Int) {
242+
let str = longComplexNewlines
243+
for _ in 0..<N {
244+
lineSink(str, view: .utf8, sink: blackHole)
245+
}
246+
}
247+
public func run_LinkSink_scalars_alpha(_ N: Int) {
248+
let str = alphaInteriorNewlines
249+
for _ in 0..<N {
250+
lineSink(str, view: .scalar, sink: blackHole)
251+
}
252+
}
253+
public func run_LinkSink_scalars_complex(_ N: Int) {
254+
let str = longComplexNewlines
255+
for _ in 0..<N {
256+
lineSink(str, view: .utf8, sink: blackHole)
257+
}
258+
}
259+
public func run_LinkSink_characters_alpha(_ N: Int) {
260+
let str = alphaInteriorNewlines
261+
for _ in 0..<N {
262+
lineSink(str, view: .character, sink: blackHole)
263+
}
264+
}
265+
public func run_LinkSink_characters_complex(_ N: Int) {
266+
let str = longComplexNewlines
267+
for _ in 0..<N {
268+
lineSink(str, view: .character, sink: blackHole)
269+
}
270+
}
271+
272+
fileprivate func setup() {
273+
var utf8Alpha: Array<String> = []
274+
lineSink(alphaInteriorNewlines, view: .utf8) { utf8Alpha.append($0) }
275+
276+
var scalarAlpha: Array<String> = []
277+
lineSink(alphaInteriorNewlines, view: .scalar) { scalarAlpha.append($0) }
278+
279+
var characterAlpha: Array<String> = []
280+
lineSink(alphaInteriorNewlines, view: .character) { characterAlpha.append($0) }
281+
282+
CheckResults(utf8Alpha == scalarAlpha)
283+
CheckResults(utf8Alpha == characterAlpha)
284+
285+
var utf8Complex: Array<String> = []
286+
lineSink(longComplexNewlines, view: .utf8) { utf8Complex.append($0) }
287+
288+
var scalarComplex: Array<String> = []
289+
lineSink(longComplexNewlines, view: .scalar) { scalarComplex.append($0) }
290+
291+
var characterComplex: Array<String> = []
292+
lineSink(longComplexNewlines, view: .character) { characterComplex.append($0) }
293+
294+
CheckResults(utf8Complex == scalarComplex)
295+
CheckResults(utf8Complex == characterComplex)
296+
297+
print("preconditions checked")
298+
}
299+
300+
301+
private let alphaInteriorNewlines: String =
302+
"""
303+
abc\(Unicode.Scalar(0x0A)! // LF
304+
)def\(Unicode.Scalar(0x0B)! // VT
305+
)ghi\(Unicode.Scalar(0x0C)! // FF
306+
)jkl\(Unicode.Scalar(0x0D)! // CR
307+
)mno\(Unicode.Scalar(0x0D)! // CR-LF
308+
)pqr\(Unicode.Scalar(0x2028)! // LS
309+
)stu\(Unicode.Scalar(0x2029)! // PS
310+
)vwx
311+
yz
312+
"""
313+
314+

benchmark/utils/main.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ import StringInterpolation
177177
import StringMatch
178178
import StringRemoveDupes
179179
import StringReplaceSubrange
180+
import StringSplitting
180181
import StringSwitch
181182
import StringTests
182183
import StringWalk
@@ -376,6 +377,11 @@ registerBenchmark(StringMatch)
376377
registerBenchmark(StringNormalization)
377378
registerBenchmark(StringRemoveDupes)
378379
registerBenchmark(StringReplaceSubrange)
380+
381+
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {
382+
registerBenchmark(StringSplitting)
383+
}
384+
379385
registerBenchmark(StringSwitch)
380386
registerBenchmark(StringTests)
381387
registerBenchmark(StringWalk)

0 commit comments

Comments
 (0)