eltoncampos1

eltoncampos1

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)
...

Showing Posts 1 to 3

smathy

smathy

First one.

outlog

outlog

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

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 #3
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews