Multiple vs single await

Is there a (noticeable) difference between something like this:

async fn f() {
match foo {
  Foo(v) => {
    func(X::x(v)).await;
  }
  Bar(x) => {
    func(X::y).await;
  }
  (...)
}
}

and

async fn f() {
let p;
match foo {
  Foo(v) => {
    p = X::x(v);
  }
  Bar(x) => {
    p = X::y;
  }
  (...)
}
func(p).await;
}

From my understanding the second one should create less "states", but I don't know if it would be better or worse.
Also the second one seems "cleaner" to me, but that's subjective.

You are probably correct that the second version will compile to a simpler state machine. Note that it is more idiomatic to initialize p like this:

let p = match foo {
    Foo(v) => X::x(v),
    Bar(x) => X::y,
};
3 Likes

IIRC the state is an enum and enum is as big as the largest variant of the enum, so more but smaller variants would take up less space than one big variant (I hope you get what I mean).
But now that I think about it, most of the things used would be no longer needed at the end of the function (when caling await) so maybe the state would actually be smaller?
However it turns out that my code actually can't be simplified to either of this versions.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.