Swift 6.2 Concurrency Modernization Plan #184
Replies: 1 comment
-
Cross-Platform Compatibility (macOS, Linux, Windows)Will it work the same on all platforms?Yes, with caveats. Swift actors and structured concurrency are fully cross-platform:
Current NSLock-based code also works cross-platform via Foundation/swift-corelibs-foundation, so we're essentially replacing one cross-platform solution with another. Platform-Specific Considerations
Risks of This RefactoringHigh Risk
Medium Risk
Low Risk
Risk Mitigation Strategies1. Incremental Rollout with Feature Flags// Temporary toggle during migration
enum ConcurrencyMode {
case legacy // NSLock-based
case modern // Actor-based
}
let concurrencyMode: ConcurrencyMode = .modernThis allows quick rollback if issues arise. 2. Parallel Implementation StrategyKeep both implementations during transition: // Old: Keep as internal
final class ActionRegistryLegacy: @unchecked Sendable { ... }
// New: Public API
public actor ActionRegistry { ... }Run both in tests, compare results. 3. Comprehensive BenchmarkingBefore each phase, establish baselines: // Benchmark hot paths
measure {
for _ in 0..<10000 {
context.bind("key", value: "value")
_ = context.resolve("key")
}
}Reject changes that regress by >10%. 4. Platform-Specific CI MatrixEnsure every PR runs tests on:
5. C Bridge IsolationKeep the C bridge as a separate phase (Phase 6). Don't touch it until the runtime refactor is stable. 6. Actor Reentrancy AuditDocument every actor EventBus {
func publish(_ event: Event) async {
// DANGER: State could change during await
let count = subscriptions.count
await notifySubscribers(event) // ⚠️ Suspension point
// subscriptions.count might differ now!
}
}Mitigate with:
When Is the Best Time for This Refactoring?Ideal Timing
Not Ideal Timing
Recommended Approach
Suggested TimelineStart with Phase 1 (Registries) as a low-risk pilot:
If Phase 1 succeeds without issues, proceed with confidence. If problems arise, the scope is contained. Summary
The refactoring is worthwhile but should be approached methodically. Start small, measure everything, and keep rollback options available. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Analysis of ARO's concurrency patterns compared to Swift 6.2 best practices from the article "Mastering Swift 6.2 Concurrency".
Current State
The ARO codebase uses a hybrid async/await + NSLock model:
@unchecked SendableclassesRepositoryStorageActorasync/awaitoccurrencesDispatchSemaphoreusagenonisolated(unsafe)Key Swift 6.2 Features We Could Adopt
@unchecked Sendable+NSLock#isolationparameter@concurrentmacrononisolated(nonsending)async letProposed Changes
Phase 1: Convert Registries to Actors (Small effort)
Current pattern:
Proposed pattern:
Files:
Sources/ARORuntime/Actions/ActionRegistry.swiftSources/ARORuntime/SystemObjects/SystemObjectRegistry.swiftSources/ARORuntime/Services/ServiceRegistry.swiftPhase 2: Convert EventBus to Actor (Medium effort)
Complex state management with 4 mutable collections currently protected by NSLock.
File:
Sources/ARORuntime/Events/EventBus.swiftPhase 3: Convert RuntimeContext + GlobalSymbolStorage (Medium effort)
Hot path for variable binding/resolution - needs benchmarking.
Files:
Sources/ARORuntime/Core/RuntimeContext.swiftSources/ARORuntime/Core/ExecutionEngine.swift(GlobalSymbolStorage)Phase 4: Convert StatementScheduler (Medium effort)
Continuation management and futures system.
File:
Sources/ARORuntime/Core/StatementScheduler.swiftPhase 5: Add Actor Isolation Inheritance (Small effort)
Use
#isolationpattern to avoid unnecessary executor hops:Phase 6: Refactor C Bridge (Large effort)
Current issues:
nonisolated(unsafe)globals in RuntimeBridge.swiftNSLock+nonisolated(unsafe)in ServiceBridge.swiftDispatchSemaphorefor sync-to-async bridgingProposed approach:
assumeIsolatedfor C callbacksFiles:
Sources/AROCRuntime/RuntimeBridge.swiftSources/AROCRuntime/ServiceBridge.swiftSources/AROCRuntime/ActionBridge.swiftPhase 7-9: Additional Improvements (Small effort each)
async letpatterns for parallel operations@concurrentto CPU-intensive methodsFull Class Conversion List
ActionRegistryActions/ActionRegistry.swift:27SystemObjectRegistrySystemObjects/SystemObjectRegistry.swift:41ExternalServiceRegistryServices/ServiceRegistry.swift:102EventBusEvents/EventBus.swift:16RuntimeContextCore/RuntimeContext.swift:12GlobalSymbolStorageCore/ExecutionEngine.swift:724DependencyGraphCore/DependencyGraph.swift:67StatementSchedulerCore/StatementScheduler.swift:124StatementFutureCore/StatementScheduler.swift:38EventLoopGroupManagerCore/EventLoopGroupManager.swift:15ShutdownCoordinatorActions/BuiltIn/ServerActions.swiftBenefits
Testing Strategy
aro build)Discussion
Would love feedback on:
Beta Was this translation helpful? Give feedback.
All reactions