You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[mypyc] Call generator helper method directly in await expression (#19376)
Previously calls like `await foo()` were compiled to code that included
code like this (in Python-like pseudocode):
```
a = foo()
...
b = get_coro(a)
...
c = next(b)
```
In the above code, `get_coro(a)` just returns `a` if `foo` is a native
async function, so we now optimize this call away. Also `next(b)` calls
`b.__next__()`, which calls the generated generator helper method
`__mypyc_generator_helper__`. Now we call the helper method directly,
which saves some unnecessary calls.
More importantly, in a follow-up PR I can easily change the way
`__mypyc_generator_helper__` is called, since we now call it directly.
This makes it possible to avoid raising a `StopIteration` exception in
many await expressions. The goal of this PR is to prepare for the latter
optimization. This PR doesn't help performance significantly by itself.
In order to call the helper method directly, I had to generate the
declaration of this method and the generated generator class before the
main irbuild pass, since otherwise a call site could be processed before
we have processed the called generator.
I also improved test coverage of related functionality. We don't have an
IR test for async calls, since the IR is very verbose. I manually
inspected the generated IR to verify that the new code path works both
when calling a top-level function and when calling a method. I'll later
add a mypyc benchmark to ensure that we will notice if the performance
of async calls is degraded.
0 commit comments