digraph edges and improper lists

Dmytro Lytovchenko dmytro.lytovchenko@REDACTED
Mon Dec 20 17:28:45 CET 2021


A list of 2 elements will consume:
1 list cell (2 words of memory) to store $n and pointer to second cell
1 list cell (2 more words) to store N and [] (that's nil, end of proper
list)
total: 4 words of memory, on a 64 bit machine that's 32 bytes

An improper list of 2 elements will consume:
1 list cell (2 words of memory) to store $n and tail is stored as N
total 16 bytes of memory on 64 bit machine

http://beam-wisdoms.clau.se/en/latest/indepth-memory-layout.html#lists-cons
http://beam-wisdoms.clau.se/en/latest/indepth-data-sizes.html#list

On Mon, 20 Dec 2021 at 17:23, Fred Youhanaie <fly@REDACTED> wrote:

> Hi
>
> The digraph module uses the term ['$e'|N] to represent an edge.
>
> Is there an advantage in using an improper list, as opposed to the more
> intuitive tuple format {'$e', N}?
>
> I recently came across a situation where the following crashed because
> logger tried to flatten the list of improper lists!
>
> logger:notice(#{ edges => digraph:edges(G) }).
>
> However, these two were OK:
>
> logger:notice(#{ edges => { digraph:edges(G) } }). %% Note the extra braces
> logger:notice("edges = ~w", [digraph:edges(G)] ).
>
> For me this was a temporary annoyance, but I'm just curious about the
> choice of list structure. Is this explained anywhere, such as docs or a
> blog?
>
> Cheers,
> Fred
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20211220/0aee2216/attachment.htm>


More information about the erlang-questions mailing list