How come Elixir (and BEAM for that matter) is fast ??

110 views
Skip to first unread message

Bulent Erdemir

unread,
Aug 24, 2020, 8:28:44 AM8/24/20
to elixir-l...@googlegroups.com
Hi, 

Elixir applications, due to immutability principle, create a copy of a data structure once that structure is modified (transformed) and if the structure's new value is to be kept via assignment to a variable. Hence, for example, when a Map is visited and some its members are modified , for each modification a copy of the whole Map is created. I understand that creating a new variable is as simple as bumping up a pointer in memory, however, the whole Map is copied over and over again while it's being modified in the application. In other languages without the immutability principle, only the changed portion of the Map would be allocated in memory during a similar operation.

In such a situation where there's this copying overhead in almost all data operations in an Elixir program, I would expect the system would run much slower compared to other languages. Yet, I've not seen mention of this problem anywhere on the web. So, probably, I'm missing something. Anyone please would care to comment on this ?  

Regards,
Bülent Erdemir 

Derek Kraan

unread,
Aug 24, 2020, 8:32:38 AM8/24/20
to 'boris kotov' via elixir-lang-core
The short answer is that the BEAM does not do a full copy of a map for every update.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Bulent Erdemir

unread,
Aug 24, 2020, 9:08:39 AM8/24/20
to elixir-l...@googlegroups.com
Oh, that's it, then. :)

Thank you for your quick reply. I'll follow up with the book and more research.

José Valim

unread,
Aug 24, 2020, 9:10:34 AM8/24/20
to elixir-l...@googlegroups.com
For more research, the term you want to look for is "Persistent data structures": https://en.wikipedia.org/wiki/Persistent_data_structure

There are also good talks on the topic, here is one from Rich Hickey: https://www.youtube.com/watch?v=toD45DtVCFM

Adam Lancaster

unread,
Aug 24, 2020, 9:21:30 AM8/24/20
to elixir-l...@googlegroups.com
That book is great and explains some nice functional data structures. In short the key to it is to use persistent data structures:


The crux of the idea is to only copy the parts that change, sort of like a diff.

You can read about clojure’s persistent vectors here too.


And see this stack overflow post from Jose:


Best

Adam



Bulent Erdemir

unread,
Aug 24, 2020, 9:43:55 AM8/24/20
to elixir-l...@googlegroups.com
That's super fast. Great. Thank you for all the guidance... :)

Adam Lancaster

unread,
Aug 24, 2020, 9:45:37 AM8/24/20
to elixir-l...@googlegroups.com
Whoops sorry totally missed José’s email, I didn’t mean to repeat what he said!

Reply all
Reply to author
Forward
0 new messages