Skip to content

Atomically load the lowered program #610

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 1 commit into from
Oct 6, 2022
Merged
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
27 changes: 22 additions & 5 deletions Sources/_StringProcessing/Regex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,29 @@ extension Regex {

/// The program for execution with the matching engine.
var loweredProgram: MEProgram {
if let loweredObject = _loweredProgramStorage as? ProgramBox {
return loweredObject.value
/// Atomically loads the compiled program if it has already been stored.
func loadProgram() -> MEProgram? {
guard let loweredObject = _stdlib_atomicLoadARCRef(object: &_loweredProgramStorage)
else { return nil }
return unsafeDowncast(loweredObject, to: ProgramBox.self).value
}
let lowered = try! Compiler(tree: tree, compileOptions: compileOptions).emit()
_stdlib_atomicInitializeARCRef(object: &_loweredProgramStorage, desired: ProgramBox(lowered))
return lowered

// Use the previously compiled program, if available.
if let program = loadProgram() {
return program
}

// 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,
desired: ProgramBox(compiledProgram))

// Return the winner of the storage race. We're guaranteed at this point
// to have compiled program stored in `_loweredProgramStorage`.
return storedNewProgram
? compiledProgram
: loadProgram()!
}

init(ast: AST) {
Expand Down