Skip to content
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

Make the Regex type Sendable #457

Merged
merged 8 commits into from
Jun 1, 2022
Merged

Conversation

natecook1000
Copy link
Member

@natecook1000 natecook1000 commented May 29, 2022

This is mostly trivial, with the exception of the lazy compiled regex. For that, we can use atomics to make storing the compiled program safe, even if it might result in an extra, thrown-away compilation.

  • So that we don't have to import swift-atomics, I've subset out the UnsafeAtomicLazyReference type and its dependencies.
  • I've added a test that consistently traps due to concurrent mutation under main, but succeeds with these changes.
  • This is now using the existing _stdlib_atomicInitializeARCRef function to synchronize initialization of the lazily compiled regex.

Regex stores a `Program` instance, which lazily lowers the DSLTree
into a compiled program. In unprotected, this lazy compilation is
under concurrency. This change uses UnsafeAtomicLazyReference
from `swift-atomics` as storage for the lowered program, which
makes storing the compiled program an atomic operation.
This test consistently traps on 'main', but succeeds with this
`Sendable` implementation.
@natecook1000 natecook1000 marked this pull request as ready for review May 30, 2022 04:16
@natecook1000
Copy link
Member Author

@swift-ci Please test

All the calls here are concrete; there's no need for conformances
@natecook1000
Copy link
Member Author

@swift-ci Please test

Package.swift Outdated Show resolved Hide resolved
Sources/_StringProcessing/Regex/Core.swift Show resolved Hide resolved
@natecook1000 natecook1000 marked this pull request as draft May 31, 2022 20:13
Didn't realize these methods existed; no need for shims or wrappers!
@natecook1000 natecook1000 marked this pull request as ready for review June 1, 2022 19:53
@natecook1000
Copy link
Member Author

@swift-ci Please test

We only need to synchronize the lazy initialization; accesses either
succeed or don't already.
@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000 natecook1000 merged commit 32afc43 into apple:main Jun 1, 2022
@natecook1000 natecook1000 deleted the sendable branch June 1, 2022 21:13
natecook1000 added a commit to natecook1000/swift-experimental-string-processing that referenced this pull request Jun 1, 2022
This change makes `Regex`, `RegexComponent`, and its component types `Sendable`.

Regex stores a `Program` instance, which lazily lowers the DSLTree into a 
compiled program. Without synchronization, this lazy compilation is unsafe 
under concurrency. This change uses atomic initialization for the compiled 
program.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants