mszmurlo

mszmurlo

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 ?

Showing Posts 1 to 7

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.

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 OP

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

Asd

Asd

This introduces no overhead in runtime in practice. Erlang compiler use SSA graph intermediate code representation form where removing such line becomes trivial.

Even if you do

y = x
f(y)

It will be compiled to exactly the same byte and native code as f(x)


It is a great question.

First of all, structures in Elixir are immutable, but not in a sense of immutability like in Rust or const in C. They are never copied on passing as an argument, but they may be copied (or partially copied) on mutation. So no, it won’t be copied.

Second, is that there are some other optimizations which might happen. Consider this code

def example do
  x = {f(), g(), h()}
  my_func(:error, x)
end

If my_func is defined in the same module and inline is enabled, it may get compiled to something like

def example do
  f()
  g()
  h()
  some_thing_else()
end

You can see that operation of constructing the tuple is removed and my_func was inlined.
But if my_func was defined in some other module, it won’t be inlined and code would be compiled without any optimizations.

Schultzer

Schultzer

I wonder if there are situations where it can’t remove the tuple.

Asd

Asd

There are and in most cases it won’t be removed. Erlang compiler can’t rely on information about functions from other modules (otherwise it would introduce dependency, and current hot module reloading implementation does not support it). Plus, inlining must be explicitly specified with @compile :inline. And there are also limits to compile-time analysis

Schultzer

Schultzer

I’ll just continue to write code in a performant way, tend to be more readable to in Erlang/Elixir if you reduce allocation and lean into recursive functions anyways.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews