halostatue
Am I over-optimizing unchanged tuple results?
Some years ago, I switched my case statement handling to something like this:
case some_operation() do
{:ok, _} = ok -> ok
{:error, reason} -> # do something with reason
end
I did this because I recalled something about an unnecessary tuple allocation improvement that José made in Erlang itself. Is this over-optimization in modern Elixir 1.13+ / Erlang 24+? Does it still make sense to do that, or would writing the (potentially clearer) version be OK now?
case some_operation() do
{:ok, value} -> {:ok, value}
{:error, reason} -> # do something interesting with reason
end
Marked As Solved
benwilson512
I believe Core Erlang Optimizations - Erlang/OTP shows that it optimizes to zero in this case. It’ll just reuse the tuple whether you write it out that way or not.
Also Liked
dimitarvp
If I remember correctly both :ok idioms compile to the same bytecode and this has been true for a while now. Can’t remember since which OTP version though.
halostatue
It’s the tuple creation that I’m concerned about. Sure, it’s cheap, but it’s not zero. Similarly, when I only care about what the shape of the {:ok, value} result is, I do this:
case something() do
{:ok, value} -> # do something interesting with value
error -> error
end
If the code generation for {:ok, value} -> {:ok, value} now avoids the creation of the second tuple (because it’s recognized as a duplicate), then the optimization is unnecessary. If it doesn’t, then I’d rather stick with the existing pattern, because I do have such things in tight loops where tuple creation might not be ideal
The opposite case of the error -> error shape above isn’t always possible:
case something() do
{:ok, %{}} = ok -> ok # or {:ok, %{} = value} -> {:ok, value}
{:ok, _} -> {:error, "Invalid return"}
error -> error
end
BartOtten
Just to chime in: when this level of optimization is what you need, you might check out a different language or use something like rustler. After all: the mass result of this nano-optimization is thousand times gone as soon as you do something else slightly off.
Last Post!
halostatue
Don’t feel jealous. I was looking for a link to the reasons why. I think we adopted the practice when we were stuck on a version that couldn’t do the optimization in question, but now we’re on 1.13 (1.14 soonish).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









