Hey
everyone,
This is an updated version of the pitch with more precise terminology and better examples.
Motivation
Before Swift 6.2, a nonisolated async function always ran on the global concurrent executor:
struct ImageCompressor {
func compress(_ image: Data) async -> Data {
// Before SE-0461: always runs on the global concurrent executor
}
}
With SE-0461 (enabled via the NonisolatedNonsendingByDefault upcoming feature, part of Xcode's "Approachable Concurrency" setting), nonisolated async functions now run on the caller's actor by default. If compress(_:) is called from the main actor, all of its synchronous work now happens on the main actor.
To get the old behavior back, we annotate the function with @concurrent:
struct ImageCompressor {
@concurrent func compress(_ image: Data) async -> Data { β¦ }
}
This is the right default for most code. But some types exist specifically to do heavy CPU-bound work β image compression, decoding large JSON payloads, hashing files, data processing in use cases after fetching from a backend. For these types, every async method needs @concurrent:
struct ImageCompressor {
@concurrent func compress(_ image: Data) async -> Data { β¦ }
@concurrent func resize(_ image: Data, to size: CGSize) async -> Data { β¦ }
@concurrent private func encode(_ image: CGImage) async -> Data { β¦ }
}
This is repetitive and easy to get wrong. Forgetting a single @concurrent doesn't produce a compiler error β it silently runs heavy work on the caller's actor (often the main actor), which is exactly the kind of performance bug the annotation exists to prevent.
Proposed solution
I propose a @Concurrent attached macro that can be applied to struct, class, and enum declarations, as well as extensions. It adds the @concurrent attribute to every nonisolated async method in that declaration:
@Concurrent
struct ImageCompressor {
func compress(_ image: Data) async -> Data { ... } // macro adds @concurrent
private func encode(_ image: CGImage) async -> Data { ... } // macro adds @concurrent
}
It can also be scoped to a single extension:
struct FetchDataUseCase { β¦ }
@Concurrent
extension FetchDataUseCase {
// macro adds @concurrent only to async methods in this extension
func process(_ response: Response) async -> Model { β¦ }
}
Detailed behavior
- Only
asyncmethods are affected. Synchronous methods are untouched β they run wherever they're called, exactly as today.@concurrentonly changes where nonisolatedasyncfunctions execute, so the macro skips synchronous methods entirely. - Per-method opt-out wins. A method explicitly marked
nonisolated(nonsending),@MainActor, or with any other explicit isolation would be skipped by the macro. - Not allowed on actors. Applying it to an
actorcontradicts the whole point of actor isolation, so the macro would emit an error there. - Naming. I'm aware
@Concurrentdiffers from@concurrentonly by capitalization, and I'm open to bikeshedding the name (e.g.@ConcurrentMembers) if that's a concern.
Alternatives considered
- Status quo: annotate every method manually. Works, but it's boilerplate with a silent failure mode.
- Extending the
@concurrentattribute itself to types and extensions: arguably the most consistent design, but it requires compiler changes, while a macro can be implemented and shipped without touching type checking. If the community prefers the attribute route, I'd be happy to pursue that direction instead.
I keep running into this pattern at work β use cases doing data processing after backend calls, image compression pipelines, and similar CPU-bound helpers β so I think this would benefit a lot of codebases adopting Swift 6.2's new defaults.
What do you think?