Record over Tuples or over optimization?

I’ve been learning Elixir slowly over the past few months. Today I noticed a Hacker News article that links to what is probably an old & popular (to this group) presentation on Erlang and First Person Shooters. It’s about using Erlang as the web server behind the game Call of Duty.

Towards the middle of the presentation it talks about lessons learned. Since this is on Hacker News & is bound to be popular among others new to Elixir, I’m really curious on the community’s opinion about the first lesson learned.

Learn to use the core datatypes: Iolists, records (not tuples), binaries/bitstrings, refs, atoms

What I’m specifically curious is the part about records (not tuples). Obviously I’m far from ever building something with performance needs like the servers in the presentation but I am still curious.

Is this just an optimization thing for people who reach a certain scale?
Is there any value at smaller scales in trying to use records more often?
Does Elixir just handle this for you? I noticed the Record module in the documentation but haven’t used it yet.

3 Likes

Records are just some syntax around triples, there will be no performance difference. Using records instead of triples though has the upside that it is easier to extend. When you want to add a field to a triple you have to find all the occcurences to change the pattern. If you use a record though, the macros will pick up that change and it just works.

3 Likes

Records are actually tuples with special metadata that allows you to access fields of the tuple by name instead of element number. Because this happens at compile-time (see Erlang docs for more information), they are very performant but not more performant than plain tuples.

Elixir can take advantage of Records through the Record module which allows you both to use tuples from Erlang libraries and to create your own records with defrecord/3.

However, in Elixir, most of the time, I think, people tend to use structs instead of records. If you don’t experience actual performance bottlenecks that you think could be solved by using a record instead, I would go with structs.

2 Likes

Thank you @NobbZ & @wmnd for your answers. It sounds like I am best to try & forget that comment that records should be used instead of tuples.

Well, as @NobbZ already pointed out, it does make sense to use Records from a structural perspective because you give “meaning” to the different elements in a tuple which makes your code easier to read and to maintain. It’s just not a performance issue :slight_smile:

1 Like

Sorry, I was thinking that I should forget the comment’s suggestion about using them instead for performance reasons only.

Easier & more maintainable code is one of my biggest reasons for trying out Elixir.