eltoncampos1

eltoncampos1

What is the most performant way to create a map?

In a case you have to create a Map with 15 ~ 20 fields, who is more performatic? A hardcoded map

%{foo: 1, bar: 2, baz: 3 ...}

Or builda Map using Map.put

%{}
|> Map.put(:foo, 1)
|> Map.put(:bar, 2)
...

Marked As Solved

al2o3cr

al2o3cr

Depends on the exact details of the keys and values, but there’s less difference between the two than you’d think if things are compile-time constants.

Here’s a module to demonstrate that:

defmodule Bar do
  @compile :S

  def bar do
    %{}
    |> Map.put(:a, 1)
    |> Map.put(:b, 2)
  end

  def baz do
    %{a: 1, b: 2}
  end

  def huh(a, b) do
    %{a: a, b: b}
  end

  def wat(a, b) do
    %{}
    |> Map.put(:a, a)
    |> Map.put(:b, b)
  end
end

The @compile :S attribute causes the compiler to emit a BEAM assembly file with a .S extension and then crash. That file is a human-readable version of what the compiler converts to a .beam artifact. Check out the BEAM Book for details on exactly what’s happening, but you don’t need to be fluent in the VM bytecode to draw conclusions.

First observation: baz’s implementation is a single move from the literal pool. Since the Map is immutable, every call to baz can return a reference to the same structure.

Second observation: bar’s implementation is… ALSO a single move from the literal pool. The compiler has noticed that the expression in bar returns a constant value and pre-calculated it at compile-time. So the answer to your original specific question is “there’s absolutely no difference”, since the two forms compile to exactly the same assembly.

Third observation: the implementation in huh uses a single put_map_assoc instruction that combines the literal keys and the input arguments.

Final observation: the implementation in wat is optimized to match huh exactly! Again, the compiler has observed that the result can be pre-computed.

You can experiment with the limits of this optimization; for instance, adding a Map.put to wats chain with a non-constant key will force two put_map_assoc calls to be used.

17
Post #4

Also Liked

smathy

smathy

First one.

outlog

outlog

you could test using benchee | Hex - most likely hardcoded, for obvious reasons…

Where Next?

Popular in Questions Top

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement