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 ?
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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_vardo you mean a variable of_ = some_function_call(x)? In the case of_ = some_varor evenx = some_varno 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 dox = some_varit just has one thing.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
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 signatureMyApp.Module.SubModule.template_name(assigns)out of template filesmodule/sub_module/template_name.eex. When the template doesn’t use theassignsbindings I got a warning saying thatassignsis not used. I spent hours trying different things, playing with the@compiledirective but the only way I found to get rid of the warning was to actually useassignsby adding at the end of the template<% _ = assigns %>. I was wondering if it generated any kind of overhead, but obviously, it doesn’tAsd
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
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
constin 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
If
my_funcis defined in the same module andinlineis enabled, it may get compiled to something likeYou can see that operation of constructing the tuple is removed and
my_funcwas inlined.But if
my_funcwas defined in some other module, it won’t be inlined and code would be compiled without any optimizations.Schultzer
I wonder if there are situations where it can’t remove the tuple.
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 analysisSchultzer
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.