<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Aug 29, 2017 at 8:41 PM, Caragea Silviu <span dir="ltr"><<a href="mailto:silviu.cpp@gmail.com" target="_blank">silviu.cpp@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hmm, <br>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.. <br></div></div></div></div></blockquote><div><br></div><div>Well, Erlang is a functional language and as such, you can't update any data structure in place. Updates create a new data structure and others may still have references to the original. Always. For lists, you can reuse the original list as the tail. For tuples, you have to copy the top level. For maps, it depends: the implementation is a HAMT structure, which means that it depends on the contents how much will be rewritten when updating; I don't know what the worst case is.</div><div><br></div><div>Whatever the case, there will be a new map data structure created. </div><div><br></div><div>regards,<br></div><div>Vlad</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div></div>Any way that we con confirm or not this theory ?<span class="HOEnZb"><font color="#888888"><br><br></font></span></div><span class="HOEnZb"><font color="#888888">Silviu<br></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 29, 2017 at 9:34 PM, Dmytro Lytovchenko <span dir="ltr"><<a href="mailto:dmytro.lytovchenko@gmail.com" target="_blank">dmytro.lytovchenko@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2017-08-29 20:19 GMT+02:00 Dmitry Kolesnikov <span dir="ltr"><<a href="mailto:dmkolesnikov@gmail.com" target="_blank">dmkolesnikov@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
Premature optimisation is an evil ;-)<br>
<br>
I would use the following syntax:<br>
```<br>
append(Key, Value, Map) -><br>
   List = case Map of<br>
      #{Key := Tail} -><br>
         [Value | Tail];<br>
      _ -><br>
         [Value]<br>
   end,<br>
   Map#{Key => List}.<br>
```<br>
<br>
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.<br></blockquote><div><br></div><div>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).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
- Dmitry<br>
<div><div class="m_123520750746700355m_2656748817632362242h5"><br>
<br>
> On 29 Aug 2017, at 20.34, Caragea Silviu <<a href="mailto:silviu.cpp@gmail.com" target="_blank">silviu.cpp@gmail.com</a>> wrote:<br>
><br>
> Hello,<br>
><br>
> Having a map where the value of each element it's a list :<br>
><br>
> #{ 1 => [], 2 => [], ... n => []}<br>
><br>
> and you need to append elements in the list for a specific key, what's the most optimal way to do this without copying the lists and the map inside the VM lot of times ?<br>
><br>
> Anything better than the following solution:<br>
><br>
> append_element(Key, Value, Map) -><br>
>     case maps:find(Key, Map) of<br>
>         {ok, V} -><br>
>             maps:put(Key, [Value | V], Map);<br>
>         _ -><br>
>             maps:put(Key, [Value], Map)<br>
>     end.<br>
><br>
> Kind regards,<br>
> Silviu<br>
</div></div>> ______________________________<wbr>_________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
</blockquote></div><br></div></div>
</blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>