Commit b137e4e
authored
[mypyc] Speed up native-to-native imports within the same group (#21101)
When compiling multiple modules, mypyc generally creates one big shared
library with all the code, and also tiny shim shared libraries for each
compiled module so that Python import machinery can find the modules.
This is inefficient at least on macOS, since each shared library that is
loaded into the process seems to have a non-trivial cost, including each
shim. On the first run, this cost is much higher, and the first mypy run
after `pip install` can take 30s or more on macOS.
This PR addresses the slow imports on macOS by adding a custom
implementation of native-to-native imports within the same compilation
group that avoids using the shim. We directly construct the module
object, populate `sys.modules`, and set an attribute in the parent
package, without using Python import machinery.
This speeds up a minimal mypy run (`mypy -c 'import os'`) on macOS by up
to 10x (first cold run after installation), but even small warm runs are
significantly faster. The measurements were all over the place, but at
least in one measurement the minimal warm run was over 1.5x faster with
these changes. Impact on Linux should be small (an earlier version of
this PR was slightly faster on Linux, but didn't measure the current
one). I haven't measured the impact on Windows.
Some notes about the implementation:
* Group similar imported names in `from <...> import` and try to
generate a single call to import multiple names to avoid verbose IR.
* When importing non-native modules or native modules defined in another
group, we still rely on Python import machinery.
* Various attributes are implicitly defined by Python when importing a
module, and I set these attributes explicitly.
* I split module init into two parts, since the attributes mentioned
above need to be set before running the module body.
* Avoid generating shims for some `__init__.py` files when compiling
mypy as a micro-optimization.
I used Claude Code and Codex to implement much of the code in small
increments (based on a manually written core implementation). I also
iterated on the code quite significantly after the basic implementation
was done.1 parent 978b711 commit b137e4e
File tree
12 files changed
+1378
-56
lines changed- mypyc
- codegen
- irbuild
- lib-rt
- primitives
- test-data
- test
12 files changed
+1378
-56
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
465 | 465 | | |
466 | 466 | | |
467 | 467 | | |
468 | | - | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
469 | 474 | | |
470 | 475 | | |
471 | 476 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
991 | 991 | | |
992 | 992 | | |
993 | 993 | | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
994 | 1000 | | |
995 | 1001 | | |
996 | 1002 | | |
| |||
1052 | 1058 | | |
1053 | 1059 | | |
1054 | 1060 | | |
1055 | | - | |
1056 | 1061 | | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
1057 | 1067 | | |
1058 | 1068 | | |
1059 | 1069 | | |
| |||
1098 | 1108 | | |
1099 | 1109 | | |
1100 | 1110 | | |
1101 | | - | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
1102 | 1114 | | |
1103 | 1115 | | |
1104 | 1116 | | |
| |||
1183 | 1195 | | |
1184 | 1196 | | |
1185 | 1197 | | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
1186 | 1222 | | |
1187 | 1223 | | |
1188 | 1224 | | |
1189 | 1225 | | |
1190 | 1226 | | |
1191 | 1227 | | |
1192 | | - | |
1193 | | - | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
1194 | 1231 | | |
1195 | 1232 | | |
| 1233 | + | |
1196 | 1234 | | |
1197 | 1235 | | |
1198 | 1236 | | |
1199 | 1237 | | |
1200 | 1238 | | |
1201 | 1239 | | |
1202 | 1240 | | |
1203 | | - | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
1208 | 1246 | | |
1209 | 1247 | | |
| 1248 | + | |
1210 | 1249 | | |
1211 | 1250 | | |
1212 | 1251 | | |
| |||
1219 | 1258 | | |
1220 | 1259 | | |
1221 | 1260 | | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
1222 | 1271 | | |
1223 | 1272 | | |
1224 | | - | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
| 1287 | + | |
| 1288 | + | |
| 1289 | + | |
1225 | 1290 | | |
1226 | 1291 | | |
1227 | 1292 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
100 | 106 | | |
101 | 107 | | |
102 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
62 | 70 | | |
63 | 71 | | |
64 | 72 | | |
| |||
76 | 84 | | |
77 | 85 | | |
78 | 86 | | |
| 87 | + | |
| 88 | + | |
79 | 89 | | |
80 | 90 | | |
81 | 91 | | |
| |||
96 | 106 | | |
97 | 107 | | |
98 | 108 | | |
| 109 | + | |
99 | 110 | | |
100 | 111 | | |
101 | 112 | | |
| |||
106 | 117 | | |
107 | 118 | | |
108 | 119 | | |
| 120 | + | |
109 | 121 | | |
110 | 122 | | |
111 | 123 | | |
| |||
130 | 142 | | |
131 | 143 | | |
132 | 144 | | |
| 145 | + | |
133 | 146 | | |
134 | 147 | | |
135 | 148 | | |
136 | 149 | | |
137 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
138 | 156 | | |
139 | 157 | | |
140 | 158 | | |
| |||
461 | 479 | | |
462 | 480 | | |
463 | 481 | | |
464 | | - | |
465 | | - | |
| 482 | + | |
| 483 | + | |
466 | 484 | | |
467 | 485 | | |
468 | | - | |
| 486 | + | |
469 | 487 | | |
470 | 488 | | |
471 | | - | |
472 | | - | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
473 | 532 | | |
474 | 533 | | |
475 | 534 | | |
| |||
1099 | 1158 | | |
1100 | 1159 | | |
1101 | 1160 | | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
1102 | 1175 | | |
1103 | 1176 | | |
1104 | 1177 | | |
| |||
0 commit comments