Module compilation taking more than 10s

When I compile my application, I see a lot of messages like:

Compiling some/module/file.ex (it’s taking more than 10s)

I understand that this message is appearing because the compilation of those modules is slow, but I don’t know why is this and where to start looking for possible causes. Any suggestions?

Is your project public?

Modules are known to take long when you have a lot of macrocalls or functions with a lot of heads. Before 1.5.somewhat compile time of a function grew squared to its number of heads. I’m not sure when this was fixed (or if at all, maybe fix is master only)

Unfortunately no. It belongs to the company I am working for.

I don’t know if it has something to do with it, but in the 95% of those warnings, the affected module is a controller or a view.

Another reason for slow compilation is if given module depends on other slow modules. [1] Is a great resource for understanding compilation. In general, you want to avoid compile-time dependencies between modules as much as possible.

On our Phoenix app, switching from import MyApp.Router.Helpers to alias MyApp.Router.Helpers, as: Routes speed up compilation and limited re-compilations greatly, see: [2]

Elixir 1.6 will ship with improvements to mix xref which should make it easier to investigate this stuff.

[1] http://milhouseonsoftware.com/2016/08/11/understanding-elixir-recompilation/
[2] https://github.com/phoenixframework/phoenix/issues/2530
[3] https://github.com/elixir-lang/elixir/blob/master/CHANGELOG.md#mix-xref

4 Likes

Hm, we may have something here :thinking:
Running mix xref graph --source on those slow modules usually shows the router as a compile time dependency, which in turn depends on lots of controllers, which depend on models, etc.

I am going to try aliasing the router module instead of importing it and check the result.
Thank you @wojtekmach

1 Like