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?