Commit ada0d2a
authored
[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.1 parent 526fec3 commit ada0d2a
File tree
8 files changed
+251
-70
lines changed- mypyc
- irbuild
- test-data
8 files changed
+251
-70
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
264 | | - | |
| 264 | + | |
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
83 | | - | |
| 81 | + | |
84 | 82 | | |
85 | 83 | | |
86 | 84 | | |
87 | 85 | | |
88 | 86 | | |
89 | 87 | | |
90 | 88 | | |
91 | | - | |
| 89 | + | |
92 | 90 | | |
93 | 91 | | |
94 | 92 | | |
| |||
117 | 115 | | |
118 | 116 | | |
119 | 117 | | |
120 | | - | |
| 118 | + | |
121 | 119 | | |
122 | 120 | | |
123 | 121 | | |
| |||
153 | 151 | | |
154 | 152 | | |
155 | 153 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
160 | 157 | | |
161 | 158 | | |
162 | 159 | | |
| |||
216 | 213 | | |
217 | 214 | | |
218 | 215 | | |
219 | | - | |
220 | 216 | | |
221 | 217 | | |
222 | 218 | | |
223 | 219 | | |
224 | | - | |
225 | | - | |
226 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
227 | 223 | | |
228 | | - | |
| 224 | + | |
229 | 225 | | |
230 | 226 | | |
231 | 227 | | |
232 | 228 | | |
233 | 229 | | |
234 | 230 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
| 231 | + | |
240 | 232 | | |
241 | 233 | | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
| 234 | + | |
259 | 235 | | |
260 | 236 | | |
261 | 237 | | |
| |||
272 | 248 | | |
273 | 249 | | |
274 | 250 | | |
275 | | - | |
276 | | - | |
277 | | - | |
| 251 | + | |
278 | 252 | | |
279 | 253 | | |
280 | 254 | | |
| |||
289 | 263 | | |
290 | 264 | | |
291 | 265 | | |
292 | | - | |
293 | | - | |
294 | | - | |
| 266 | + | |
295 | 267 | | |
296 | 268 | | |
297 | 269 | | |
| |||
307 | 279 | | |
308 | 280 | | |
309 | 281 | | |
310 | | - | |
311 | | - | |
312 | | - | |
| 282 | + | |
313 | 283 | | |
314 | 284 | | |
315 | 285 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
41 | 45 | | |
42 | 46 | | |
43 | 47 | | |
| |||
76 | 80 | | |
77 | 81 | | |
78 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
79 | 92 | | |
80 | 93 | | |
81 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
67 | 69 | | |
68 | 70 | | |
69 | 71 | | |
| |||
171 | 173 | | |
172 | 174 | | |
173 | 175 | | |
174 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
175 | 184 | | |
176 | 185 | | |
177 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
55 | 62 | | |
56 | 63 | | |
57 | 64 | | |
| |||
115 | 122 | | |
116 | 123 | | |
117 | 124 | | |
118 | | - | |
| 125 | + | |
119 | 126 | | |
120 | 127 | | |
121 | 128 | | |
| |||
179 | 186 | | |
180 | 187 | | |
181 | 188 | | |
| 189 | + | |
| 190 | + | |
182 | 191 | | |
183 | 192 | | |
184 | 193 | | |
| |||
190 | 199 | | |
191 | 200 | | |
192 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
193 | 234 | | |
194 | 235 | | |
195 | 236 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| |||
930 | 932 | | |
931 | 933 | | |
932 | 934 | | |
933 | | - | |
934 | | - | |
935 | | - | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
936 | 948 | | |
937 | | - | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
938 | 954 | | |
939 | 955 | | |
940 | 956 | | |
941 | 957 | | |
942 | | - | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
943 | 970 | | |
944 | 971 | | |
945 | 972 | | |
| |||
948 | 975 | | |
949 | 976 | | |
950 | 977 | | |
951 | | - | |
| 978 | + | |
952 | 979 | | |
953 | 980 | | |
954 | 981 | | |
| |||
0 commit comments