japplegame

japplegame

Creating structures: new vs modify

Erlang maps can share a tuple of keys to reduce memory usage.
I found that when creating identical structures using the %Struct{...} syntax, the created structures do not share keys tuple.
But if you modify an already created default structure instead of creating a new one, the keys will be shared.

defmodule Struct do
  defstruct [:a, :b, :c, :d]
end

defmodule Test do
  def run() do
    Enum.map(0..1000, &%Struct{a: &1, b: &1, c: &1, d: &1})
    |> :erts_debug.size()
    |> IO.puts()

    Enum.map(0..1000, &%{%Struct{} | a: &1, b: &1, c: &1, d: &1})
    |> :erts_debug.size()
    |> IO.puts()
  end
end

Test.run()

Output

16016
10016

Is that %Struct{...} behavior a flaw or is it intentional?

First Post!

LostKobrakai

LostKobrakai

When creating maps it’s probably cheaper to not look at other maps in memory to see if their keys align. For updates the beam however knows that the keys are the same.

The beam is completely unaware of the fact that structs in elixir enforce the same set of keys.

Where Next?

Popular in Questions Top

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 records...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement