[erlang-questions] Static Map ( Keys known at compile time )

Mikael Pettersson mikpelinux@REDACTED
Mon Apr 29 10:29:24 CEST 2019


On Mon, Apr 29, 2019 at 8:23 AM Prakash Parmar
<prakash.parmar@REDACTED> wrote:
>
> Hello All,
>
> Does Erlang supports Static Maps ? Something like, All possible Keys of Map has to be define at compile time and Can not be added/deleted dynamically.
>
> In a project we have a record with ~50 element and their fields will be accessed/Modified multiple times through out the Business Logic flow. Though we are not storing it in Mnesia. Does replacing Record with Map will be better option in terms of performance ?

That depends.  In principle a record is faster because the
field-to-index mapping is a compile-time constant, so all you're left
with at runtime are element and setelement calls, which are O(1).  A
map OTOH has a dynamic key set so must indirect through a hash to
search tree.  For smaller mappings a record should always win.

However, your records are quite large, and you state that they're
updated multiple times.  While lookups (element calls) are as fast as
can be, updates are O(size(Tuple)).  Past some point, it becomes more
efficient to split the tuple into chunks to reduce update costs, at
the expense of slightly higher lookup costs.  What's optimal depends
very much on the job mix and the HW it's running on.

So for these large mappings, maps may very well be faster.  You'll
need to measure on your own job mix.



More information about the erlang-questions mailing list