How is a list constructed by the Erlang VM (BEAM)?

后端 未结 1 607
深忆病人
深忆病人 2021-01-13 04:42

When I create a list in Erlang, such as in the Erlang shell:

1> [1, 2].

From what I understand, in the vm this list will be represented

相关标签:
1条回答
  • 2021-01-13 05:04

    The Erlang VM implementation BEAM uses a technique which dates back to first Lisp implementations back to the 60s or early 70s. It is sometimes referred as tagged or typed pointers. (Tags) This technique does not store type of a target in a target object (lists CONS in this case) but in the pointer itself or save a scalar value in the place where is usually a pointer. It allows save a quite good amount of memory especially in dynamically typed languages as LISP or Erlang. (It was interesting in old days when memory was very expensive and become important again nowadays when CPU become much faster than memory and cache miss/hit determines a speed of algorithms.) As a drawback, it also leads to a little bit confusing code. The whole part which deals with list construction starts at line 216 of erl_term.h. You can note there is macro

    #define _unchecked_make_list(x) ((Uint) COMPRESS_POINTER(x) + TAG_PRIMARY_LIST)
    

    which is macro you are looking for. It is your make_list. The line

    _ET_DECLARE_CHECKED(Eterm,make_list,const Eterm*)
    

    Would make a checked version of it when compiled with ET_DEBUG. (See more details.) Macro make_list

    #define make_list(x)        _ET_APPLY(make_list,(x))
    

    would just call the checked or unchecked version of make_list. The macros which really constructing list are

    #define CONS(hp, car, cdr) \
            (CAR(hp)=(car), CDR(hp)=(cdr), make_list(hp))
    
    #define CAR(x)  ((x)[0])
    #define CDR(x)  ((x)[1])
    

    The list cell structure is simply two consecutive Uint values on the heap (hp) which address is compressed and tagged (See _unchecked_make_list). I hope this description helps you.

    0 讨论(0)
提交回复
热议问题