<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="h5"><br>
<br>
> On 29 Aug 2017, at 20.34, Caragea Silviu <<a href="mailto:silviu.cpp@gmail.com">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">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>
______________________________<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>
</blockquote></div><br></div></div>