[Prospective Vision] Improving the approachability of data-race safety

Oh man, do I agree with the diagnosis in this vision document.

However, I'm not sure I agree with (some of) the prescription here. @MainActor-all-the-things by default doesn't work well for image processing and other CPU-intensive work. I fear that this change would set us up for a kind of brick wall at the point where you do want to do something in the background. If you realize late that doing this kind of work on the main actor is a bad idea, your code will just explode with warnings and errors as switch the build mode of a module from single-threaded to anything else.

Essentially I fear a similar experience to what you find when you first switch a module from Swift 5 mode to Swift 6. It's not pleasant. My horror story is using Apollo iOS in Swift 6 mode and trying to bridge its API into async/await with task cancellation ultimately leading me to read posts like this one. I have no hope that junior members on my team would have been able to make this migration work.

One specific pain point I had that this proposal addresses (I think) is one where refactoring an actor into multiple classes/structs causes an absolute explosion in complexity, unless you figure out that you need to declare explicit isolation parameters to stay within the actor's context. This is highly non-obvious, and is never, ever suggested by compiler diagnostics.

In my codebase I solved this issue by forcing my actor to be a global actor and then annotating everything around it that way, but this greatly reduces the reusability of these components--now they're stuck on that specific actor, even if they'd be useful elsewhere. Changing the default of async functions to propagate isolation would make this so much easier.

What's particularly attractive about this is that you can make a break at a specific point in your code where you know you need to delegate to some background thing--say by changing a class to an actor--but the implementation details of that class-now-actor are otherwise unaffected. This is crucial to making incremental adoption so much easier than it is today.

3 Likes