Skip to content

Load modules lazily and only if needed #14453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 28, 2025
Merged

Load modules lazily and only if needed #14453

merged 12 commits into from
Apr 28, 2025

Conversation

josevalim
Copy link
Member

@josevalim josevalim commented Apr 26, 2025

Prior to this pull request, Elixir would load modules as soon as
they were defined. However, because Erlang code loading
happens within a single process (the code server), this would
make it a bottleneck, reducing the amount of parallelization.

This pull request makes it so modules are loaded lazily. This
reduces the pressure on the code server, making compilation
up to 2x faster for large projects, and also reduces the amount
of overall work done.

Implementation wise, the parallel compiler already acts as a
mechanism to resolve modules during compilation, so we build
on that. This has the additional benefit of making compilation more
consistent: Code.ensure_loaded will be less dependent on the
order code is compiled.

The only potential regression in this approach happens if you
have @on_load callbacks which depends on other modules
defined in the same project and is loaded at compile-time.
Setting @compile {:autoload, true} addresses those cases
in a forward and backwards compatible manner.

@josevalim josevalim changed the title Load modules lazily Load modules lazily and only if needed Apr 26, 2025
@josevalim josevalim merged commit 64a387d into main Apr 28, 2025
22 checks passed
@josevalim josevalim deleted the jv-lazy-load branch April 28, 2025 10:45
@josevalim
Copy link
Member Author

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant