Today when I read the Erlang Doc about Efficiency Guide Advanced Chapter
small integer:
1 word
On 32-bit architectures: -134217729 < i < 134217728 (28 bits)
On 64-bit architectures: -576460752303423489 < i < 576460752303423488 (60 bits)big integer:
3..N words
(sorry, I tried to use tables in markdown, but It didn't work, forgive me, this is my first question here)
why use 28 bits or 60 bits? what's the function of the rest 4 bits? and small or big is judged by the erlang VM? the 32-bit architectures means 32-bits system or cpu? my English is poor and I new to Erlang. thks .
The remaining four bits are used to identify types other than numbers, such as lists, tuples, pids, etc.
The alternative way of doing things would be having an extra byte or word prepended to each value, identifying what type it is. However, packing the value together with the type specifier means that many values can fit in a single word.
Whether the VM is 32-bit or 64-bit depends on how it was compiled. When you start up Erlang, you'll see [64-bit] in the banner if it was compiled as a 64 bit program:
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
You can also check with erlang:system_info(wordsize):
1> erlang:system_info(wordsize).
8
In this case, the word size is 8 bytes, i.e. 64 bits.
Being a dynamically typed language, Erlang uses four bits of each word as a type tag or "dope-field" in order to identify the type of data referred to by that word. Erlang actually has indefinite precision integers; if the integer is larger than 28 bits on 32 bit word architectures or 60 bits on 64 bit architectures then it will be transparently boxed into a larger object on the heap, as many bytes as required to represent it. However boxed integers are less efficient to perform operations with because they involve an additional layer of indirection. This is why the efficiency guide makes a note of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With