<div dir="auto">Not to start a holy war, but Unicode is not complex, it just has a lot of tables. And pretty much any modern language has immensely better Unicode support built in than Erlang.<div dir="auto">Thankfully there's ux.</div><div dir="auto"><br></div><div dir="auto">About processes and mailboxes. Every process has its share of scheduler's memory, which is used for mailbox, stack and heap. Send operation simply writes to the end of mailbox. Receive matches all the inbox (there is a temporary area too, the algorithm is clever to not block incoming messages) and selects one according to the pattern. Receiving a message which contains a freshly-created reference is optimized, otherwise it potentially has to search the whole mailbox.</div><div dir="auto"><br></div><div dir="auto">VM (BEAM) is actually several OS processes to allow for multi-core execution. They share almost nothing, and each one is executing one Erlang process at one time. When process switching happens, it's almost a matter of one pointer to an internal structure changing; of course, as always with virtual machines, the price is more indirection and slower code.</div><div dir="auto"><br></div><div dir="auto">Strings are a mess. Generally there should only be lists of characters, and binaries containing UTF-8, but some things work with lists of bytes and other such bullshit. IO lists, for one, are generally deep lists of bytes and binaries, but not always. In general, you have to be either very careful with Unicode wrangling, or just give up like many other did before you.</div></div><div class="gmail_extra"><br><div class="gmail_quote">11 янв. 2017 г. 3:12 пользователь "Richard A. O'Keefe" <<a href="mailto:ok@cs.otago.ac.nz">ok@cs.otago.ac.nz</a>> написал:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
On 11/01/17 7:58 AM, Bhag Chandra wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
1) "Strings in Erlang are internally treated as a list of integers of<br>
each character's ASCII values, this representation of string makes<br>
operations faster. For example, string concatenation is constant time<br>
operation in Erlang." Can someone explain why?<br>
</blockquote>
<br>
Pretty much all of this is somewhere between misleading and confusing.<br>
Mind you, pretty much everything about strings in general is<br>
somewhere between confusing and misguided, whatever the programming<br>
language, especially since the introduction of Unicode.<br>
<br>
The closest thing to the truth is to say that Erlang has no native<br>
string datatype whatsoever. Instead, there are several general purpose<br>
data types that are pressed into service to fake it.<br>
<br>
(1) An atom is a unique string. There's more to say about them,<br>
but the general rule is DON'T use them for strings.<br>
<br>
(2) A list of character codes is stringy. Once upon a time character<br>
codes were 8-bit quantities, so taking two computer words per character<br>
was sneered at. Unicode has a 21-bit character set these days. (Not<br>
only that, it's now racist and sexist, so that you can distinguish<br>
between pink/yellow male/female astronaut, I kid you not. Why do we<br>
need a character "man in business suit levitating"?) The astral planes<br>
are increasingly being populated, "Version 9.0 adds exactly 7,500 characters, for a total of 128,172 characters."<br>
<br>
Let X = "1....m" and Y = "1...n".<br>
Then X ++ Y concatenates them in O(m) time.<br>
NOT O(1).<br>
<br>
(3) For many purposes, a *tree* of numbers and pairs can be used to<br>
hold text. It is *this* that allows O(1) concatenation:<br>
[X|Y] *isn't* the string "1...m1...n" but can in certain contexts<br>
*represent* it. You can, for example, build up a text of total<br>
size S in O(S) time and then flatten it (or write it or transmit it)<br>
in O(S) time.<br>
<br>
Look up the 'iolist()' type in Erlang documentation.<br>
<br>
(4) The 'binary' type was originally a byte string and is now a<br>
bit string. A byte sequence can of course represent a string,<br>
with the encoding of the text being provided by context. It<br>
used to be common to use Latin 1, it's now common to use UTF8.<br>
There is library code to help with this, but frankly, Unicode<br>
is so appallingly complex that there will probably never be<br>
enough library support (in any language).<br>
<br>
Concatenating byte strings of length m and n costs O(m+n)><br>
<br>
(5) A tree of numbers and pairs could also contain binaries.<br>
<br>
The important thing about strings is that they are good at<br>
accepting data you don't care to inspect further, storing it,<br>
and giving it back later, and LOUSY at almost anything else,<br>
in EVERY programming language.<br>
<br>
One of the ways that they are lousy is that thanks to Unicode's<br>
rules, there is *structure* in strings (which were historically<br>
unstructured), so that you can have a well formed string of<br>
2 characters, split it into two 1-character strings, and find<br>
that at least one of those strings is no longer legal.<br>
(Emoji flags, for example.)<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
2) "It makes sense to use Erlang only where system's availability is<br>
very high". Is it not a very general requirement of most of the<br>
systems? Whatsapp to Google to FB to Amazon to Paypal to Barclays etc<br>
they all are high availability systems, so we can use Erlang in all of them?<br>
</blockquote>
<br>
I do not think many people in this mailing list would say<br>
"it makes sense to use Erlang ONLY where availability [must be] high."<br>
<br>
People use Erlang (and other languages running on the Erlang VM,<br>
such as LFE and Elixir) for all sorts of things. Yes, Erlang is<br>
not as fast as other languages.<br>
<br>
Let's have some perspective on that, though.<br>
Just today I measured four programs to solve the same problem,<br>
using the same structure, written in four languages.<br>
C 0.04 sec -- has types<br>
Smalltalk 0.25 sec -- lacks them<br>
Java 0.65 sec -- has types<br>
Python 2.98 sec -- lacks them.<br>
Have figures like that ever stopped anyone using Python?<br>
Have they even encouraged people to switch to Smalltalk?<br>
<br>
People use a programming language when it lets them get<br>
the job at hand done to an adequate standard.<br>
These days this is as much about infrastructure such as<br>
profiling, test coverage analysis, test frameworks,<br>
documentation tools, libraries, interface support (JSON,<br>
XML, ASN.1, Protobufs, whatever) as it is about the<br>
language as such.<br>
<br>
I find that it makes sense to use Erlang whenever concurrency<br>
is important/useful, because it's so much easier to get<br>
concurrent programs right than in Java or C11 or pretty much<br>
anything else (although Ada comes close). There are of course<br>
plenty of libraries for other languages that claim to add<br>
Erlang-style abilities to those languages; what they don't<br>
mention is that you cannot remove dangers by adding libraries.<br>
For example, gets() has been deprecated in C; the OpenBSD<br>
linker tells you off if you use it. There are safer alternatives.<br>
But nothing can stop you writing your own version of gets(),<br>
and the OpenBSD linker has no idea that mygets() is just as<br>
dangerous as gets() ever was.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
3) "Every message which is sent to a process, goes to the mailbox of<br>
that process. When process is free, it consumes that message from<br>
mailbox". So how exactly does process ask from the mailbox for that<br>
message? Is there a mechanism in a process' memory which keeps polling<br>
its mailbox. I basically want to understand how message is sent from<br>
mailbox to my code in process.<br>
</blockquote>
<br>
"When process is free" should presumably be<br>
"When that process is RUNNING and FEELS LIKE checking its mailbox,<br>
that message MAY BE consumed if it is one the process WANTS to<br>
consume."<br>
<br>
A process reads from its mailbox using the 'receive' construction,<br>
described in for example Learn You Some Erlang For Great Good<br>
(<a href="http://learnyousomeerlang.com" rel="noreferrer" target="_blank">http://learnyousomeerlang.com</a><wbr>) which I recommend. This happens<br>
when the code decides it is time to try to receive a message.<br>
<br>
If you know about message passing in UNIX,<br>
Pid ! Msg is like msgsnd( Queue, Msg, Msg_Size, Flags)<br>
or mq_send( Queue, Msg, Msg_Size, Prio)<br>
receive ... end is like msgrcv( Queue, Msg, Msg_Size, Type, Flags)<br>
or mq_receive(Queue, Msg, Msg_Size, &Prio)<br>
<br>
That is, sending happens when there is something EXPLICIT in the<br>
code to make it happen, and receiving happens when there is<br>
something EXPLICIT in the code to make it happen, and there is<br>
buffering in between so things don't have to be simultaneous.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
4) We say that a message is passed from process A to process B by simply<br>
using a bang (!) character, but what happens behind the scenes to pass<br>
this message? Do both processes establish a tcp connection first and<br>
then pass message or what?<br>
</blockquote>
<br>
First of, we have Erlang "nodes" that are Unix or Windows "processes",<br>
and inside them we have Erlang "processes" that are like Unix or Windows<br>
"threads" except much cheaper; the word "fibres" is sometimes used to<br>
describe similar things in those environments.<br>
<br>
Erlang processes inside nodes on different machines may well use<br>
TCP connections automatically set up between the nodes. Or they<br>
could use something else. They could in principle use Infiniband<br>
or carrier pigeons.<br>
<br>
Erlang processes inside different nodes on the same machine<br>
could use any IPC facility provided by the host OS, such as<br>
System V message queues, POSIX message queues, pipes, UNIX sockets,<br>
...<br>
<br>
Erlang processes inside the same node will use shared memory,<br>
which might or might not involve copying, depending on the Erlang<br>
version. Whatever happens, it's likely to be cheaper than TCP.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
5) At 30:25 in this video ( <a href="https://youtu.be/YaUPdgtUYko?t=1825" rel="noreferrer" target="_blank">https://youtu.be/YaUPdgtUYko?t<wbr>=1825</a> ) Mr.<br>
Armstrong is talking about the difference between the context switching<br>
overhead between OS threads and Erlang processes. He says, thread<br>
context switching is of order 700 words but Erlang process context<br>
switching is ... ?<br>
</blockquote>
<br>
He said that OS thread switching MOVES about 700 words,<br>
while Erlang process switching involves THREE REGISTERS.<br>
______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
</blockquote></div></div>