supersimple

supersimple

Hex Core Team

Are there performance differences or style preferences in case vs function heads?

@adkron brought up an interesting point today; Case and cond statements could be better represented as multiple function heads.
for example:

  case :foo do
     {:ok, :bar} -> :baz
     {:error, _} -> :bing 
   end

versus

    private_fun(:foo)
    ....
    defp private_fun({:ok, :bar}), do: :baz
    defp private_fun({:error, _}), do: :bing 

I am just wondering if one is more performant than the other. And also, which style is preferred.

Most Liked

NobbZ

NobbZ

  1. replacing a case which spans the whole function, wouldn’t make any difference, its compiled to the same bytecode anyway.
def f({:ok, :bar}), do: :baz
def f({:error, _}), do: :bing 
defp f(x) do
  case x do
    {:ok, :bar} -> :baz
    {:error, _} -> :bing 
  end
end

Are therefore equivalent.

  1. If you replace a case in a function by a call to a function in the same module, there might be the slight overhead of a local function call, but depending on the optimisation settings, it might get inlined. In performance critical parts I wouldn’t rely on the inline, but in general I do consider foo = find_the_answer(data) much more readable than foo = case data do … end, so I’d prefer extracting into a function most of the time.

  2. If you have a case in a function as before and replace it with a function in a different module, you’ll pay the “cost” of a remote call. It will never get inlined. But even here I’d prefer the function call, if it makes sense in the context of the application design.

Conclusion:
Only ever consider the “faster” way when you know its actually a problem. You know what Dijkstra said about premature optimisation?

stefanchrobot

stefanchrobot

I personally have a strong preference for case, because I don’t like the repetition of function name.

tty

tty

In general I would favor function heads because of the way Erlang VM treats functions. It’s not about speed. The VM uses function executions, aka reductions, as a means of determining when to allow a process to run. In addition SASL works on functions, having the built-in error reporting work for you is always a Good Thing. Lastly, you can turn on function tracing at runtime, something unavailable with expressions.

Coding wise, I do find it easier on the eyes with another function head then another case block. This also avoids the case lead Pyramid of Doom.

I’m not certain if Elixir case, if, cond are converted internally into function heads or dealt with in a different manner.

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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