# Does adding a dummy guard \`when true\` affect performance

**URL:** <https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151>\
**Category:** Questions\
**Tags:** macro, defmacro\
**Created:** [December 16, 2024, 10:23pm UTC](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151 "2024-12-16T22:23:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [December 16, 2024, 10:23pm UTC](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151/1 "2024-12-16T22:23:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![al2o3cr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/32/3457_2.png) [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Post date:** [December 17, 2024, 3:54am UTC](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151/2 "2024-12-17T03:54:21Z")

</div>

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`):

```elixir
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`:

```elixir
{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.

---

<div class="post-metadata">

**Author:** ![christhekeele](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/christhekeele/32/1039_2.png) [@christhekeele](https://forum.elixirforum.com/u/christhekeele)\
**Post date:** [December 18, 2024, 2:23am UTC](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151/3 "2024-12-18T02:23:44Z")

</div>

> [@al2o3cr](#):
>
> You can use the `:S` option to `@compile` to cause Elixir to dump a text representation of the generated bytecode.

That’s a neat trick, thanks for sharing!

---

<div class="post-metadata">

**Author:** ![harrisi](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/harrisi/32/33003_2.png) [@harrisi](https://forum.elixirforum.com/u/harrisi)\
**Post date:** [December 18, 2024, 3:15am UTC](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151/4 "2024-12-18T03:15:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [December 18, 2024, 9:50am UTC](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151/5 "2024-12-18T09:50:51Z")

</div>

Ah yes I forgot about the `S` thing. I remember there was also an `E` format. I should take some time to learn about those.

Thank you 🙂
