[erlang-questions] Question about implementation

Robert Virding robert.virding@REDACTED
Sun Sep 16 23:10:33 CEST 2012


Yes. There are a few rules which need to be followed so that the compiler can safely do this optimisation, and generally doing multiple updates to a record *in one go* will satisfy these rules. So

X1 = X0#r{a=foo(),b=bar(),c=baz()},

will be optimised, while

X1 = X0#r{a=foo()},
X2 = X1#r{b=bar()},
X3 = X2#r{c=baz()},

will not. It always tries to do this optimisation but it is generally seldom safe to do it.

Robert

----- Original Message -----
> From: "Loïc Hoguin" <essen@REDACTED>
> To: "Daniel Dormont" <dan@REDACTED>
> Cc: "Lukas Larsson" <lukas@REDACTED>, "Erlang-Questions Questions" <erlang-questions@REDACTED>
> Sent: Saturday, 15 September, 2012 1:45:58 AM
> Subject: Re: [erlang-questions] Question about implementation
> 
> Hello,
> 
> On 09/10/2012 07:35 PM, Daniel Dormont wrote:
> >
> > On Mon, Sep 10, 2012 at 12:08 PM, Lukas Larsson
> > <lukas@REDACTED <mailto:lukas@REDACTED>>
> > wrote:
> >
> >     Hello,
> >
> >     Only the tuple which is the record and the immidiates (i.e.
> >     small int,
> >     atom, pid, ref etc) will be copied. All other parts use
> >     pointers
> >     internally and the same pointers will be copied into the
> >     correct
> >     places within the tuple. So in this case a new 6 word memory
> >     block
> >     will be created and filled with the atom undefined and finally
> >     the new
> >     counter.
> >
> >     I think that the compiler does some optimization based on
> >     updating
> >     tuples within the same function body, though I've forgotten
> >     what it is
> >     it does.
> >
> >
> > This bit?
> > http://www.erlang.org/doc/efficiency_guide/commoncaveats.html#id61731
> 
> Is the optimization applied automatically to records?
> 
> --
> Loïc Hoguin
> Erlang Cowboy
> Nine Nines
> http://ninenines.eu
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 



More information about the erlang-questions mailing list