Erlang Mailing Lists

Author Message

<  Erlang  ~  call by value/ref

danielywoo
Posted: Mon Jan 03, 2011 3:56 am Reply with quote
Joined: 31 Dec 2010 Posts: 8
I read a book about Erlang recently and it says:

Quote:
All calls with variables in Erlang are call by value


I am wondering, since everything in Erlang is immutable, there should be no difference to call by value or referrence, right?

-Daniel
View user's profile Send private message
rayor
Posted: Tue Jan 04, 2011 9:56 am Reply with quote
Joined: 04 Jan 2011 Posts: 1 Location: USA
We should only call by value!! As I understood and read the concept of call by reference does not exist, removing one way in which side effect can be caused. All variables are considered local to the function in which they are bound.
View user's profile Send private message Visit poster's website
danielywoo
Posted: Tue Jan 04, 2011 12:59 pm Reply with quote
Joined: 31 Dec 2010 Posts: 8
Not "should", it's "must" since erlang only supports "call by value".
Actually in other languages like Java, everything is passed by reference for performance reasons, if you are passing a huge tuple or list no doubt you will encounter performance issues. My question is actually how the optimization is done in the Erlang VM, since everything is immutable, the VM should be able to optimize the "call by value" to "call by ref". E.g. Some of the app servers like WebLogic has such optimization, if you call remote interface of an EJB within a single VM and the variables are immutable, the default "call by val" will be optimized to "call by ref". This also leave less work for the gabage collector. Does anybody know about the possible VM optimization of this?
View user's profile Send private message
zajda
Posted: Tue Jan 04, 2011 10:38 pm Reply with quote
User Joined: 22 Aug 2009 Posts: 83
In general everything in Erlang is copied. The only case where references are used are operations on binaries (bigger than 64 bytes). Each kind of content (short lived, medium, long) has its own memory area and it lets to use GC in efficient way. All of it is done transparently by the VM.

Obviously coping stuff around can reduce performance, however it increases scalability by removing areas to lock or synchronize. I think this concept much better utilizes multicore CPUs. All in all thats the assumption of share-nothing paradigm.

There are many successful attempts to use one large heap (not sure if production ready), but each way has its pros and cons.
View user's profile Send private message
wojtnar
Posted: Wed Jan 05, 2011 2:02 pm Reply with quote
Joined: 05 Feb 2010 Posts: 7
I bet this quote is not from the chapter "VM internals".

I don't think this quote should be read literally. I think it means that erlang is functional.

I really don't think all the arguments are always copied on a function call.

Messages sent from one process to another are copied, with the exception of certain large binaries.
View user's profile Send private message
danielywoo
Posted: Thu Jan 06, 2011 12:30 pm Reply with quote
Joined: 31 Dec 2010 Posts: 8
wojtnar wrote:

I really don't think all the arguments are always copied on a function call.

Messages sent from one process to another are copied, with the exception of certain large binaries.


I fully agree with you but i just cannot find out the official doc to prove this.
View user's profile Send private message
danielywoo
Posted: Thu Jan 06, 2011 12:52 pm Reply with quote
Joined: 31 Dec 2010 Posts: 8
@zadja
But the arguments are "immutable" and can only be bound once, and they should be able to be shared with multiple processes/threads without data race problem, right? In most cases, only mutable objects have data race issue.
View user's profile Send private message
wojtnar
Posted: Wed Jan 12, 2011 3:51 pm Reply with quote
Joined: 05 Feb 2010 Posts: 7
Haskell (GHC) shares values (possibly not yet evaluated) between threads. The effects on garbage collection are nasty.

--Regards,
Wojtek Narczynski
View user's profile Send private message
danielywoo
Posted: Sun Jan 16, 2011 2:30 pm Reply with quote
Joined: 31 Dec 2010 Posts: 8
wojtnar wrote:
Haskell (GHC) shares values (possibly not yet evaluated) between threads. The effects on garbage collection are nasty.

--Regards,
Wojtek Narczynski


Thanks, this is kind of answer I expected Smile

I guess it's just a balance or trade off between "object copying" and “GC complexity". e.g. Java in on the opposite way, Sun put huge effort to develop several GCs and make the VM GCs tunable, but it's still very complicated to tune, you have to know the JVM spec, JMM spec, and each GC's characteristic etc.

I am not saying java is bad, actually I like java a little more Smile
View user's profile Send private message
rvirding
Posted: Mon Jan 24, 2011 10:29 am Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
I think the discussion is going at cross-purposes here.

In Erlang all data is immutable, both the language and the implementation ensure this. This means that if you want to change the value of a field in a data structure you will have copy some of the structure to do this. Basically enough of the structure to reach the data element and to rebuild the structure after the new value has been added.

As data is immutable then there is no problem with the implementation internally sharing data. It cannot be modified so there can be no mysterious changing of data under your feet.

This also means that even though arguments in function calls logically are call-by-value internally they are usually call-by-reference for efficiency. There is no need to copy data in a function call as it cannot be modified anyway.

The same holds for passing data in messages between processes, logically messages are copied and each process has its own copy of the data, but how this is actually done is an implementation detail. For various reasons the BEAM chooses to copy data between processes, but it could just as well send references and have processes share data. Immutability of data ensures that the application would not see the difference.
View user's profile Send private message Visit poster's website MSN Messenger
ultranewb
Posted: Mon Jan 24, 2011 2:29 pm Reply with quote
User Joined: 10 Jan 2011 Posts: 10
Very good information. This is how I thought it SHOULD work, i.e. if data is immutable then there should be no problem with internally passing around references to it for efficiency reasons. I'm glad to see that it works that way.
View user's profile Send private message
bbxj54321
Posted: Tue Aug 07, 2012 7:00 am Reply with quote
Joined: 07 Aug 2012 Posts: 3
Chen une sorte de de la glace pilée banquise en le passé de bandes de mariage final , a pris part à primaire , il est propre que le défavorable organiser . Chen Yibing pour résister le stress , démarrage en Les meilleurs Les aspects facettes air max tn crossstitching , horizontalement fold, dans le mauvais sens fold inactive force mouvement sera systématiquement accomplie , a la suite combinaison aller sur placé dans terminé qualité, directement l'ensemble du corps 360 rotation avec rigoureuse était debout toujours , meilleure efficacité au sein de l' complète forme physique. La dernière très gamme de couple de impossibilité de 7 Sept Les routines poursuites généré lui 13 600 idées.
Chen Yibing, la dominante Les oppositions des adversaires était initialement en PUT global jouer le jeu . Cracking ouvrir Actions d'innombrables post, la personne terminé bien , ainsi que semblait balancent vers le mi étape Croix normes à l'intérieur l' état d'esprit pour aider à garder le dernier un nombre limité saut , les actions complexité est divisé directement dans Cinq Six , une finale reçu environ 15 Zones .
Brésilienne Napa Leiter dernière jouer le jeu , Et quand acné cru typiquement le bijoux médaillon a que Whispering Chen Yibing, toute blessure Comprend montré jusqu'à : le réel Brésil nike tn pas cher groupe de impraticabilité des 6 6 action réalisé grand choix, stop temps remarquablement long terme pour montrer se Les exceptionnel une intéressante , mais le dernière bas du dos a nice mesure pointe. Avec une petite mal à l'aise nostalgie , a Arbitre révélé permanent : 13 700 This Napa Hamlet scène "Hukouduoshi, saisi le particulier lingots d'or médaillon dans pour Chen Yibing, presque au cours de la fourre-tout .
Un nombre incalculable de gens la suite de votre première fois a préoccupation : "Est-il tout un excellent mesures , significativement moins un nouveau avec Les fluctuations alors que dans le étapes En signalés souligné:. "juste conclu voir bijoux aventure , assez déprimée , que infime proportion petite qu'il est difficile pouvoir , si pour vérifier le particulier Les glaçons rendement global est tout à fait excellente grande , i ne crois vous pourriez être un champion Dans
View user's profile Send private message
dongdongwu
Posted: Thu Sep 20, 2012 5:36 am Reply with quote
User Joined: 19 Sep 2012 Posts: 236
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum