<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Maps have two implementations depending on number of keys.<br>
    <br>
    Small maps (<= 32 keys) are basically a key-tuple and a value
    tuple.<br>
    Updating the value of an existing key will copy the value-tuple<br>
    while reusing the key-tuple.<br>
    <br>
    Large maps (> 32 keys) are basically a tree (HAMT).<br>
    All nodes from the root to the one containing the key must be copied<br>
    while all other nodes can be reused.<br>
    <br>
    /Sverker<br>
    <br>
    <div class="moz-cite-prefix">On 08/29/2017 08:41 PM, Caragea Silviu
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAJsc=ovrHD6hyU0bSfnoOb23b6F83D192d-8NTR1XvnRqqdnfA@mail.gmail.com">
      <pre wrap="">Hmm,

Basically what you are saying is that any update over a map requires
rebuilding of the entire map ? I doubt as time implementation seems so
lame..

Any way that we con confirm or not this theory ?

Silviu

On Tue, Aug 29, 2017 at 9:34 PM, Dmytro Lytovchenko <
<a class="moz-txt-link-abbreviated" href="mailto:dmytro.lytovchenko@gmail.com">dmytro.lytovchenko@gmail.com</a>> wrote:

</pre>
      <blockquote type="cite">
        <pre wrap="">

2017-08-29 20:19 GMT+02:00 Dmitry Kolesnikov <a class="moz-txt-link-rfc2396E" href="mailto:dmkolesnikov@gmail.com"><dmkolesnikov@gmail.com></a>:

</pre>
        <blockquote type="cite">
          <pre wrap="">Hello,

Premature optimisation is an evil ;-)

I would use the following syntax:
```
append(Key, Value, Map) ->
   List = case Map of
      #{Key := Tail} ->
         [Value | Tail];
      _ ->
         [Value]
   end,
   Map#{Key => List}.
```

Lists are not copied they are referenced. Maps… Hmm, I am not sure. I
hope the implementation is smart enough to keep reference as well.

</pre>
        </blockquote>
        <pre wrap="">
In BEAM any object which does not fit into a machine register (such as a
list or a map) will be placed on heap and a pointer will be used instead.
But prepending to a list will change the list pointer value (the pointer to
list head will change, the remaining tail elements will not move and will
not change). This requires writing the new head into the map value. And
this will incur a map update, which most likely will rebuild the map. I'm
almost sure that the optimization possibilities for this are very limited
and are similar to tuple optimizations (i.e. works only at creation time).


</pre>
        <blockquote type="cite">
          <pre wrap="">
- Dmitry


</pre>
          <blockquote type="cite">
            <pre wrap="">On 29 Aug 2017, at 20.34, Caragea Silviu <a class="moz-txt-link-rfc2396E" href="mailto:silviu.cpp@gmail.com"><silviu.cpp@gmail.com></a> wrote:

Hello,

Having a map where the value of each element it's a list :

#{ 1 => [], 2 => [], ... n => []}

and you need to append elements in the list for a specific key, what's
</pre>
          </blockquote>
          <pre wrap="">the most optimal way to do this without copying the lists and the map
inside the VM lot of times ?
</pre>
          <blockquote type="cite">
            <pre wrap="">
Anything better than the following solution:

append_element(Key, Value, Map) ->
    case maps:find(Key, Map) of
        {ok, V} ->
            maps:put(Key, [Value | V], Map);
        _ ->
            maps:put(Key, [Value], Map)
    end.

Kind regards,
Silviu
_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
          </blockquote>
          <pre wrap="">
_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>

</pre>
        </blockquote>
        <pre wrap="">

</pre>
      </blockquote>
      <pre wrap="">
</pre>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>