Skip to content

feat: introduce await? to short-circuit fulfilled async futures #5215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
Jun 17, 2025

Conversation

ggreif
Copy link
Contributor

@ggreif ggreif commented Jun 4, 2025

Fixes #5212.

Previously one had to use await (without the *) to force an async future to a value. The extended semantics implemented for await? will opt out of the the mandatory state commit coming with await and return the value from the future instantly if already fulfilled.

The intuition for the syntax is that this causes 0 or 1 commit points (the former when the future is already fulfilled in an earlier await or await?).

FYI: @mraszyk

TODO:

  • IR-interpreter
  • docs

@ggreif ggreif self-assigned this Jun 6, 2025
Copy link
Contributor

github-actions bot commented Jun 7, 2025

Comparing from 4d2aedf to 087c2c7:
In terms of gas, 2 tests improved and the mean change is -0.0%.
In terms of size, 2 tests improved and the mean change is -0.0%.

@ggreif ggreif marked this pull request as ready for review June 8, 2025 08:29
@ggreif ggreif requested a review from a team as a code owner June 8, 2025 08:29
@crusso
Copy link
Contributor

crusso commented Jun 11, 2025

Thinking about this some more, maybe we should not overload await*, but instead add await? as a variant of await on futures.

Inspired by your pretty printing of IE and the fact that we are actually tweaking await, not await*.

Then your current await_sort also makes more sense I think.

Copy link
Contributor

@crusso crusso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good - I guess the final thing is to add documentation to the language manual. I.e. just add a line for await? in the the syntax of terms, and copy and adapt the section on await. Or treat both in the same section.

I wouldn't worry about the tutorial style doc pages for now (maybe just add a <!--TODO --->), though I guess a faster parallel await would be a nice example...

@ggreif ggreif requested review from crusso and jessiemongeon1 June 13, 2025 09:42
@crusso crusso mentioned this pull request Jun 16, 2025
crusso and others added 2 commits June 16, 2025 22:45
* adjust text

* reword

* reword

* Update doc/md/15-language-manual.md

* tweaks

* Apply suggestions from code review

Co-authored-by: Jessie Mongeon <[email protected]>

---------

Co-authored-by: Gabor Greif <[email protected]>
Co-authored-by: Jessie Mongeon <[email protected]>
assert (not env.flavor.has_await && env.flavor.has_async_typ);
begin match V.as_tup v2 with
| [vf; vr] ->
let (_, f) = V.as_func vf in
let (_, r) = V.as_func vr in
await env exp.at (V.as_async v1)
await env exp.at (sort <> T.AwaitFut false)(*FIXME?*) (V.as_async v1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. It was a FIXME, but now it should be fine. I was unsure whether AwaitCmp is possible here. It probably isn't, but we don't behave differently for that as before, even then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe an assert just in case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need, this code is working just fine for ages.

@ggreif ggreif requested a review from Kamirus June 17, 2025 09:25
} catch _ {}
};

public func state(shortCircuit : Bool) : async () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding a version of this test (or entire file) where let a = async { throw Prim.error(..) } (exiting via throw, not return) and we check the results accordingly.

Copy link
Contributor Author

@ggreif ggreif Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 087c2c7!

Copy link
Contributor

@crusso crusso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but maybe add one more test for await? on async that exits via throw

@ggreif ggreif added the automerge-squash When ready, merge (using squash) label Jun 17, 2025
@mergify mergify bot merged commit 3695173 into master Jun 17, 2025
28 checks passed
@mergify mergify bot deleted the gabor/fastpath branch June 17, 2025 12:35
@mergify mergify bot removed the automerge-squash When ready, merge (using squash) label Jun 17, 2025
ggreif added a commit that referenced this pull request Jun 18, 2025
…5215)

Fixes #5212.

Previously one had to use `await` (without the `*`) to force an `async` future to a value. The extended semantics implemented for `await?` will opt out of the the mandatory state commit coming with `await` and return the value from the future instantly if already fulfilled.

The intuition for the syntax is that this causes 0 or 1 commit points (the former when the future is already fulfilled in an earlier `await` or `await?`).

FYI: @mraszyk

TODO:
- [x] IR-interpreter
- [x] docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request typing Involves the type system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Order of arrival of message responses can delay execution
4 participants