lud

lud

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.

First 4 of 4 Posts Switch mode

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.

20
Post #1
christhekeele

christhekeele

That’s a neat trick, thanks for sharing!

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!

lud

lud OP

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 :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement