Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Erlang really fast given it incur many memory copy under the hood?

I am a newbie for Erlang, I understand the language adopts a design of actor model and create the concept of light-weight process, which is key point for high concurrent programming. But, it also adopts the functional programming paradigm, which impose reference transparency. That means a variable can't be changed after assignment. So, I see lots of similar function like:

gb_trees:delete(Key, Tree1) -> Tree2

When we delete a key from a tree, we indeed create a whole new tree. Does this mean, we clone all remain nodes of Tree1 here under the hood ?

If so, is this language really suitable for high performance server development ?

Thanks !

like image 502
Yang Bo Avatar asked Mar 26 '13 06:03

Yang Bo


1 Answers

In the case of the tree, you only need to copy the nodes that actually change. Lets say you have a tree:

       A
      / \
     /   \
    B     C
         / \
        D   E

If you call your delete_tree method with B as an argument, the only node that needs to be copied is A, since the subtree CDE is still the same as before the operation.

Also, if you don't use Tree1 after the operation and only use the resulting tree, the compiler can change the operation to modify the tree directly, which may be faster.

These operations are not very expensive, and for most data structures the redundant copying overhead is very small. For some things (ie. large images loaded as byte arrays) you might need creative solutions.

Erlang is suitable for server systems not for it's speed, but for it's reliability. It's not a big deal for large system to add another dozen or hundred servers, but it is a huge deal if you have a 1-second downtime for phone billing for example. In the US, that might be hundreds of thousands of phone calls not billed - which obviously is a larger cost than buying more servers.

like image 72
Hampus Nilsson Avatar answered Sep 30 '22 12:09

Hampus Nilsson