-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathCharacterProperties.swift.gyb
222 lines (197 loc) · 6.33 KB
/
CharacterProperties.swift.gyb
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
//===--- CharacterProperties.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 CharacterProperties.swift.gyb
// and run scripts/generate_harness/generate_harness.py to regenerate this file.
////////////////////////////////////////////////////////////////////////////////
import TestsUtils
import Foundation
public let benchmarks = [
BenchmarkInfo(
name: "CharacterPropertiesFetch",
runFunction: run_CharacterPropertiesFetch,
tags: [.validation, .api, .String],
setUpFunction: { blackHole(workload) },
legacyFactor: 10),
BenchmarkInfo(
name: "CharacterPropertiesStashed",
runFunction: run_CharacterPropertiesStashed,
tags: [.validation, .api, .String],
setUpFunction: { setupStash() },
legacyFactor: 10),
BenchmarkInfo(
name: "CharacterPropertiesStashedMemo",
runFunction: run_CharacterPropertiesStashedMemo,
tags: [.validation, .api, .String],
setUpFunction: { setupMemo() },
legacyFactor: 10),
BenchmarkInfo(
name: "CharacterPropertiesPrecomputed",
runFunction: run_CharacterPropertiesPrecomputed,
tags: [.validation, .api, .String],
setUpFunction: { setupPrecomputed() },
legacyFactor: 10),
]
extension Character {
var firstScalar: UnicodeScalar { return unicodeScalars.first! }
}
% Properties = [ ( "Alphanumeric", "alphanumerics"),
% ( "Capitalized", "capitalizedLetters"),
% ( "Control", "controlCharacters"),
% ( "Decimal", "decimalDigits"),
% ( "Letter", "letters"),
% ( "Lowercase", "lowercaseLetters"),
% ( "Uppercase", "uppercaseLetters"),
% ( "Newline", "newlines"),
% ( "Whitespace", "whitespaces"),
% ( "Punctuation", "punctuationCharacters")
% ]
// Fetch the CharacterSet for every call
% for Property, Set in Properties:
func is${Property}(_ c: Character) -> Bool {
return CharacterSet.${Set}.contains(c.firstScalar)
}
% end
// Stash the set
% for Property, Set in Properties:
let ${Set} = CharacterSet.${Set}
func is${Property}Stashed(_ c: Character) -> Bool {
return ${Set}.contains(c.firstScalar)
}
% end
func setupStash() {
blackHole(workload)
% for Property, Set in Properties:
blackHole(${Set})
% end
}
// Memoize the stashed set
% for Property, Set in Properties:
var ${Set}Memo = Set<UInt32>()
func is${Property}StashedMemo(_ c: Character) -> Bool {
let scalar = c.firstScalar
if ${Set}Memo.contains(scalar.value) { return true }
if ${Set}.contains(scalar) {
${Set}Memo.insert(scalar.value)
return true
}
return false
}
% end
func setupMemo() {
blackHole(workload)
% for Property, Set in Properties:
blackHole(${Set}Memo)
% end
}
// Precompute whole scalar set
func precompute(_ charSet: CharacterSet, capacity: Int) -> Set<UInt32> {
var result = Set<UInt32>(minimumCapacity: capacity)
for plane in 0...0x10 {
guard charSet.hasMember(inPlane: UInt8(plane)) else { continue }
let offset = plane &* 0x1_0000
for codePoint in 0...0xFFFF {
guard let scalar = UnicodeScalar(codePoint &+ offset) else { continue }
if charSet.contains(scalar) {
result.insert(scalar.value)
}
}
}
return result
}
%{ #// Reduce wide memory range. Capacities from `print("${Set}:…` below.
precomputed_capacity = dict(
controlCharacters=24951, alphanumerics=122647, lowercaseLetters=2063,
punctuationCharacters=770, whitespaces=19, letters=121145,
uppercaseLetters=1733, decimalDigits=590, newlines=7,
capitalizedLetters=31
)
}%
% for Property, Set in Properties:
var ${Set}Precomputed: Set<UInt32> =
precompute(${Set}, capacity: ${precomputed_capacity[Set]})
func is${Property}Precomputed(_ c: Character) -> Bool {
return ${Set}Precomputed.contains(c.firstScalar.value)
}
% end
func setupPrecomputed() {
blackHole(workload)
% for Property, Set in Properties:
blackHole(${Set}Precomputed)
%#// print("${Set}: \(${Set}Precomputed.count)")
% end
}
// Compute on the fly
//
// TODO: If UnicodeScalars ever exposes category, etc., implement the others!
func isNewlineComputed(_ c: Character) -> Bool {
switch c.firstScalar.value {
case 0x000A...0x000D: return true
case 0x0085: return true
case 0x2028...0x2029: return true
default: return false
}
}
let workload = """
the quick brown 🦊 jumped over the lazy 🐶.
в чащах юга жил-был цитрус? да, но фальшивый экземпляр
𓀀𓀤𓁓𓁲𓃔𓃗𓃀𓃁𓃂𓃃𓆌𓆍𓆎𓆏𓆐𓆑𓆒𓆲𓁿
🝁ꃕ躍‾∾📦⺨
👍👩👩👧👧👨👨👦👦🇺🇸🇨🇦🇲🇽👍🏻👍🏼👍🏽👍🏾👍🏿
Lorem ipsum something something something...
"""
@inline(never)
public func run_CharacterPropertiesFetch(_ n: Int) {
for _ in 1...n {
for c in workload {
% for Property, Set in Properties:
blackHole(is${Property}(c))
% end
}
}
}
@inline(never)
public func run_CharacterPropertiesStashed(_ n: Int) {
for _ in 1...n {
for c in workload {
% for Property, Set in Properties:
blackHole(is${Property}Stashed(c))
% end
}
}
}
@inline(never)
public func run_CharacterPropertiesStashedMemo(_ n: Int) {
for _ in 1...n {
for c in workload {
% for Property, Set in Properties:
blackHole(is${Property}StashedMemo(c))
% end
}
}
}
@inline(never)
public func run_CharacterPropertiesPrecomputed(_ n: Int) {
for _ in 1...n {
for c in workload {
% for Property, Set in Properties:
blackHole(is${Property}Precomputed(c))
% end
}
}
}
// TODO: run_CharacterPropertiesComputed
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: