lud

lud

Does adding a dummy guard `when true` affect performance

In order to only have one branch of code in a macro that defines functions with support for guards, I would like to always have a guard.

So I want to define the generated functions as def myfun(arg1,arg2) when true do when no guard is given.

I suspect that the compiler will get rid of it and that it will not impact performance, but I am not sure how to verify that.

Thank you.

Marked As Solved

al2o3cr

al2o3cr

You can use the :S option to @compile to cause Elixir to dump a text representation of the generated bytecode.

For this module (in foo.ex):

defmodule Foo do
  @compile :S

  def without_guard(x) do
    x + 1
  end

  def with_guard(x) when true do
    x + 1
  end
end

Compiling it with elixir foo.ex produces a CompileError - that’s expected, because the @compile :S has caused the compiler to stop midway. The relevant bit of the result in foo.ex.S:

{function, with_guard, 1, 11}.
  {label,10}.
    {line,[{location,"foo.ex",8}]}.
    {func_info,{atom,'Elixir.Foo'},{atom,with_guard},1}.
  {label,11}.
    {line,[{location,"foo.ex",9}]}.
    {gc_bif,'+',{f,0},1,[{x,0},{integer,1}],{x,0}}.
    return.


{function, without_guard, 1, 13}.
  {label,12}.
    {line,[{location,"foo.ex",4}]}.
    {func_info,{atom,'Elixir.Foo'},{atom,without_guard},1}.
  {label,13}.
    {line,[{location,"foo.ex",5}]}.
    {gc_bif,'+',{f,0},1,[{x,0},{integer,1}],{x,0}}.
    return.

Other than labels and line numbers, with_guard and without_guard produce identical bytecode.

19
Post #2

Also Liked

harrisi

harrisi

I’ll also add that, in my experience, adding guards tends to increase performance, not decrease. In some cases it can be a significant speed increase.

Always crucial to profile actual code, though!

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics 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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement