halostatue

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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

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

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

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).

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

We're in Beta

About us Mission Statement