[erlang-questions] Record efficiency

Witold Baryluk baryluk@REDACTED
Wed Jul 29 17:35:40 CEST 2009


Dnia 2009-07-29, śro o godzinie 17:22 +0200, Erik Rigtorp pisze:
> Hi!
> 
> I'm trying to figure out the performance characteristics of records.
> I've been looking at the code at it seems like named attribute access
> on records gets turned into some function evaluation by the
> preprocessor. I'm trying to read a bunch of attributes of a record and
> there are two ways to do this.
> 
> Either:
> Rec#rec{attr1 = Attr1, attr2 = Attr2}
this is record assigment, it is pattern only in function head right?

> Or:
> Attr1 = Rec#rec.attr1
> Attr1 = Rec#rec.attr2
> 
> Will these two have the same performance or will the first one just be
> a match and the second one result in two function calls?


They will be exactly the same (compile using erlc -S and compare
assembly). It isn't two functions calls. Records are just syntactic
sugar for tuples, and accessing "fields" of records are like
element/2 call for tuple, but it isn't function exactly, it is BIF
implemented directly in VM.

One can argue that in second case compiler will test Rec variable two
times, if it is really record rec, but actually it test it only once.


-- 
Witold Baryluk <baryluk@REDACTED>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: To jest cz??? wiadomo?ci podpisana cyfrowo
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090729/1f0775de/attachment.bin>


More information about the erlang-questions mailing list