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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement