Skip to content

[nfc] Avoid pointer conversions #691

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 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 14 additions & 5 deletions Sources/_StringProcessing/Regex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 `_loweredProgramStoragePtr `.
fileprivate var _loweredProgramStorage: AnyObject? = nil

/// The underlying IR.
///
/// FIXME: If Regex is the unit of composition, then it should be a Node instead,
Expand All @@ -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<AnyObject?> {
_getUnsafePointerToStoredProperties(self)
.assumingMemoryBound(to: Optional<AnyObject>.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
}
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions Sources/_StringProcessing/Unicode/ScalarProps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
}
Expand Down