mszmurlo

mszmurlo

Does the compiler optimize contructs with `_` or with `_variable`

Hi.

I was wondering if the compiler makes any optimization for constructs like below.

# Let’s define `my_func`
def my_func(:ok, param) do some_thing_with(param) end
def my_func(:error, _param) do some_thing_else() end

# and then call my_func
my_func(:error, some_var)

Does the compiler copy the value of some_var but then simply doesn’t use it or does it not even bother to copy it ?

Same question if I write _ = some_var. Does the compiler simply discard the line as it does nothing or does it do something that uses CPU ?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @mszmurlo values passed to functions are not copied. Values in Elixir are immutable, so no copying needs to occur when a value is passed to a function.

When you say _ = some_var do you mean a variable of _ = some_function_call(x) ? In the case of _ = some_var or even x = some_var no work happens at all and I believe the whole thing is optimized out. Again values are immutable so the runtime doesn’t have two things to deal with when you do x = some_var it just has one thing.

Also Liked

garrison

garrison

Not your exact question but the binary efficiency guide has a section which mentions that unused variables in binary matches are optimized out regardless of whether they start with an underscore. I’d imagine this is true in general (that the underscore doesn’t matter), though in practice of course the Elixir compiler will complain either way.

mszmurlo

mszmurlo

Yes, this sounds very reasonable! Thanks for your enlightenment.

No, I really meant _ = some_var. To give the context of my question, I was building a template compiler based on EEX. I’m creating dynamically functions with the following signature MyApp.Module.SubModule.template_name(assigns) out of template files module/sub_module/template_name.eex. When the template doesn’t use the assigns bindings I got a warning saying that assigns is not used. I spent hours trying different things, playing with the @compile directive but the only way I found to get rid of the warning was to actually use assigns by adding at the end of the template <% _ = assigns %>. I was wondering if it generated any kind of overhead, but obviously, it doesn’t

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement