1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
//===--- UTF8Decode.swift -------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import TestsUtils
import Foundation
public let benchmarks = [
BenchmarkInfo(
name: "UTF8Decode",
runFunction: run_UTF8Decode,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromCustom_contiguous",
runFunction: run_UTF8Decode_InitFromCustom_contiguous,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromCustom_contiguous_ascii",
runFunction: run_UTF8Decode_InitFromCustom_contiguous_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromCustom_contiguous_ascii_as_ascii",
runFunction: run_UTF8Decode_InitFromCustom_contiguous_ascii_as_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromCustom_noncontiguous",
runFunction: run_UTF8Decode_InitFromCustom_noncontiguous,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromCustom_noncontiguous_ascii",
runFunction: run_UTF8Decode_InitFromCustom_noncontiguous_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromCustom_noncontiguous_ascii_as_ascii",
runFunction: run_UTF8Decode_InitFromCustom_noncontiguous_ascii_as_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromData",
runFunction: run_UTF8Decode_InitFromData,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitDecoding",
runFunction: run_UTF8Decode_InitDecoding,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromBytes",
runFunction: run_UTF8Decode_InitFromBytes,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromData_ascii",
runFunction: run_UTF8Decode_InitFromData_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitDecoding_ascii",
runFunction: run_UTF8Decode_InitDecoding_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromBytes_ascii",
runFunction: run_UTF8Decode_InitFromBytes_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromData_ascii_as_ascii",
runFunction: run_UTF8Decode_InitFromData_ascii_as_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitDecoding_ascii_as_ascii",
runFunction: run_UTF8Decode_InitDecoding_ascii_as_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromBytes_ascii_as_ascii",
runFunction: run_UTF8Decode_InitFromBytes_ascii_as_ascii,
tags: [.validation, .api, .String]),
]
// 1-byte sequences
// This test case is the longest as it's the most performance sensitive.
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."
let asciiBytes: [UInt8] = Array(ascii.utf8)
let asciiData: Data = Data(asciiBytes)
// 2-byte sequences
let russian = "Ру́сский язы́к один из восточнославянских языков, национальный язык русского народа."
// 3-byte sequences
let japanese = "日本語(にほんご、にっぽんご)は、主に日本国内や日本人同士の間で使われている言語である。"
// 4-byte sequences
// Most commonly emoji, which are usually mixed with other text.
let emoji = "Panda 🐼, Dog 🐶, Cat 🐱, Mouse 🐭."
let allStrings = [ascii, russian, japanese, emoji].map { Array($0.utf8) }
let allStringsBytes: [UInt8] = Array(allStrings.joined())
let allStringsData: Data = Data(allStringsBytes)
@inline(never)
public func run_UTF8Decode(_ n: Int) {
let strings = allStrings
func isEmpty(_ result: UnicodeDecodingResult) -> Bool {
switch result {
case .emptyInput:
return true
default:
return false
}
}
for _ in 1...200*n {
for string in strings {
var it = string.makeIterator()
var utf8 = UTF8()
while !isEmpty(utf8.decode(&it)) { }
}
}
}
@inline(never)
public func run_UTF8Decode_InitFromData(_ n: Int) {
let input = allStringsData
for _ in 0..<200*n {
blackHole(String(data: input, encoding: .utf8))
}
}
@inline(never)
public func run_UTF8Decode_InitDecoding(_ n: Int) {
let input = allStringsBytes
for _ in 0..<200*n {
blackHole(String(decoding: input, as: UTF8.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromBytes(_ n: Int) {
let input = allStringsBytes
for _ in 0..<200*n {
blackHole(String(bytes: input, encoding: .utf8))
}
}
@inline(never)
public func run_UTF8Decode_InitFromData_ascii(_ n: Int) {
let input = asciiData
for _ in 0..<1_000*n {
blackHole(String(data: input, encoding: .utf8))
}
}
@inline(never)
public func run_UTF8Decode_InitDecoding_ascii(_ n: Int) {
let input = asciiBytes
for _ in 0..<1_000*n {
blackHole(String(decoding: input, as: UTF8.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromBytes_ascii(_ n: Int) {
let input = asciiBytes
for _ in 0..<1_000*n {
blackHole(String(bytes: input, encoding: .utf8))
}
}
@inline(never)
public func run_UTF8Decode_InitFromData_ascii_as_ascii(_ n: Int) {
let input = asciiData
for _ in 0..<1_000*n {
blackHole(String(data: input, encoding: .ascii))
}
}
@inline(never)
public func run_UTF8Decode_InitDecoding_ascii_as_ascii(_ n: Int) {
let input = asciiBytes
for _ in 0..<1_000*n {
blackHole(String(decoding: input, as: Unicode.ASCII.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromBytes_ascii_as_ascii(_ n: Int) {
let input = asciiBytes
for _ in 0..<1_000*n {
blackHole(String(bytes: input, encoding: .ascii))
}
}
struct CustomContiguousCollection: Collection {
let storage: [UInt8]
typealias Index = Int
typealias Element = UInt8
init(_ bytes: [UInt8]) { self.storage = bytes }
subscript(position: Int) -> Element { self.storage[position] }
var startIndex: Index { 0 }
var endIndex: Index { storage.count }
func index(after i: Index) -> Index { i+1 }
@inline(never)
func withContiguousStorageIfAvailable<R>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> R
) rethrows -> R? {
try storage.withContiguousStorageIfAvailable(body)
}
}
struct CustomNoncontiguousCollection: Collection {
let storage: [UInt8]
typealias Index = Int
typealias Element = UInt8
init(_ bytes: [UInt8]) { self.storage = bytes }
subscript(position: Int) -> Element { self.storage[position] }
var startIndex: Index { 0 }
var endIndex: Index { storage.count }
func index(after i: Index) -> Index { i+1 }
@inline(never)
func withContiguousStorageIfAvailable<R>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> R
) rethrows -> R? {
nil
}
}
let allStringsCustomContiguous = CustomContiguousCollection(allStringsBytes)
let asciiCustomContiguous = CustomContiguousCollection(Array(ascii.utf8))
let allStringsCustomNoncontiguous = CustomNoncontiguousCollection(allStringsBytes)
let asciiCustomNoncontiguous = CustomNoncontiguousCollection(Array(ascii.utf8))
@inline(never)
public func run_UTF8Decode_InitFromCustom_contiguous(_ n: Int) {
let input = allStringsCustomContiguous
for _ in 0..<200*n {
blackHole(String(decoding: input, as: UTF8.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromCustom_contiguous_ascii(_ n: Int) {
let input = asciiCustomContiguous
for _ in 0..<1_000*n {
blackHole(String(decoding: input, as: UTF8.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromCustom_contiguous_ascii_as_ascii(_ n: Int) {
let input = asciiCustomContiguous
for _ in 0..<1_000*n {
blackHole(String(decoding: input, as: Unicode.ASCII.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromCustom_noncontiguous(_ n: Int) {
let input = allStringsCustomNoncontiguous
for _ in 0..<200*n {
blackHole(String(decoding: input, as: UTF8.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromCustom_noncontiguous_ascii(_ n: Int) {
let input = asciiCustomNoncontiguous
for _ in 0..<1_000*n {
blackHole(String(decoding: input, as: UTF8.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromCustom_noncontiguous_ascii_as_ascii(_ n: Int) {
let input = asciiCustomNoncontiguous
for _ in 0..<1_000*n {
blackHole(String(decoding: input, as: Unicode.ASCII.self))
}
}
|