slouchpie

slouchpie

Are module attributes "read" multiple times in same function?

Greetings comrades,

According to: https://elixir-lang.org/getting-started/module-attributes.html#as-constants

Every time an attribute is read inside a function, Elixir takes a snapshot of its current value.

My question is:

In this function:

@money %{amount: 0, currency: :DOGE}

def reset_global_debt do
  for human <- Earth.list_humans() do
    update_financial_record_for_human(human, %{
      "total_amount" => @money.amount,
      "base_currency" => @money.currency
    })
  end
end

do 2 copies of @money get created, since it is “read” twice?

I hope not…

Marked As Solved

eksperimental

eksperimental

Here’s the code.
you can clone it GitHub - eksperimental-help/slouchpie · GitHub

Unoptimized: slouchpie/lib/foo_big.ex at main · eksperimental-help/slouchpie · GitHub

Optimized: slouchpie/lib/foo_small.ex at main · eksperimental-help/slouchpie · GitHub

$ mix compile

$ touch lib/foo_big.ex && rm _build/dev/lib/slouchpie/ebin/Elixir.Foo.Big.beam 
$ time mix compile
Compiling 1 file (.ex)
Compiling lib/foo_big.ex (its taking more than 10s)

real  0m33.185s
user  0m28.222s
sys 0m4.836s

$ touch lib/foo_small.ex && rm _build/dev/lib/slouchpie/ebin/Elixir.Foo.Small.beam 
$ time mix compile
Compiling 1 file (.ex)

real  0m2.502s
user  0m2.306s
sys 0m0.331s

$ ls -alh _build/dev/lib/slouchpie/ebin/Elixir.Foo.*.beam
 23M Dec 15 14:14 _build/dev/lib/slouchpie/ebin/Elixir.Foo.Big.beam
2.1K Dec 15 14:15 _build/dev/lib/slouchpie/ebin/Elixir.Foo.Small.beam

$ elixir --version
Erlang/OTP 24 [erts-12.1.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.13.0 (compiled with Erlang/OTP 24)

UPDATE:

Here are the benchmarks
https://github.com/eksperimental-help/slouchpie/blob/main/benchmarks/foo.exs

Elixir 1.13.0
Erlang 24.1.2

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 5 s
parallel: 1
inputs: none specified
Estimated total run time: 24 s

Benchmarking Unoptimized: Foo.Big...
Benchmarking Optimized: Foo.Small...

Name                           ips        average  deviation         median         99th %
Optimized: Foo.Small      292.62 K        3.42 μs   ±436.00%        2.46 μs        6.48 μs
Unoptimized: Foo.Big       83.26 K       12.01 μs   ±123.18%        7.73 μs       30.17 μs

Comparison: 
Optimized: Foo.Small      292.62 K
Unoptimized: Foo.Big       83.26 K - 3.51x slower +8.59 μs

Memory usage statistics:

Name                    Memory usage
Optimized: Foo.Small         4.53 KB
Unoptimized: Foo.Big         9.09 KB - 2.01x memory usage +4.56 KB

**All measurements for memory usage were the same**

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It is as if you hand typed that map in two places. This can affect compile times, but it generally requires very large constants to be an issue.

lud

lud

If you are seeing this because of “decompiled” code, keep in mind that this code is built from the debug_info or abstract_code chunks of the beam files. I am not sure those contain all optimizations done by the compiler.

lud

lud

If you can read erlang you can use mix decompile to turn your compiled module into erlang source. And you will see that whenever an @attribute is used, the litteral value is present in the source.

Now if you cannot read erlang I guess you will recocgnize your own data enough to verify too.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
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
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

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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

We're in Beta

About us Mission Statement