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
|
@_spi(RegexBenchmark) import _StringProcessing
@_implementationOnly import _RegexParser
import Foundation
protocol RegexBenchmark: Debug {
var name: String { get }
func run()
}
protocol SwiftRegexBenchmark: RegexBenchmark {
var regex: Regex<AnyRegexOutput> { get set }
var pattern: String? { get }
}
extension SwiftRegexBenchmark {
mutating func compile() {
let _ = regex._forceAction(.recompile)
}
mutating func parse() -> Bool {
guard let s = pattern else {
return false
}
do {
let _ = try _RegexParser.parse(s, .traditional)
return true
} catch {
return false
}
}
mutating func enableTracing() {
let _ = regex._forceAction(.addOptions(.enableTracing))
}
mutating func enableMetrics() {
let _ = regex._forceAction(.addOptions([.enableMetrics]))
}
}
struct Benchmark: SwiftRegexBenchmark {
let name: String
var regex: Regex<AnyRegexOutput>
let pattern: String?
let type: MatchType
let target: String
enum MatchType {
case whole
case first
case allMatches
}
func run() {
switch type {
case .whole: blackHole(target.wholeMatch(of: regex))
case .allMatches: blackHole(target.matches(of: regex))
case .first: blackHole(target.firstMatch(of: regex))
}
}
}
struct NSBenchmark: RegexBenchmark {
let name: String
let regex: NSRegularExpression
let type: NSMatchType
let target: String
var range: NSRange {
NSRange(target.startIndex..<target.endIndex, in: target)
}
enum NSMatchType {
case allMatches
case first
init(_ type: Benchmark.MatchType) {
switch type {
case .whole, .first: self = .first
case .allMatches: self = .allMatches
}
}
}
func run() {
switch type {
case .allMatches: blackHole(regex.matches(in: target, range: range))
case .first: blackHole(regex.firstMatch(in: target, range: range))
}
}
}
/// A benchmark running a regex on strings in input set
struct InputListBenchmark: SwiftRegexBenchmark {
let name: String
var regex: Regex<AnyRegexOutput>
let pattern: String?
let targets: [String]
func run() {
for target in targets {
blackHole(target.wholeMatch(of: regex))
}
}
}
struct InputListNSBenchmark: RegexBenchmark {
let name: String
let regex: NSRegularExpression
let targets: [String]
init(name: String, regex: String, targets: [String]) {
self.name = name
self.regex = try! NSRegularExpression(pattern: "^" + regex + "$")
self.targets = targets
}
func range(in target: String) -> NSRange {
NSRange(target.startIndex..<target.endIndex, in: target)
}
func run() {
for target in targets {
let range = range(in: target)
blackHole(regex.firstMatch(in: target, range: range))
}
}
}
/// A benchmark meant to be ran across multiple engines
struct CrossBenchmark {
/// Suffix added onto NSRegularExpression benchmarks
static var nsSuffix: String { "_NS" }
/// The base name of the benchmark
var baseName: String
/// The string to compile in different engines
var regex: String
/// The text to search
var input: String
// TODO: var output, for validation
/// Whether this is whole string matching or a searching benchmark
///
/// TODO: Probably better ot have a whole-line vs search anywhere, maybe
/// accomodate multi-line matching, etc.
var isWhole: Bool = false
/// Whether or not to do firstMatch as well or just allMatches
var includeFirst: Bool = false
/// Whether to also run scalar-semantic mode
var alsoRunScalarSemantic: Bool = true
func register(_ runner: inout BenchmarkRunner) {
if isWhole {
runner.registerCrossBenchmark(
nameBase: baseName,
input: input,
pattern: regex,
.whole,
alsoRunScalarSemantic: alsoRunScalarSemantic)
} else {
runner.registerCrossBenchmark(
nameBase: baseName,
input: input,
pattern: regex,
.allMatches,
alsoRunScalarSemantic: alsoRunScalarSemantic)
if includeFirst || runner.includeFirstOverride {
runner.registerCrossBenchmark(
nameBase: baseName,
input: input,
pattern: regex,
.first,
alsoRunScalarSemantic: alsoRunScalarSemantic)
}
}
}
}
/// A benchmark running a regex on strings in input list, run across multiple engines
struct CrossInputListBenchmark {
/// The base name of the benchmark
var baseName: String
/// The string to compile in differnet engines
var regex: String
/// The list of strings to search
var inputs: [String]
/// Also run in scalar-semantic mode
var alsoRunScalarSemantic: Bool = true
func register(_ runner: inout BenchmarkRunner) {
runner.registerCrossBenchmark(
name: baseName,
inputList: inputs,
pattern: regex,
alsoRunScalarSemantic: alsoRunScalarSemantic)
}
}
// TODO: Capture-containing benchmarks
// nom nom nom, consume the argument
@inline(never)
func blackHole<T>(_ x: T) {
}
|