Commit c6cd07f
Speed up zero-argument class patterns in match statements
A class pattern with no sub-patterns (`case C():`) is just an isinstance
check, but it compiled to LOAD_COMMON_CONSTANT (empty names tuple) +
MATCH_CLASS + COPY + POP_JUMP_IF_NONE + UNPACK_SEQUENCE 0, building and
unpacking an empty attributes tuple every time.
Compile it instead to a CALL_INTRINSIC_2 invoking a new
INTRINSIC_MATCH_CLASS_ISINSTANCE, which performs the same PyType_Check
and PyObject_IsInstance as _PyEval_MatchClass (including the identical
TypeError when the pattern does not name a class) and pushes a bool.
No new opcode is added.
This is ~25% faster on a matching subject and ~14% faster on a
non-matching subject for the zero-argument case, and is even faster than
an equivalent isinstance() call since the intrinsic avoids a Python-level
call frame.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 1b0c05f commit c6cd07f
3 files changed
Lines changed: 32 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | | - | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6180 | 6180 | | |
6181 | 6181 | | |
6182 | 6182 | | |
| 6183 | + | |
| 6184 | + | |
| 6185 | + | |
| 6186 | + | |
| 6187 | + | |
| 6188 | + | |
| 6189 | + | |
| 6190 | + | |
| 6191 | + | |
| 6192 | + | |
| 6193 | + | |
6183 | 6194 | | |
6184 | 6195 | | |
6185 | 6196 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
257 | 275 | | |
258 | 276 | | |
259 | 277 | | |
| |||
278 | 296 | | |
279 | 297 | | |
280 | 298 | | |
| 299 | + | |
281 | 300 | | |
282 | 301 | | |
283 | 302 | | |
| |||
0 commit comments