Does the Elixir compiler tree-shake any unused functions from an imported module?

In this case, considering we don’t have any overlapping function names, is it worth it to use only as a means of asserting that we’re only importing the from public function from the Ecto.Query module or will the compiler tree-shake all unused functions from Ecto.Query regardless?

import Ecto.Query, only: [from: 2]
1 Like

The short answer is yes.

To add some pedantry, I wouldn’t call it “treeshaking” per se. import is more “opening up the module” to use for function resolution, not so much “importing” it as you would think about it in JS. All imported functions are being expanding to their fully qualified names during compilation.

While :only isn’t strictly necessary, it is nice for when someone comes blind to a function in your module and it may not be obvious where a particular function is coming from if import is used multiple times. With your example of Ecto.Query, though, I never both specifying from because it’s generally super obvious by the context of the file what from is. It’s context-dependent and, of course, a matter of taste!

But ya, unused functions are not compiled.

6 Likes

Just to be clear though, the functions will exist inside the Ecto.Query module regardless of whether they are used anywhere or not. import is only lexical, and calls to imported functions are translated into remote calls a la Ecto.Query.from.

6 Likes

Ya sorry, very poor wording. And reading over my response I see more poor grammar :sweat_smile: :face_with_peeking_eye:

2 Likes