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
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.
Popular in Questions
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
I would like to know what is the best IDE for elixir development?
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
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
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
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
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
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







