[erlang-questions] Erlang basic doubts about String, message passing and context switching overhead

Karolis Petrauskas k.petrauskas@REDACTED
Tue Jan 10 21:53:35 CET 2017


Hi,

On Tue, Jan 10, 2017 at 10:03 PM, Hugo Mills <hugo@REDACTED> wrote:
> On Wed, Jan 11, 2017 at 12:28:56AM +0530, Bhag Chandra wrote:
>> I have been coding in Erlang for 2 years.  A wonderful language but not
>> very big community, so I cant discuss my questions with programmers around
>> me (Java, Python guys). I found out about this list today.
>>
>> I have some fundamental doubts about the Erlang. It would be great if
>> someone can help me clarify them.
>
>    I can answer some of them.
>
>> 1) "Strings in Erlang are internally treated as a list of integers of each
>> character's ASCII values, this representation of string makes operations
>> faster. For example, string concatenation is constant time operation in
>> Erlang."  Can someone explain why?
>
>    That's not right. String concatenation is O(n), not O(1). When
> constructing the concatenation A ++ B, the list A must be traversed in
> order to place each character at the head of the result list.

You can have constant time concatenation if you can consider your
strings as iolist() (which you can on most IO functions). Example:

A = "hello", B = "world", C = [A , " ", C].
io:format("~s", [C]). % This will print "hello world"

>
>> 2) "It makes sense to use Erlang only where system's availability is very
>> high".  Is it not a very general requirement of most of the systems?
>> Whatsapp to Google to FB to Amazon to Paypal to Barclays etc they all are
>> high availability systems, so we can use Erlang in all of them?

For me its usually much easier to develop a system in Erlang, that
other language, even if it has no high availability requirements.
Despite my 12 years experience in Java.

>> 3) "Every message which is sent to a process, goes to the mailbox of that
>> process. When process is free, it consumes that message from mailbox". So
>> how exactly does process ask from the mailbox for that message? Is there a
>> mechanism in a process' memory which keeps polling its mailbox. I basically
>> want to understand how message is sent from mailbox to my code in process.
>
>    This is the "receive" expression. It runs a set of matches against
> the mailbox and returns the first matching message.
>
>> 4) We say that a message is passed from process A to process B by simply
>> using a bang (!) character, but what happens behind the scenes to pass this
>> message? Do both processes establish a tcp connection first and then pass
>> message or what?
>
>    Erlang processes aren't the same thing as OS processes. Typically,
> you'll have one VM (a node), which will run anywhere between dozens
> and millions of erlang processes. This all takes place under one OS
> process -- the VM. Communications between erlang processes are handled
> internally to the VM.
>
>    If you're doing distributed erlang, you'll have several nodes
> (possibly, but not always, on the different machines). These nodes
> connect to each other with permanent TCP connections, over which they
> talk the erlang distribution protocol.
>
>    Hugo.
>
> --
> Hugo Mills             | Great films about cricket: Batsman Begins
> hugo@REDACTED carfax.org.uk | starring Christian Bail
> http://carfax.org.uk/  |
> PGP: E2AB1DE4          |
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>

Karolis



More information about the erlang-questions mailing list