Skip to content

[swift/main] Merge performance improvements into swift/main #705

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 50 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 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
b4b4315
Fix doc comment for trimPrefix and trimmingPrefix funcs (#673)
valeriyvan May 23, 2023
d61ba4c
Update availability for the 5.8 release (#680)
natecook1000 Jul 6, 2023
4c9b28e
Optimize search for start-anchored regexes (#682)
natecook1000 Jul 19, 2023
ba6e49d
Fix misuse of `XCTSkip()` (#685)
grynspan Jul 20, 2023
f5b0b5e
Handle boundaries when matching in substrings (#675)
natecook1000 Aug 11, 2023
185ebd6
Overhaul quantification fast-path (#689)
milseman Aug 21, 2023
d45027b
adopt the stdlib’s pattern for atomic lazy references
glessard Aug 31, 2023
74637cc
pass a pointer instead of inout conversion
glessard Aug 31, 2023
bc70423
Update Sources/_StringProcessing/Regex/Core.swift
glessard Aug 31, 2023
45fd8ec
Merge pull request #691 from glessard/pointer-conversion-restriction
glessard Sep 1, 2023
69f406c
Adds SPI for a NSRE compatibility mode option (#698)
natecook1000 Dec 1, 2023
4e742b4
Add ASCII fast-path ASCII character class matching (#690)
milseman Dec 11, 2023
4760abe
Merge remote-tracking branch 'origin/main' into swift/main
milseman Dec 11, 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
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ let availabilityDefinition = PackageDescription.SwiftSetting.unsafeFlags([
"-Xfrontend",
"-define-availability",
"-Xfrontend",
"SwiftStdlib 5.8:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
"SwiftStdlib 5.8:macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4",
"-Xfrontend",
"-define-availability",
"-Xfrontend",
"SwiftStdlib 5.9:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
])

/// Swift settings for building a private stdlib-like module that is to be used
Expand Down Expand Up @@ -128,7 +132,8 @@ let package = Package(
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"_RegexParser",
"_StringProcessing"
]),
],
swiftSettings: [availabilityDefinition]),
.executableTarget(
name: "RegexBenchmark",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/RegexTester/RegexTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import _RegexParser
import _StringProcessing

@main
@available(macOS 9999, *)
@available(SwiftStdlib 5.8, *)
struct RegexTester: ParsableCommand {
typealias MatchFunctionType = (String) throws -> Regex<AnyRegexOutput>.Match?

Expand Down
2 changes: 1 addition & 1 deletion Sources/_RegexParser/Regex/AST/MatchingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension AST {

// Swift-only default possessive quantifier
case possessiveByDefault // t.b.d.

// NSRegularExpression compatibility special-case
case nsreCompatibleDot // no AST representation
}
Expand Down
32 changes: 12 additions & 20 deletions Sources/_StringProcessing/Algorithms/Algorithms/Trim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ extension RangeReplaceableCollection {
// MARK: Fixed pattern algorithms

extension Collection where Element: Equatable {
/// Returns a new collection of the same type by removing initial elements
/// that satisfy the given predicate from the start.
/// - Parameter predicate: A closure that takes an element of the sequence
/// as its argument and returns a Boolean value indicating whether the
/// element should be removed from the collection.
/// Returns a new collection of the same type by removing `prefix` from the start
/// of the collection.
/// - Parameter prefix: The collection to remove from this collection.
/// - Returns: A collection containing the elements of the collection that are
/// not removed by `predicate`.
/// not removed by `prefix`.
@available(SwiftStdlib 5.7, *)
public func trimmingPrefix<Prefix: Sequence>(
_ prefix: Prefix
Expand All @@ -97,11 +95,8 @@ extension Collection where Element: Equatable {
}

extension Collection where SubSequence == Self, Element: Equatable {
/// Removes the initial elements that satisfy the given predicate from the
/// start of the sequence.
/// - Parameter predicate: A closure that takes an element of the sequence
/// as its argument and returns a Boolean value indicating whether the
/// element should be removed from the collection.
/// Removes `prefix` from the start of the collection.
/// - Parameter prefix: The collection to remove from this collection.
@available(SwiftStdlib 5.7, *)
public mutating func trimPrefix<Prefix: Sequence>(
_ prefix: Prefix
Expand All @@ -111,11 +106,8 @@ extension Collection where SubSequence == Self, Element: Equatable {
}

extension RangeReplaceableCollection where Element: Equatable {
/// Removes the initial elements that satisfy the given predicate from the
/// start of the sequence.
/// - Parameter predicate: A closure that takes an element of the sequence
/// as its argument and returns a Boolean value indicating whether the
/// element should be removed from the collection.
/// Removes `prefix` from the start of the collection.
/// - Parameter prefix: The collection to remove from this collection.
@available(SwiftStdlib 5.7, *)
public mutating func trimPrefix<Prefix: Sequence>(
_ prefix: Prefix
Expand All @@ -127,11 +119,11 @@ extension RangeReplaceableCollection where Element: Equatable {
// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
/// Returns a new collection of the same type by removing `prefix` from the
/// start.
/// - Parameter prefix: The collection to remove from this collection.
/// Returns a new collection of the same type by removing the initial elements
/// that matches the given regex.
/// - Parameter regex: The regex to remove from this collection.
/// - Returns: A collection containing the elements that does not match
/// `prefix` from the start.
/// `regex` from the start.
@_disfavoredOverload
@available(SwiftStdlib 5.7, *)
public func trimmingPrefix(_ regex: some RegexComponent) -> SubSequence {
Expand Down
66 changes: 33 additions & 33 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,16 @@ fileprivate extension Compiler.ByteCodeGen {
assert(high != 0)
assert((0...(high ?? Int.max)).contains(low))

let extraTrips: Int?
let maxExtraTrips: Int?
if let h = high {
extraTrips = h - low
maxExtraTrips = h - low
} else {
extraTrips = nil
maxExtraTrips = nil
}
let minTrips = low
assert((extraTrips ?? 1) >= 0)
assert((maxExtraTrips ?? 1) >= 0)

if tryEmitFastQuant(child, updatedKind, minTrips, extraTrips) {
if tryEmitFastQuant(child, updatedKind, minTrips, maxExtraTrips) {
return
}

Expand All @@ -492,19 +492,19 @@ fileprivate extension Compiler.ByteCodeGen {
decrement %minTrips and fallthrough

loop-body:
<if can't guarantee forward progress && extraTrips = nil>:
<if can't guarantee forward progress && maxExtraTrips = nil>:
mov currentPosition %pos
evaluate the subexpression
<if can't guarantee forward progress && extraTrips = nil>:
<if can't guarantee forward progress && maxExtraTrips = nil>:
if %pos is currentPosition:
goto exit
goto min-trip-count control block

exit-policy control block:
if %extraTrips is zero:
if %maxExtraTrips is zero:
goto exit
else:
decrement %extraTrips and fallthrough
decrement %maxExtraTrips and fallthrough

<if eager>:
save exit and goto loop-body
Expand All @@ -531,12 +531,12 @@ fileprivate extension Compiler.ByteCodeGen {
/* fallthrough */
"""

// Specialization based on `extraTrips` for 0 or unbounded
// Specialization based on `maxExtraTrips` for 0 or unbounded
_ = """
exit-policy control block:
<if extraTrips == 0>:
<if maxExtraTrips == 0>:
goto exit
<if extraTrips == .unbounded>:
<if maxExtraTrips == .unbounded>:
/* fallthrough */
"""

Expand Down Expand Up @@ -569,12 +569,12 @@ fileprivate extension Compiler.ByteCodeGen {
minTripsReg = nil
}

let extraTripsReg: IntRegister?
if (extraTrips ?? 0) > 0 {
extraTripsReg = builder.makeIntRegister(
initialValue: extraTrips!)
let maxExtraTripsReg: IntRegister?
if (maxExtraTrips ?? 0) > 0 {
maxExtraTripsReg = builder.makeIntRegister(
initialValue: maxExtraTrips!)
} else {
extraTripsReg = nil
maxExtraTripsReg = nil
}

// Set up a dummy save point for possessive to update
Expand Down Expand Up @@ -606,7 +606,7 @@ fileprivate extension Compiler.ByteCodeGen {
let startPosition: PositionRegister?
let emitPositionChecking =
(!optimizationsEnabled || !child.guaranteesForwardProgress) &&
extraTrips == nil
maxExtraTrips == nil

if emitPositionChecking {
startPosition = builder.makePositionRegister()
Expand All @@ -616,7 +616,7 @@ fileprivate extension Compiler.ByteCodeGen {
}
try emitNode(child)
if emitPositionChecking {
// in all quantifier cases, no matter what minTrips or extraTrips is,
// in all quantifier cases, no matter what minTrips or maxExtraTrips is,
// if we have a successful non-advancing match, branch to exit because it
// can match an arbitrary number of times
builder.buildCondBranch(to: exit, ifSamePositionAs: startPosition!)
Expand All @@ -629,20 +629,20 @@ fileprivate extension Compiler.ByteCodeGen {
}

// exit-policy:
// condBranch(to: exit, ifZeroElseDecrement: %extraTrips)
// condBranch(to: exit, ifZeroElseDecrement: %maxExtraTrips)
// <eager: split(to: loop, saving: exit)>
// <possesive:
// clearSavePoint
// split(to: loop, saving: exit)>
// <reluctant: save(restoringAt: loop)
builder.label(exitPolicy)
switch extraTrips {
switch maxExtraTrips {
case nil: break
case 0: builder.buildBranch(to: exit)
default:
assert(extraTripsReg != nil, "logic inconsistency")
assert(maxExtraTripsReg != nil, "logic inconsistency")
builder.buildCondBranch(
to: exit, ifZeroElseDecrement: extraTripsReg!)
to: exit, ifZeroElseDecrement: maxExtraTripsReg!)
}

switch updatedKind {
Expand Down Expand Up @@ -672,12 +672,12 @@ fileprivate extension Compiler.ByteCodeGen {
_ child: DSLTree.Node,
_ kind: AST.Quantification.Kind,
_ minTrips: Int,
_ extraTrips: Int?
_ maxExtraTrips: Int?
) -> Bool {
let isScalarSemantics = options.semanticLevel == .unicodeScalar
guard optimizationsEnabled
&& minTrips <= QuantifyPayload.maxStorableTrips
&& extraTrips ?? 0 <= QuantifyPayload.maxStorableTrips
&& maxExtraTrips ?? 0 <= QuantifyPayload.maxStorableTrips
&& kind != .reluctant else {
return false
}
Expand All @@ -687,7 +687,7 @@ fileprivate extension Compiler.ByteCodeGen {
guard let bitset = ccc.asAsciiBitset(options) else {
return false
}
builder.buildQuantify(bitset: bitset, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
builder.buildQuantify(bitset: bitset, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)

case .atom(let atom):
switch atom {
Expand All @@ -696,17 +696,17 @@ fileprivate extension Compiler.ByteCodeGen {
guard let val = c._singleScalarAsciiValue else {
return false
}
builder.buildQuantify(asciiChar: val, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
builder.buildQuantify(asciiChar: val, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)

case .any:
builder.buildQuantifyAny(
matchesNewlines: true, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
matchesNewlines: true, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
case .anyNonNewline:
builder.buildQuantifyAny(
matchesNewlines: false, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
matchesNewlines: false, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)
case .dot:
builder.buildQuantifyAny(
matchesNewlines: options.dotMatchesNewline, kind, minTrips, extraTrips, isScalarSemantics: isScalarSemantics)
matchesNewlines: options.dotMatchesNewline, kind, minTrips, maxExtraTrips, isScalarSemantics: isScalarSemantics)

case .characterClass(let cc):
// Custom character class that consumes a single grapheme
Expand All @@ -715,19 +715,19 @@ fileprivate extension Compiler.ByteCodeGen {
model: model,
kind,
minTrips,
extraTrips,
maxExtraTrips,
isScalarSemantics: isScalarSemantics)
default:
return false
}
case .convertedRegexLiteral(let node, _):
return tryEmitFastQuant(node, kind, minTrips, extraTrips)
return tryEmitFastQuant(node, kind, minTrips, maxExtraTrips)
case .nonCapturingGroup(let groupKind, let node):
// .nonCapture nonCapturingGroups are ignored during compilation
guard groupKind.ast == .nonCapture else {
return false
}
return tryEmitFastQuant(node, kind, minTrips, extraTrips)
return tryEmitFastQuant(node, kind, minTrips, maxExtraTrips)
default:
return false
}
Expand Down
Loading