Question about Lists

I was just wondering how elixir loads a linked list? For example let’s say I had an ets table with a key whose value was a linked list. E.g.

iex>, [:named_table])
iex> :ets.insert(:lists, {:test, [1,2,3,4,5,6,7,8,9,0]})

If I were to call:

iex> test = :ets.lookup(:lists, :test)

Would it:

  1. Load the entire list into memory (edit mean’t to add ‘copy’ here)? e.g. all 18 bytes (:erts_debug.size([1,2,3,4,5,6,7,8,9,0]) # => 18)
  2. Just load the first/head object into memory (copy) and only the rest when it’s accesed?

If it’s in ETS, it’s already in memory.

But it’s worth noting that according to the erlang docs:

In the current implementation, every object insert and look-up operation results in a copy of the object.

oops yeah I meant copy. So basically, it will create a copy of the entire list, and not just the head element?

Would this be the same for a process state? e.g. it creates a copy on say a handle_call callback?

In general every bit of data that crosses processes boundaries is copied. There is only a very small set of exceptions to this rule. Bit strings with a length of more than 64 byte are the only one I’m aware of.

Also your 10 element list of integers has 108+8 byte on a 64 bit system and 104+4 on 32 bitters. You can read about the size of every data type somewhere in the erlang manuals (can’t look it up, since I’m on mobile)