Skip to content
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

Possible optimization: Eager task execution #118

Open
JelleZijlstra opened this issue May 4, 2023 · 0 comments
Open

Possible optimization: Eager task execution #118

JelleZijlstra opened this issue May 4, 2023 · 0 comments

Comments

@JelleZijlstra
Copy link
Collaborator

JelleZijlstra commented May 4, 2023

CPython just applied an optimization to asyncio which avoids creating a full-fledged Task object if possible: python/cpython#97696. This apparently gives up to a 50% speedup on some asyncio benchmarks.

In principle, the same optimization should apply to asynq: we always create an AsynqTask and put it on the scheduler for every executed asynq function, but many asynq function likely complete without ever needing to block. I don't have hard numbers for that, though.

This optimization could be implemented mostly in _call_pure (

def _call_pure(self, args, kwargs):
):

  • If self.needs_wrapper is true, we have an @asynq function that never yields. We can just return a ConstFuture directly without going through the scheduler.
  • Otherwise, we do need to execute the generator. We can start the first step of the generator. If it returns, we can end early too and return a ConstFuture. If it yields, we can check whether it's yielding a ConstFuture: if so, we can simply send the ConstFuture back into the generator and proceed. But if it yields something else, we have to fall back to using the real scheduler.

This change could break user-visible behavior in some ways: for example, there is code in the @deduplicate decorator that assumes .asynq() always returns an AsyncTask, not a ConstFuture. Adapting such code may need some additional work. Compare python/cpython#97696 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant