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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

We're in Beta

About us Mission Statement