From 9e07efb4c2ac2b633aaedc5caf2e671886833938 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Mon, 15 Sep 2025 21:52:38 -0500 Subject: [PATCH] Add @_effects(readnone) to string-based Regex inits It doesn't seem like regex instantiation is being hoisted out of loops, which means that both the parsing (which happens at runtime) and compilation (which happens on first use) are being lost for string-based and possibly literal regexes. This may address the issue. --- Sources/_StringProcessing/Regex/AnyRegexOutput.swift | 3 +++ Sources/_StringProcessing/Regex/Core.swift | 1 + 2 files changed, 4 insertions(+) diff --git a/Sources/_StringProcessing/Regex/AnyRegexOutput.swift b/Sources/_StringProcessing/Regex/AnyRegexOutput.swift index 0a76f2d86..5b8b1b0ec 100644 --- a/Sources/_StringProcessing/Regex/AnyRegexOutput.swift +++ b/Sources/_StringProcessing/Regex/AnyRegexOutput.swift @@ -178,10 +178,12 @@ extension Regex where Output == AnyRegexOutput { /// ``init(_:as:)`` initializer instead. /// /// - Parameter pattern: A string with regular expression syntax. + @_effects(readnone) public init(_ pattern: String) throws { self.init(ast: try parse(pattern, .traditional)) } + @_effects(readnone) internal init(_ pattern: String, syntax: SyntaxOptions) throws { self.init(ast: try parse(pattern, syntax)) } @@ -212,6 +214,7 @@ extension Regex { /// - Parameters: /// - pattern: A string with regular expression syntax. /// - outputType: The desired type for the output captures. + @_effects(readnone) public init( _ pattern: String, as outputType: Output.Type = Output.self diff --git a/Sources/_StringProcessing/Regex/Core.swift b/Sources/_StringProcessing/Regex/Core.swift index 11445531c..2904cbdf7 100644 --- a/Sources/_StringProcessing/Regex/Core.swift +++ b/Sources/_StringProcessing/Regex/Core.swift @@ -103,6 +103,7 @@ public struct Regex: RegexComponent { } // Compiler interface. Do not change independently. + @_effects(readnone) @usableFromInline init(_regexString pattern: String) { self.init(ast: try! parse(pattern, .traditional))