[erlang-questions] optimal way to append an element in a list inside a map

Vlad Dumitrescu vladdu55@REDACTED
Tue Aug 29 20:56:28 CEST 2017


On Tue, Aug 29, 2017 at 8:41 PM, Caragea Silviu <silviu.cpp@REDACTED>
wrote:

> 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..
>

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.

Whatever the case, there will be a new map data structure created.

regards,
Vlad



> Any way that we con confirm or not this theory ?
>
> Silviu
>
> On Tue, Aug 29, 2017 at 9:34 PM, Dmytro Lytovchenko <
> dmytro.lytovchenko@REDACTED> wrote:
>
>>
>>
>> 2017-08-29 20:19 GMT+02:00 Dmitry Kolesnikov <dmkolesnikov@REDACTED>:
>>
>>> 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.
>>>
>>
>> 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).
>>
>>
>>>
>>> - Dmitry
>>>
>>>
>>> > On 29 Aug 2017, at 20.34, Caragea Silviu <silviu.cpp@REDACTED> 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
>>> the most optimal way to do this without copying the lists and the map
>>> inside the VM lot of times ?
>>> >
>>> > 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
>>> > erlang-questions@REDACTED
>>> > http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>
>>
>>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170829/f2149653/attachment.htm>


More information about the erlang-questions mailing list