<div dir="ltr">Hello, <br><br>Thanks for clarification. I thought that in background erlang can see when there is no other reference to the original map/list (for example in case of accumulator inside a recursive fun) and will append to the original object while updating the reference instead of making partial copy. <br><br>Silviu<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 29, 2017 at 10:01 PM, Sverker Eriksson <span dir="ltr"><<a href="mailto:sverker.eriksson@ericsson.com" target="_blank">sverker.eriksson@ericsson.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div 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="m_5697418266492138255moz-cite-prefix">On 08/29/2017 08:41 PM, Caragea Silviu
      wrote:<br>
    </div>
    <blockquote type="cite">
      <pre>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="m_5697418266492138255moz-txt-link-abbreviated" href="mailto:dmytro.lytovchenko@gmail.com" target="_blank">dmytro.lytovchenko@gmail.com</a>> wrote:

</pre>
      <blockquote type="cite">
        <pre>
2017-08-29 20:19 GMT+02:00 Dmitry Kolesnikov <a class="m_5697418266492138255moz-txt-link-rfc2396E" href="mailto:dmkolesnikov@gmail.com" target="_blank"><dmkolesnikov@gmail.com></a>:

</pre>
        <blockquote type="cite">
          <pre>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>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>- Dmitry


</pre>
          <blockquote type="cite">
            <pre>On 29 Aug 2017, at 20.34, Caragea Silviu <a class="m_5697418266492138255moz-txt-link-rfc2396E" href="mailto:silviu.cpp@gmail.com" target="_blank"><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>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>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
______________________________<wbr>_________________
erlang-questions mailing list
<a class="m_5697418266492138255moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>
<a class="m_5697418266492138255moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a>
</pre>
          </blockquote>
          <pre>______________________________<wbr>_________________
erlang-questions mailing list
<a class="m_5697418266492138255moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>
<a class="m_5697418266492138255moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a>

</pre>
        </blockquote>
        <pre>
</pre>
      </blockquote>
      <pre></pre>
      <br>
      <fieldset class="m_5697418266492138255mimeAttachmentHeader"></fieldset>
      <br>
      <pre>______________________________<wbr>_________________
erlang-questions mailing list
<a class="m_5697418266492138255moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>
<a class="m_5697418266492138255moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a>
</pre>
    </blockquote>
    <br>
  </div>

</blockquote></div><br></div>