5

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 !

Yang Bo
  • 679
  • 2
  • 8
  • 20
  • 5
    From a single-threaded point of view I've found Erlang to be relatively slow compared to (e.g.) Java, which I've guessed is largely because of its interpreted nature etc. However it seems to scale across multiple CPUs much better (in my *limited* experience). So it might be fair to say copying makes single-threaded apps slightly slower but enables average programmers to write multi-threaded apps with significantly higher throughput and fewer bottlenecks. – Paul Cager Mar 26 '13 at 11:59

1 Answers1

10

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.

Community
  • 1
  • 1
Hampus Nilsson
  • 6,692
  • 1
  • 25
  • 29