Commit 34e2c80
committed
perf: closes #753 — skip eager init for modules reached only via dynamic import()
Follow-up to #100/#752. The dynamic-import resolver registered every
target as a regular Import with `is_dynamic: true` and put it in the
eager init chain at program start — functionally correct, but a heavy
locale bundle or optional feature module was paid upfront even when no
dispatch site ever fired.
Reachability classification (compile.rs): fixed-point pass starting from
the entry, propagating Eager across non-type-only static imports and
re-export sources. Everything unmarked is Deferred. Writes the result
to each Module::init_kind (HIR field from #100, no codegen consulted
it before).
Codegen (codegen.rs + expr.rs):
- Entry main filters `deferred_module_prefixes` out of the eager init
call sequence — Deferred modules only fire from dispatch sites.
- Each non-entry module gains a 3-block wrapper `<prefix>__init` (load
`@__perry_init_done_<prefix>`, icmp ne 0, cond_br to ret-or-do; do
block stores 1, calls dep wrappers transitively, then calls
`<prefix>__init_body`). Existing body code keeps every semantic;
rename to `_body` is invisible to other code paths.
- `Expr::DynamicImport` (single + multi-path arms) calls
`<target>__init` before loading `@__perry_ns_<target_prefix>`. For
Eager targets the guard short-circuits; for Deferred targets it's
the only invocation that builds the namespace.
- Entry emits a no-op `<entry_prefix>__init` stub so a non-entry
module dispatching `await import("./entry.ts")` resolves at link.
The entry's actual body still runs in main; the stub just satisfies
the dispatch's unconditional init call.
Module init deps (per-module: static-import + re-export sources) are
plumbed to the wrapper's do block so a Deferred module that reaches
another Deferred only through its own re-export chain still initializes
the source before its namespace populator runs.
Cache key hashes deferred_module_prefixes (sorted) and module_init_deps
(ordered) so a program that gains or loses a dynamic-import reachability
path invalidates the entry's cached .o.
Acceptance:
- All 7 dynamic-import gap tests from #100 (literal, ternary, template,
reexport, tla, cycle, init_time) byte-equal `node --experimental-strip-types`.
- New test_gap_dynamic_import_deferred.ts covers the Deferred-only case:
marker module's top-level console.log fires only on the dispatch
branch, never on the no-arg path.
- cargo test --workspace green (excluding cross-host UI crates).
Benchmark (heavy.ts builds a 1M-entry int array at top level,
main.ts dynamically imports it only when argv[2] === 'use'):
| | PRE-#753 | POST-#753 |
|--------------|-----------------------|-----------------------|
| no-arg | 8.4 ms ± 1.6 (min 7.4) | 4.8 ms ± 2.2 (min 3.1) |
| use | 7.6 ms ± 0.4 (min 7.1) | 8.4 ms ± 0.7 (min 7.5) |
hyperfine -N -w 5 -r 30. No-arg branch drops by 43% on mean / 58% on
min — that 4 ms is the for-loop that no longer runs at startup. The
`use` branch is statistically indistinguishable; the heavy init still
runs, just lazily, with one extra call indirection.1 parent 8a7ea99 commit 34e2c80
6 files changed
Lines changed: 354 additions & 3 deletions
File tree
- crates
- perry-codegen/src
- perry/src/commands
- compile
- test-files
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
229 | 250 | | |
230 | 251 | | |
231 | 252 | | |
| |||
483 | 504 | | |
484 | 505 | | |
485 | 506 | | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
486 | 518 | | |
487 | 519 | | |
488 | 520 | | |
| |||
1325 | 1357 | | |
1326 | 1358 | | |
1327 | 1359 | | |
| 1360 | + | |
| 1361 | + | |
1328 | 1362 | | |
1329 | 1363 | | |
1330 | 1364 | | |
| |||
2715 | 2749 | | |
2716 | 2750 | | |
2717 | 2751 | | |
| 2752 | + | |
| 2753 | + | |
| 2754 | + | |
| 2755 | + | |
| 2756 | + | |
| 2757 | + | |
| 2758 | + | |
2718 | 2759 | | |
2719 | 2760 | | |
2720 | 2761 | | |
| |||
3947 | 3988 | | |
3948 | 3989 | | |
3949 | 3990 | | |
| 3991 | + | |
| 3992 | + | |
| 3993 | + | |
| 3994 | + | |
| 3995 | + | |
| 3996 | + | |
| 3997 | + | |
| 3998 | + | |
| 3999 | + | |
| 4000 | + | |
| 4001 | + | |
| 4002 | + | |
| 4003 | + | |
| 4004 | + | |
| 4005 | + | |
3950 | 4006 | | |
3951 | 4007 | | |
3952 | 4008 | | |
| |||
4018 | 4074 | | |
4019 | 4075 | | |
4020 | 4076 | | |
| 4077 | + | |
| 4078 | + | |
| 4079 | + | |
| 4080 | + | |
| 4081 | + | |
| 4082 | + | |
| 4083 | + | |
| 4084 | + | |
| 4085 | + | |
4021 | 4086 | | |
| 4087 | + | |
| 4088 | + | |
| 4089 | + | |
4022 | 4090 | | |
4023 | 4091 | | |
4024 | 4092 | | |
| |||
4282 | 4350 | | |
4283 | 4351 | | |
4284 | 4352 | | |
| 4353 | + | |
| 4354 | + | |
| 4355 | + | |
| 4356 | + | |
| 4357 | + | |
| 4358 | + | |
| 4359 | + | |
| 4360 | + | |
| 4361 | + | |
| 4362 | + | |
| 4363 | + | |
| 4364 | + | |
| 4365 | + | |
| 4366 | + | |
| 4367 | + | |
4285 | 4368 | | |
| 4369 | + | |
| 4370 | + | |
| 4371 | + | |
| 4372 | + | |
| 4373 | + | |
| 4374 | + | |
| 4375 | + | |
| 4376 | + | |
| 4377 | + | |
| 4378 | + | |
| 4379 | + | |
| 4380 | + | |
| 4381 | + | |
| 4382 | + | |
| 4383 | + | |
| 4384 | + | |
| 4385 | + | |
| 4386 | + | |
| 4387 | + | |
| 4388 | + | |
| 4389 | + | |
| 4390 | + | |
| 4391 | + | |
| 4392 | + | |
| 4393 | + | |
| 4394 | + | |
| 4395 | + | |
| 4396 | + | |
| 4397 | + | |
| 4398 | + | |
| 4399 | + | |
| 4400 | + | |
| 4401 | + | |
| 4402 | + | |
| 4403 | + | |
| 4404 | + | |
| 4405 | + | |
| 4406 | + | |
| 4407 | + | |
| 4408 | + | |
| 4409 | + | |
| 4410 | + | |
| 4411 | + | |
| 4412 | + | |
| 4413 | + | |
| 4414 | + | |
| 4415 | + | |
| 4416 | + | |
| 4417 | + | |
| 4418 | + | |
| 4419 | + | |
| 4420 | + | |
| 4421 | + | |
| 4422 | + | |
| 4423 | + | |
| 4424 | + | |
| 4425 | + | |
4286 | 4426 | | |
4287 | 4427 | | |
4288 | 4428 | | |
| |||
4295 | 4435 | | |
4296 | 4436 | | |
4297 | 4437 | | |
| 4438 | + | |
4298 | 4439 | | |
4299 | 4440 | | |
4300 | 4441 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10819 | 10819 | | |
10820 | 10820 | | |
10821 | 10821 | | |
10822 | | - | |
| 10822 | + | |
| 10823 | + | |
| 10824 | + | |
| 10825 | + | |
| 10826 | + | |
| 10827 | + | |
| 10828 | + | |
| 10829 | + | |
| 10830 | + | |
10823 | 10831 | | |
10824 | 10832 | | |
10825 | 10833 | | |
| |||
10890 | 10898 | | |
10891 | 10899 | | |
10892 | 10900 | | |
10893 | | - | |
10894 | | - | |
| 10901 | + | |
| 10902 | + | |
| 10903 | + | |
| 10904 | + | |
| 10905 | + | |
| 10906 | + | |
10895 | 10907 | | |
10896 | 10908 | | |
10897 | 10909 | | |
| 10910 | + | |
10898 | 10911 | | |
10899 | 10912 | | |
10900 | 10913 | | |
| |||
0 commit comments