Skip to content

Commit a07e84f

Browse files
committed
New benchmark: word boundaries in urls
1 parent 685ab76 commit a07e84f

File tree

6 files changed

+72
-18
lines changed

6 files changed

+72
-18
lines changed

Package.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ let package = Package(
5959
name: "VariadicsGenerator",
6060
targets: ["VariadicsGenerator"]),
6161
// Disable to work around rdar://126877024
62-
// .executable(
63-
// name: "RegexBenchmark",
64-
// targets: ["RegexBenchmark"])
62+
.executable(
63+
name: "RegexBenchmark",
64+
targets: ["RegexBenchmark"])
6565
],
6666
dependencies: [
6767
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
@@ -143,17 +143,17 @@ let package = Package(
143143
"_StringProcessing"
144144
],
145145
swiftSettings: [availabilityDefinition]),
146-
// .executableTarget(
147-
// name: "RegexBenchmark",
148-
// dependencies: [
149-
// .product(name: "ArgumentParser", package: "swift-argument-parser"),
150-
// "_RegexParser",
151-
// "_StringProcessing",
152-
// "RegexBuilder"
153-
// ],
154-
// swiftSettings: [
155-
// .unsafeFlags(["-Xfrontend", "-disable-availability-checking"]),
156-
// ]),
146+
.executableTarget(
147+
name: "RegexBenchmark",
148+
dependencies: [
149+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
150+
"_RegexParser",
151+
"_StringProcessing",
152+
"RegexBuilder"
153+
],
154+
swiftSettings: [
155+
.unsafeFlags(["-Xfrontend", "-disable-availability-checking"]),
156+
]),
157157

158158
// MARK: Exercises
159159
.target(

Sources/RegexBenchmark/Benchmark.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,34 @@ struct CrossBenchmark {
153153
/// Whether to also run scalar-semantic mode
154154
var alsoRunScalarSemantic: Bool = true
155155

156+
var alsoRunSimpleWordBoundaries: Bool = false
157+
156158
func register(_ runner: inout BenchmarkRunner) {
157159
if isWhole {
158160
runner.registerCrossBenchmark(
159161
nameBase: baseName,
160162
input: input,
161163
pattern: regex,
162164
.whole,
163-
alsoRunScalarSemantic: alsoRunScalarSemantic)
165+
alsoRunScalarSemantic: alsoRunScalarSemantic,
166+
alsoRunSimpleWordBoundaries: alsoRunSimpleWordBoundaries)
164167
} else {
165168
runner.registerCrossBenchmark(
166169
nameBase: baseName,
167170
input: input,
168171
pattern: regex,
169172
.allMatches,
170-
alsoRunScalarSemantic: alsoRunScalarSemantic)
173+
alsoRunScalarSemantic: alsoRunScalarSemantic,
174+
alsoRunSimpleWordBoundaries: alsoRunSimpleWordBoundaries)
171175

172176
if includeFirst || runner.includeFirstOverride {
173177
runner.registerCrossBenchmark(
174178
nameBase: baseName,
175179
input: input,
176180
pattern: regex,
177181
.first,
178-
alsoRunScalarSemantic: alsoRunScalarSemantic)
182+
alsoRunScalarSemantic: alsoRunScalarSemantic,
183+
alsoRunSimpleWordBoundaries: alsoRunSimpleWordBoundaries)
179184
}
180185
}
181186
}

Sources/RegexBenchmark/BenchmarkRegistration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ extension BenchmarkRunner {
1818
self.addDiceNotation()
1919
self.addErrorMessages()
2020
self.addIpAddress()
21+
22+
self.addURLWithWordBoundaries()
2123
// -- end of registrations --
2224
}
2325
}

Sources/RegexBenchmark/BenchmarkRunner.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ struct BenchmarkRunner {
3333
input: String,
3434
pattern: String,
3535
_ type: Benchmark.MatchType,
36-
alsoRunScalarSemantic: Bool = true
36+
alsoRunScalarSemantic: Bool = true,
37+
alsoRunSimpleWordBoundaries: Bool
3738
) {
3839
let swiftRegex = try! Regex(pattern)
3940
let nsRegex: NSRegularExpression
@@ -58,6 +59,16 @@ struct BenchmarkRunner {
5859
type: .init(type),
5960
target: input))
6061

62+
if alsoRunSimpleWordBoundaries {
63+
register(
64+
Benchmark(
65+
name: nameBase + nameSuffix + "_SimpleWordBoundaries",
66+
regex: swiftRegex.wordBoundaryKind(.simple),
67+
pattern: pattern,
68+
type: type,
69+
target: input))
70+
}
71+
6172
if alsoRunScalarSemantic {
6273
register(
6374
Benchmark(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
extension Inputs {
2+
static let url: String = {
3+
let element = """
4+
Item 1 | Item 2® •Item 3 Item4
5+
6+
7+
\t\t\t
8+
9+
Check it out here: http://www.test.com/this-is-a-fake-url-that-should-be-replaced?a=1
10+
Check it out here: https://www.test.com/this-is-a-fake-url-that-should-be-replaced?a=1
11+
This is not a web link ftp://user@host:domain.com/path
12+
This is a link without a scheme www.apple.com/mac
13+
14+
This is some good text and should not be removed.
15+
Thanks.
16+
😀🩷🤵🏿
17+
"""
18+
let multiplier = 30
19+
return Array(repeating: element, count: multiplier).joined()
20+
}()
21+
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import _StringProcessing
2+
3+
extension BenchmarkRunner {
4+
mutating func addURLWithWordBoundaries() {
5+
let urlRegex = #"https?://([-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6})\b[-a-zA-Z0-9()@:%_+.~#?&=]*"#
6+
let url = CrossBenchmark(
7+
baseName: "URLWithWordBoundaries",
8+
regex: urlRegex,
9+
input: Inputs.url,
10+
alsoRunSimpleWordBoundaries: true
11+
)
12+
url.register(&self)
13+
}
14+
}

0 commit comments

Comments
 (0)