Skip to content

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.

2 participants