[Erlang 0032] internal implementation of Erlang binary

Source: Internet
Author: User
[Erlang 0014] Erlang garbage collection mechanism [Erlang 0024] Erlang binary data processing the previous two articles mentioned binary data-related content, today, I read about binary implementation in the official documentation and take notes here. first look at a picture:

 

 

Mind Map explanation

  • The internal implementation mechanism of binary and bitstring is the same.
  • Erlang has four binary types, two containers, and two references.
  • Containers include refc binaries (reference count binary) and heap Binaries
  • Refc binaries can be divided into two parts: procbin of process heap and binary objects other than process heap.
  • Procbin contains metadata of binary data, including the location and reference count of binary data.
  • Binary objects outside the process heap can be referenced by any number of processes and any number of procbin. This object contains the reference counter. Once the counter is set to zero, it can be removed.
  • All procbin objects are part of the linked list, so GC traces them and removes the application count by one when procbin disappears.
  • Heap binaries is a small binary data with a maximum of 64 bytes, which is directly stored in the process heap. The garbage collection and message sending are implemented through copying, and no special processing is required by the garbage collector.
  • There are two types of objects that can reference part of refc binary and heap binary data: Sub binaries, match contexts
  • Sub binary is generated when split_binary is used. sub binary is another application of binary data (refc or heap binary). Because there is no data copy, binary mode matching costs are quite low.
  • Match context is similar to sub binary, but it is optimized for binary matching. For example, it contains a pointer pointing directly to binary data. You can move the pointer position after the field value is obtained from binary matching.

Official Document link: http://www.erlang.org/doc/efficiency_guide/binaryhandling.html

Internally, binaries and bitstrings are implemented in the same way.

There are four types of binary objects internally. Two of them areContainersFor binary data and two of them are merelyReferencesTo a part of a binary.

The binary containers are called Refc Binaries(Short for reference-counted binaries) and Heap Binaries. Refc binaries consist of two parts: an object stored on the process heap, called Procbin, And Binary objectItself stored outside all process heaps. the binary object can be referenced by any number of procbins from any number of processes; the object contains a reference counter to keep track of the number of references, so that it can be removed when the last reference disappears. all procbin objects in a process are part of a linked list, so that the garbage collector can keep track of them and decrement the reference counters in the binary when a procbin disappears.

Heap BinariesAre small binaries, up to 64 bytes, that are stored directly on the process heap. they will be copied when the process is garbage collected and when they are sent as a message. they don't require any special handling by the garbage collector.

There are two types of reference objects that can reference part of a refc binary or heap binary. They are called Sub BinariesAnd Match contexts.

A sub binary is created by split_binary/2 and when a binary is matched out in a binary pattern. A sub binary is a reference into a part of another binary (refc or heap binary, never into a another sub binary ). therefore, matching out a binary is relatively cheap because the actual binary data is never copied.
A match context is similar to a sub binary, but is optimized for binary matching; for instance, it contains a direct pointer to the binary data. for each field that is matched out of a binary, the position in the match context will be incremented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.