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