Skip to content

Update swift main #674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0b38ca9
Atomically load the lowered program (#610)
natecook1000 Oct 6, 2022
335a0c2
Add tests for line start/end word boundary diffs (#616)
natecook1000 Dec 2, 2022
54ff516
Add tweaks for Android
finagolfin Dec 5, 2022
eb7f801
Fix documentation typo (#615)
ole Dec 6, 2022
c51e8f2
Fix abstract for Regex.dotMatchesNewlines(_:). (#614)
amartini51 Dec 6, 2022
45f752a
Remove `RegexConsumer` and fix its dependencies (#617)
natecook1000 Dec 14, 2022
ed95066
Improve StringProcessing and RegexBuilder documentation (#611)
natecook1000 Dec 14, 2022
c34cea5
Set availability for inverted character class test (#621)
natecook1000 Dec 16, 2022
3ca8b13
Merge pull request #618 from buttaface/droid
Azoy Dec 18, 2022
3a3dc7a
Add type annotations in RegexBuilder tests
natecook1000 Feb 1, 2023
6c4f291
Workaround for fileprivate array issue
natecook1000 Feb 1, 2023
7e059b7
Merge pull request #628 from apple/result_builder_changes_workaround
natecook1000 Feb 1, 2023
6a4077f
Fix an issue where named character classes weren't getting converted …
DaveEwing Feb 1, 2023
8184fc0
Merge pull request #629 from apple/dewing/CharacterClassDSLConversion
DaveEwing Feb 2, 2023
2a78475
Stop at end of search string in TwoWaySearcher (#631)
natecook1000 Feb 8, 2023
d5a6cec
Correct misspelling in DSL renderer (#627)
natecook1000 Feb 8, 2023
7756942
Fix output type mismatch with RegexBuilder (#626)
natecook1000 Feb 9, 2023
070e0ec
Revert "Merge pull request #628 from apple/result_builder_changes_wor…
natecook1000 Feb 15, 2023
1358fc0
Use `some` syntax in variadics
natecook1000 Feb 15, 2023
083d32a
Type checker workaround: adjust test
milseman Apr 2, 2023
ca92db7
Further refactor to work around type checker regression
milseman Apr 3, 2023
336f9c5
Merge pull request #643 from milseman/typechecker_workaround
milseman Apr 3, 2023
852b890
Align availability macro with OS versions (#641)
milseman Apr 4, 2023
236b47c
Speed up general character class matching (#642)
milseman Apr 4, 2023
348e6c3
Test for \s matching CRLF when scalar matching (#648)
natecook1000 Apr 4, 2023
a7ba701
General ascii fast paths for character classes (#644)
milseman Apr 4, 2023
e01e43d
Remove the unsupported `anyScalar` case (#650)
natecook1000 Apr 4, 2023
e0352a2
Fix range-based quantification fast path (#653)
natecook1000 Apr 11, 2023
923cf5e
Add in ASCII fast-path for anyNonNewline (#654)
milseman Apr 11, 2023
9ea9936
Avoid long expression type checks (#657)
natecook1000 Apr 11, 2023
58626cc
Processor cleanup (#655)
milseman Apr 14, 2023
4418183
Fix `firstRange(of:)` search (#656)
natecook1000 Apr 14, 2023
57b343d
Bug fix and hot path for quantified `.` (#658)
milseman Apr 18, 2023
6695027
Run scalar-semantic benchmark variants (#659)
milseman Apr 18, 2023
8eafd55
Refactor operations to be on String (#664)
milseman Apr 19, 2023
0354667
Provide unique generic method parameter names (#669)
natecook1000 May 16, 2023
98d5ddc
Enable quantification optimizations for scalar semantics (#671)
milseman May 22, 2023
84bc9c8
Merge remote-tracking branch 'origin/main' into swift/main
milseman May 22, 2023
59fce2f
Remove redundant test
milseman May 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 35 additions & 57 deletions Sources/RegexBenchmark/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ struct NSBenchmark: RegexBenchmark {
enum NSMatchType {
case allMatches
case first

init(_ type: Benchmark.MatchType) {
switch type {
case .whole, .first: self = .first
case .allMatches: self = .allMatches
}
}
}

func run() {
Expand Down Expand Up @@ -126,7 +133,7 @@ struct CrossBenchmark {
/// The base name of the benchmark
var baseName: String

/// The string to compile in differnet engines
/// The string to compile in different engines
var regex: String

/// The text to search
Expand All @@ -143,57 +150,32 @@ struct CrossBenchmark {
/// 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) {
let swiftRegex = try! Regex(regex)
let nsRegex: NSRegularExpression
if isWhole {
nsRegex = try! NSRegularExpression(pattern: "^" + regex + "$")
runner.registerCrossBenchmark(
nameBase: baseName,
input: input,
pattern: regex,
.whole,
alsoRunScalarSemantic: alsoRunScalarSemantic)
} else {
nsRegex = try! NSRegularExpression(pattern: regex)
}
runner.registerCrossBenchmark(
nameBase: baseName,
input: input,
pattern: regex,
.allMatches,
alsoRunScalarSemantic: alsoRunScalarSemantic)

if isWhole {
runner.register(
Benchmark(
name: baseName + "Whole",
regex: swiftRegex,
pattern: regex,
type: .whole,
target: input))
runner.register(
NSBenchmark(
name: baseName + "Whole" + CrossBenchmark.nsSuffix,
regex: nsRegex,
type: .first,
target: input))
} else {
runner.register(
Benchmark(
name: baseName + "All",
regex: swiftRegex,
pattern: regex,
type: .allMatches,
target: input))
runner.register(
NSBenchmark(
name: baseName + "All" + CrossBenchmark.nsSuffix,
regex: nsRegex,
type: .allMatches,
target: input))
if includeFirst || runner.includeFirstOverride {
runner.register(
Benchmark(
name: baseName + "First",
regex: swiftRegex,
pattern: regex,
type: .first,
target: input))
runner.register(
NSBenchmark(
name: baseName + "First" + CrossBenchmark.nsSuffix,
regex: nsRegex,
type: .first,
target: input))
runner.registerCrossBenchmark(
nameBase: baseName,
input: input,
pattern: regex,
.first,
alsoRunScalarSemantic: alsoRunScalarSemantic)
}
}
}
Expand All @@ -209,20 +191,16 @@ struct CrossInputListBenchmark {

/// The list of strings to search
var inputs: [String]

/// Also run in scalar-semantic mode
var alsoRunScalarSemantic: Bool = true

func register(_ runner: inout BenchmarkRunner) {
let swiftRegex = try! Regex(regex)
runner.register(InputListBenchmark(
runner.registerCrossBenchmark(
name: baseName,
regex: swiftRegex,
inputList: inputs,
pattern: regex,
targets: inputs
))
runner.register(InputListNSBenchmark(
name: baseName + CrossBenchmark.nsSuffix,
regex: regex,
targets: inputs
))
alsoRunScalarSemantic: alsoRunScalarSemantic)
}
}

Expand Down
143 changes: 141 additions & 2 deletions Sources/RegexBenchmark/BenchmarkRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import Foundation
/// The number of times to re-run the benchmark if results are too varying
private var rerunCount: Int { 3 }

extension Benchmark.MatchType {
fileprivate var nameSuffix: String {
switch self {
case .whole: return "_Whole"
case .first: return "_First"
case .allMatches: return "_All"
}
}
}

struct BenchmarkRunner {
let suiteName: String
var suite: [any RegexBenchmark] = []
Expand All @@ -16,12 +26,141 @@ struct BenchmarkRunner {

// Forcibly include firstMatch benchmarks for all CrossBenchmarks
let includeFirstOverride: Bool

// Register a cross-benchmark
mutating func registerCrossBenchmark(
nameBase: String,
input: String,
pattern: String,
_ type: Benchmark.MatchType,
alsoRunScalarSemantic: Bool = true
) {
let swiftRegex = try! Regex(pattern)
let nsRegex: NSRegularExpression
if type == .whole {
nsRegex = try! NSRegularExpression(pattern: "^" + pattern + "$")
} else {
nsRegex = try! NSRegularExpression(pattern: pattern)
}
let nameSuffix = type.nameSuffix

register(
Benchmark(
name: nameBase + nameSuffix,
regex: swiftRegex,
pattern: pattern,
type: type,
target: input))
register(
NSBenchmark(
name: nameBase + nameSuffix + CrossBenchmark.nsSuffix,
regex: nsRegex,
type: .init(type),
target: input))

if alsoRunScalarSemantic {
register(
Benchmark(
name: nameBase + nameSuffix + "_Scalar",
regex: swiftRegex.matchingSemantics(.unicodeScalar),
pattern: pattern,
type: type,
target: input))
register(
NSBenchmark(
name: nameBase + nameSuffix + "_Scalar" + CrossBenchmark.nsSuffix,
regex: nsRegex,
type: .init(type),
target: input))
}
}

// Register a cross-benchmark list
mutating func registerCrossBenchmark(
name: String,
inputList: [String],
pattern: String,
alsoRunScalarSemantic: Bool = true
) {
let swiftRegex = try! Regex(pattern)
register(InputListBenchmark(
name: name,
regex: swiftRegex,
pattern: pattern,
targets: inputList
))
register(InputListNSBenchmark(
name: name + CrossBenchmark.nsSuffix,
regex: pattern,
targets: inputList
))

if alsoRunScalarSemantic {
register(InputListBenchmark(
name: name + "_Scalar",
regex: swiftRegex.matchingSemantics(.unicodeScalar),
pattern: pattern,
targets: inputList
))
register(InputListNSBenchmark(
name: name + "_Scalar" + CrossBenchmark.nsSuffix,
regex: pattern,
targets: inputList
))
}

}

// Register a swift-only benchmark
mutating func register(
nameBase: String,
input: String,
pattern: String,
_ swiftRegex: Regex<AnyRegexOutput>,
_ type: Benchmark.MatchType,
alsoRunScalarSemantic: Bool = true
) {
let nameSuffix = type.nameSuffix

register(
Benchmark(
name: nameBase + nameSuffix,
regex: swiftRegex,
pattern: pattern,
type: type,
target: input))

if alsoRunScalarSemantic {
register(
Benchmark(
name: nameBase + nameSuffix + "_Scalar",
regex: swiftRegex,
pattern: pattern,
type: type,
target: input))
}
}

mutating func register(_ benchmark: some RegexBenchmark) {
private mutating func register(_ benchmark: NSBenchmark) {
suite.append(benchmark)
}

mutating func register(_ benchmark: some SwiftRegexBenchmark) {
private mutating func register(_ benchmark: Benchmark) {
var benchmark = benchmark
if enableTracing {
benchmark.enableTracing()
}
if enableMetrics {
benchmark.enableMetrics()
}
suite.append(benchmark)
}

private mutating func register(_ benchmark: InputListNSBenchmark) {
suite.append(benchmark)
}

private mutating func register(_ benchmark: InputListBenchmark) {
var benchmark = benchmark
if enableTracing {
benchmark.enableTracing()
Expand Down
72 changes: 37 additions & 35 deletions Sources/RegexBenchmark/Suite/CustomCharacterClasses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,55 @@ extension BenchmarkRunner {

let input = Inputs.graphemeBreakData

register(Benchmark(
name: "BasicCCC",
regex: try! Regex(basic),
// TODO: Which of these can be cross-benchmarks?

register(
nameBase: "BasicCCC",
input: input,
pattern: basic,
type: .allMatches,
target: input))
try! Regex(basic),
.allMatches)

register(Benchmark(
name: "BasicRangeCCC",
regex: try! Regex(basicRange),
register(
nameBase: "BasicRangeCCC",
input: input,
pattern: basicRange,
type: .allMatches,
target: input))
try! Regex(basicRange),
.allMatches)

register(Benchmark(
name: "CaseInsensitiveCCC",
regex: try! Regex(caseInsensitive),
register(
nameBase: "CaseInsensitiveCCC",
input: input,
pattern: caseInsensitive,
type: .allMatches,
target: input))
try! Regex(caseInsensitive),
.allMatches)

register(Benchmark(
name: "InvertedCCC",
regex: try! Regex(inverted),
register(
nameBase: "InvertedCCC",
input: input,
pattern: inverted,
type: .allMatches,
target: input))
try! Regex(inverted),
.allMatches)

register(Benchmark(
name: "SubtractionCCC",
regex: try! Regex(subtraction),
register(
nameBase: "SubtractionCCC",
input: input,
pattern: subtraction,
type: .allMatches,
target: input))
try! Regex(subtraction),
.allMatches)

register(Benchmark(
name: "IntersectionCCC",
regex: try! Regex(intersection),
register(
nameBase: "IntersectionCCC",
input: input,
pattern: intersection,
type: .allMatches,
target: input))
try! Regex(intersection),
.allMatches)

register(Benchmark(
name: "symDiffCCC",
regex: try! Regex(symmetricDifference),
register(
nameBase: "symDiffCCC",
input: input,
pattern: symmetricDifference,
type: .allMatches,
target: input))
try! Regex(symmetricDifference),
.allMatches)
}
}
4 changes: 2 additions & 2 deletions Sources/RegexBenchmark/Utils/Stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Foundation
enum Stats {}

extension Stats {
// Maximum allowed standard deviation is 5% of the median runtime
static let maxAllowedStdev = 0.05
// Maximum allowed standard deviation is 7.5% of the median runtime
static let maxAllowedStdev = 0.075

static func tTest(_ a: Measurement, _ b: Measurement) -> Bool {
// Student's t-test
Expand Down
Loading