Is the compiler allowed to coalesce actor-isolated method calls?

suppose i have

actor A
{
    func x() -> X
    func y() -> Y
}

and i want to use it to initialize some (non-Sendable) structure like:

init(a:A) async
{
    self.init(
        x: await a.x(),
        y: await a.y())
}

formally, this would acquire and release the actor lock twice to allow other code to run in between, so i would be tempted to refactor the two methods into func xy() -> (X, Y).

but is this needed? is the compiler able to coalesce the lock acquisitions into a single isolation?

2 Likes

If you want a guarantee of atomicity, you should make a single call, of course. But yes, as an optimization, Swift can execute this without dynamically switching away from the actor between the calls.

8 Likes