Performance impact when using "_var" over "_" in Elixir?

There is a general rule for designating any unused variables with _ in Elixir. This prevents everything related to this variable.

However, I noticed a commonly used underscore prefix pattern to indicate an ignored argument in the form of _tail (with the intention of indicating a hint about which variable will be).

This is recommended by the language with a warning in the shell if you try to access _tail :

warning: after installation, the underlined variable "_tail" is used. The underlined underline means that the value of the variable should be ignored. Rename this variable if necessary to remove the underline.

But here is the catch; _tail has a variable associated with it, whereas when using only _ it is not.

Does this mean that there is a performance limitation when naming ignored variables with anything other than _ ? Or does Elixir still bind _ backstage and just a mistake on any access attempt?

Edit: It seems that the Erlang compiler specifically optimizes this case to handle _* as _ and therefore there is no overhead, source: http://erlang.org/doc/efficiency_guide/myths.html

+7
elixir
source share
2 answers

Given that everyone has already given a disclaimer for this performance behavior, the answer is this: if the variable is not used, the compiler will notice it, and the compiled bytecode will simply ignore it, as if you were using _ . For the same reason, if you do x = 1 and never x , you get a compiler warning.

+6
source share

Indeed, there is a special behavior in Erlang (and therefore Elixir) for the variable _ . But if you did not notice that this is a performance issue for your application, I would not be too worried about that. I assume that the overhead of the required variables would be completely negligible if you do something interesting inside the function.

+5
source share

All Articles