Erlangbinary data conversion problem

Source: Internet
Author: User

Erlangbinary data conversion problem

Erlang provides binary_to_term to convert binary data into original erlang data. However, this function also has side effects. If you directly perform a binary_to_term operation on the data transmitted from the peer end, the VM crash may be dropped.

Binary_to_term side effects

This is because binary_to_term generates these atoms when binary data has Atomic values. However, the total number of VM atoms is limited, and atoms are not involved in GC. If the number of atoms in the data exceeds the erlang VM limit, the VM crash

To address this problem, erlang provides another function binary_to_term (Binary, [safe]), which does not generate new atoms. At this point, these two function relationships are also like list_to_atom and list_to_existing_atom.

1> term_to_binary (atom ). <131,100, 97,116,111,109,> 2> binary_to_term (v (1), [safe]). atom3> binary_to_term (v (1 )). atom4> binary_to_term (<131,100, 116,101,115,116,>, [safe]). % an exception will be thrown here ** exception error: bad argument in function binary_to_term/2 called as binary_to_term (<131,100, 116,101,115,116,>, [safe]) 5> binary_to_term (<131,100, 116,101,115,116,> ). test6> binary_to_term (<131,100, 116,101,115,116,>, [safe]). % No exception is thrown here test
Term_to_binary binary Compression

Contrary to binary_to_term, term_to_binary converts erlang data into a binary data. By default, the binary data after term_to_binary is not compressed, which is relatively large. erlang also provides another function to compress the generated binary data.

Term_to_binary (Term, [{compressed, Level}]

The value range of Level is 1-9, indicating the compression ratio. 1 indicates the lowest compression ratio, but the minimum time consumed, and 9 indicates the highest compression ratio. It can also be 0, indicating no compression.

In fact, 1 can meet the requirements, and it will not take too much time. Furthermore, the binary after term_to_binary/2 compression has little impact on binary_to_term.

In addition, binary compression of term_to_binary must carry certain overhead, but the overhead is much lower than that of zlib: zip data compression after term_to_binary, and zlib: unzip data is required at the receiving end. Obviously, term_to_binary/2 is suitable for traffic-oriented scenarios.


Reference: http://blog.csdn.net/mycwq/article/details/42622193


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.