From d45027b9e9497fb22da87a14593cb7dc497d12d9 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 31 Aug 2023 13:53:30 -0700 Subject: [PATCH 1/3] =?UTF-8?q?adopt=20the=20stdlib=E2=80=99s=20pattern=20?= =?UTF-8?q?for=20atomic=20lazy=20references?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - avoids reliance on a pointer conversion --- Sources/_StringProcessing/Regex/Core.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sources/_StringProcessing/Regex/Core.swift b/Sources/_StringProcessing/Regex/Core.swift index 5940fd500..aa4a99207 100644 --- a/Sources/_StringProcessing/Regex/Core.swift +++ b/Sources/_StringProcessing/Regex/Core.swift @@ -126,6 +126,13 @@ extension Regex { /// A program representation that caches any lowered representation for /// execution. internal final class Program { + + // This stored property should be stored at offset zero. We perform atomic + // operations on it. + // + /// Do not access this property directly - all accesses must go through `loweredProgram`. + fileprivate var _loweredProgramStorage: AnyObject? = nil + /// The underlying IR. /// /// FIXME: If Regex is the unit of composition, then it should be a Node instead, @@ -141,14 +148,16 @@ extension Regex { init(_ value: MEProgram) { self.value = value } } - /// Do not use directly - all accesses must go through `loweredProgram`. - fileprivate var _loweredProgramStorage: AnyObject? = nil - + fileprivate var _loweredProgramStoragePtr: UnsafeMutablePointer { + _getUnsafePointerToStoredProperties(self) + .assumingMemoryBound(to: Optional.self) + } + /// The program for execution with the matching engine. var loweredProgram: MEProgram { /// Atomically loads the compiled program if it has already been stored. func loadProgram() -> MEProgram? { - guard let loweredObject = _stdlib_atomicLoadARCRef(object: &_loweredProgramStorage) + guard let loweredObject = _stdlib_atomicLoadARCRef(object: _loweredProgramStoragePtr) else { return nil } return unsafeDowncast(loweredObject, to: ProgramBox.self).value } @@ -161,7 +170,7 @@ extension Regex { // Compile the DSLTree into a lowered program and store it atomically. let compiledProgram = try! Compiler(tree: tree, compileOptions: compileOptions).emit() let storedNewProgram = _stdlib_atomicInitializeARCRef( - object: &_loweredProgramStorage, + object: _loweredProgramStoragePtr, desired: ProgramBox(compiledProgram)) // Return the winner of the storage race. We're guaranteed at this point From 74637cc6ae6735b2f44148fcb397f58d69dfdc97 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 31 Aug 2023 13:54:56 -0700 Subject: [PATCH 2/3] pass a pointer instead of inout conversion - this function is imported in a way that causes the compiler to not detect it as a C function --- Sources/_StringProcessing/Unicode/ScalarProps.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/_StringProcessing/Unicode/ScalarProps.swift b/Sources/_StringProcessing/Unicode/ScalarProps.swift index f5d9b5966..8864fd459 100644 --- a/Sources/_StringProcessing/Unicode/ScalarProps.swift +++ b/Sources/_StringProcessing/Unicode/ScalarProps.swift @@ -29,8 +29,10 @@ extension Unicode.Script { static func extensions(for scalar: Unicode.Scalar) -> [Unicode.Script] { var count: UInt8 = 0 - let pointer = _swift_string_processing_getScriptExtensions(scalar.value, &count) - + let pointer = withUnsafeMutablePointer(to: &count) { + _swift_string_processing_getScriptExtensions(scalar.value, $0) + } + guard let pointer = pointer else { return [Unicode.Script(scalar)] } From bc7042312a0cda5440e5d8f46e3216221107f79b Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 31 Aug 2023 15:39:28 -0700 Subject: [PATCH 3/3] Update Sources/_StringProcessing/Regex/Core.swift comment spelling fix --- Sources/_StringProcessing/Regex/Core.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_StringProcessing/Regex/Core.swift b/Sources/_StringProcessing/Regex/Core.swift index aa4a99207..78b8d1842 100644 --- a/Sources/_StringProcessing/Regex/Core.swift +++ b/Sources/_StringProcessing/Regex/Core.swift @@ -130,7 +130,7 @@ extension Regex { // This stored property should be stored at offset zero. We perform atomic // operations on it. // - /// Do not access this property directly - all accesses must go through `loweredProgram`. + /// Do not access this property directly - all accesses must go through `_loweredProgramStoragePtr `. fileprivate var _loweredProgramStorage: AnyObject? = nil /// The underlying IR.