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
|
//===--- StringWalk.swift -------------------------------------*- 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
//
//===----------------------------------------------------------------------===//
% # Ignore the following warning. This _is_ the correct file to edit.
////////////////////////////////////////////////////////////////////////////////
// WARNING: This file is manually generated from .gyb template and should not
// be directly modified. Instead, make changes to StringWalk.swift.gyb and run
// scripts/generate_harness/generate_harness.py to regenerate this file.
////////////////////////////////////////////////////////////////////////////////
//
// Test String iteration performance over a variety of workloads, languages,
// and symbols.
//
import TestsUtils
//
// Helper functionality
//
@inline(never) func count_unicodeScalars(_ s: String.UnicodeScalarView) {
var count = 0
for _ in s {
count += 1
}
blackHole(count)
}
@inline(never) func count_characters(_ s: String) {
var count = 0
for _ in s {
count += 1
}
blackHole(count)
}
@inline(never) func count_unicodeScalars_rev(
_ s: ReversedCollection<String.UnicodeScalarView>
) {
var count = 0
for _ in s {
count += 1
}
blackHole(count)
}
@inline(never) func count_characters_rev(
_ s: ReversedCollection<String>
) {
var count = 0
for _ in s {
count += 1
}
blackHole(count)
}
//
// Workloads
//
let ascii =
"siebenhundertsiebenundsiebzigtausendsiebenhundertsiebenundsiebzig"
let emoji = "👍👩👩👧👧👨👨👦👦🇺🇸🇨🇦🇲🇽👍🏻👍🏼👍🏽👍🏾👍🏿"
let utf16 = emoji + "the quick brown fox" + String(emoji.reversed())
let japanese = "今回のアップデートでSwiftに大幅な改良が施され、安定していてしかも直感的に使うことができるAppleプラットフォーム向けプログラミング言語になりました。"
let chinese = "Swift 是面向 Apple 平台的编程语言,功能强大且直观易用,而本次更新对其进行了全面优化。"
let korean = "이번 업데이트에서는 강력하면서도 직관적인 Apple 플랫폼용 프로그래밍 언어인 Swift를 완벽히 개선하였습니다."
let russian = "в чащах юга жил-был цитрус? да, но фальшивый экземпляр"
let punctuated = "\u{201c}Hello\u{2010}world\u{2026}\u{201d}"
let punctuatedJapanese = "\u{300c}\u{300e}今日は\u{3001}世界\u{3002}\u{300f}\u{300d}"
// A workload that's mostly Latin characters, with occasional emoji
// interspersed. Common for tweets.
let tweet = "Worst thing about working on String is that it breaks *everything*. Asserts, debuggers, and *especially* printf-style debugging 😭"
//
// Benchmarks
//
// Pre-commit benchmark: simple scalar walk
@inline(never)
public func run_StringWalk(_ n: Int) {
return run_StringWalk_ascii_unicodeScalars(n)
}
// Extended String benchmarks:
let baseMultiplier = 250
let unicodeScalarsMultiplier = baseMultiplier
let charactersMultiplier = baseMultiplier / 5
% Names = ["ascii", "utf16", "tweet", "japanese", "chinese", "korean", "russian", "punctuated", "punctuatedJapanese"]
% Kinds = ["unicodeScalars", "characters"]
% Directions = ["", "_Backwards"]
// An extended benchmark suite exercising finer-granularity behavior of our
// Strings.
public let benchmarks = [
BenchmarkInfo(
name: "StringWalk",
runFunction: run_StringWalk,
tags: [.validation, .api, .String],
legacyFactor: 40),
% for Name in Names:
% for Direction in Directions:
% for Kind in Kinds:
BenchmarkInfo(
name: "StringWalk_${Name}_${Kind}${Direction}",
runFunction: run_StringWalk_${Name}_${Kind}${Direction},
tags: [.api, .String, .skip],
legacyFactor: 40),
% end # Kinds
BenchmarkInfo(
name: "CharIteration_${Name}_unicodeScalars${Direction}",
runFunction: run_CharIteration_${Name}_unicodeScalars${Direction},
tags: [.validation, .api, .String],
legacyFactor: 40),
BenchmarkInfo(
name: "CharIndexing_${Name}_unicodeScalars${Direction}",
runFunction: run_CharIndexing_${Name}_unicodeScalars${Direction},
tags: [.validation, .api, .String],
legacyFactor: 40),
% end # Directions
% end # Names
]
% for Name in Names:
% for (Kind, View) in zip(Kinds, [".unicodeScalars", ""]):
@inline(never)
public func run_StringWalk_${Name}_${Kind}(_ n: Int) {
for _ in 1...${Kind}Multiplier*n {
count_${Kind}(${Name}${View})
}
}
@inline(never)
public func run_StringWalk_${Name}_${Kind}_Backwards(_ n: Int) {
for _ in 1...${Kind}Multiplier*n {
count_${Kind}_rev(${Name}${View}.reversed())
}
}
% end
let ${Name}Characters = Array(${Name})
@inline(never)
public func run_CharIteration_${Name}_unicodeScalars(_ n: Int) {
var count = 0
for _ in 1...unicodeScalarsMultiplier*n {
for c in ${Name}Characters {
for u in c.unicodeScalars {
count |= Int(u.value)
}
}
}
blackHole(count)
}
@inline(never)
public func run_CharIteration_${Name}_unicodeScalars_Backwards(_ n: Int) {
var count = 0
for _ in 1...unicodeScalarsMultiplier*n {
for c in ${Name}Characters {
for u in c.unicodeScalars.reversed() {
count |= Int(u.value)
}
}
}
blackHole(count)
}
@inline(never)
public func run_CharIndexing_${Name}_unicodeScalars(_ n: Int) {
var count = 0
for _ in 1...unicodeScalarsMultiplier*n {
for c in ${Name}Characters {
let s = c.unicodeScalars
for i in s.indices {
count |= Int(s[i].value)
}
}
}
blackHole(count)
}
@inline(never)
public func run_CharIndexing_${Name}_unicodeScalars_Backwards(_ n: Int) {
var count = 0
for _ in 1...unicodeScalarsMultiplier*n {
for c in ${Name}Characters {
let s = c.unicodeScalars
for i in s.indices.reversed() {
count |= Int(s[i].value)
}
}
}
blackHole(count)
}
% end
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End:
|