Inlining await for decreasing the number of checkpoints

Consider this code:

func f(): Async () {
  // ...
};

func g(): Async () {
  await f();
};

func h(): Async () {
  await g();
};

During execution of g, await is hit twice. So, if I understand correctly, there are two “checkpoints” where the state can be changed by another “thread”.

But the code for h is “equivalent” to:

func f(): Async () {
  // ...
};

func h(): Async () {
  await f();
};

where we have just one checkpoint.

If I want to decrease the quantity of checkpoints, is there any better solution than to inline function code? (that // ... in f)

Yes, there is this solution:

func f() : async () {
 // ..
};

func g() : async* () {
  await f();
};

func h() : async* () {
  await* g();
};

I remember there was a good, lengthy explanation in the release notes (or somewhere else on github) when this was introduced, in moc 0.7.4. But now I don’t find it anymore. The release notes for 0.7.4. are brief. Must have been somewhere else on github but I can’t find it.

async* await* is of course explained in the language manual: Internet Computer Loading

1 Like

Also see Star.mo - Dealing with async* - A Library for a library to kelp you track if your check point was hit or not.

When an async* returns you won’t know if a checkpoint triggered or not unless you mange it. I recommend never calling async* unless it returns this(or similar) structure. of course if you ALWAYS await this isn’t the case…but any branches induce a safety issue.