What is the difference between iolist and list? (Classic)

Source: Internet
Author: User
I saw a post on the erlang-china.org asking this question reference has not understood iolist, what is the difference with list? Thank you for your advice ..

I have read the Erlang document for a long time and I haven't written it clearly, so let's look at the source code:
ERTs/emulator/beam/utils. c

Java code
  1. 3015int io_list_len (eterm OBJ)
  2. 3016 {
  3. 3017 eterm * objp;
  4. 3018 Sint Len = 0;
  5. 3019 declare_estack (s );
  6. 3020GotoRochelle again;
  7. 3021
  8. 3022While(! Estack_isempty (s )){
  9. 3023 OBJ = estack_pop (s );
  10. 3024 l_again:
  11. 3025If(Is_list (OBJ )){
  12. 3026 l_iter_list:
  13. 3027 objp = list_val (OBJ );
  14. 3028/* head */
  15. 3029 OBJ = car (objp );
  16. 3030If(Is_byte (OBJ )){
  17. 3031 Len ++;
  18. 3032}Else If(Is_binary (OBJ) & binary_bitsize (OBJ) = 0 ){
  19. 3033 Len + = binary_size (OBJ );
  20. 3034}Else If(Is_list (OBJ )){
  21. 3035 estack_push (S, CDR (objp ));
  22. 3036GotoRochelle iter_list;/* on head */
  23. 3037}Else If(Is_not_nil (OBJ )){
  24. 3038GotoRochelle type_error;
  25. 3039}
  26. 3040/* tail */
  27. 3041 OBJ = cdr (objp );
  28. 3042If(Is_list (OBJ ))
  29. 3043GotoRochelle iter_list;/* on tail */
  30. 3044Else If(Is_binary (OBJ) & binary_bitsize (OBJ) = 0 ){
  31. 3045 Len + = binary_size (OBJ );
  32. 3046}Else If(Is_not_nil (OBJ )){
  33. 3047GotoRochelle type_error;
  34. 3048}
  35. 3049}Else If(Is_binary (OBJ) & binary_bitsize (OBJ) = 0) {/* tail was binary */
  36. 3050 Len + = binary_size (OBJ );
  37. 3051}Else If(Is_not_nil (OBJ )){
  38. 3052GotoRochelle type_error;
  39. 3053}
  40. 3054}
  41. 3055
  42. 3056 destroy_estack (s );
  43. 3057ReturnLen;
  44. 3058
  45. 3059 l_type_error:
  46. 3060 destroy_estack (s );
  47. 3061Return-1;
  48. 3062}
3015int io_list_len (eterm OBJ) 3016 {3017 eterm * objp; 3018 Sint Len = 0; 3019 declare_estack (s); 3020 goto l_again; 30213022 while (! Estack_isempty (s) {3023 OBJ = estack_pop (s); 3024 l_again: 3025 if (is_list (OBJ) {3026 l_iter_list: 3027 objp = list_val (OBJ ); 3028/* head */3029 OBJ = car (objp); 3030 if (is_byte (OBJ) {3031 Len ++; 3032} else if (is_binary (OBJ) & binary_bitsize (OBJ) = 0) {3033 Len + = binary_size (OBJ); 3034} else if (is_list (OBJ) {3035 estack_push (S, CDR (objp); 3036 goto l_iter_list;/* on head */3037} else if (is_not_nil (OBJ) {3038 goto l_type_error; 3039} 3040/* tail */3041 OBJ = cdr (objp); 3042 if (is_list (OBJ) 3043 goto l_iter_list; /* on tail */3044 else if (is_binary (OBJ) & binary_bitsize (OBJ) = 0) {3045 Len + = binary_size (OBJ ); 3046} else if (is_not_nil (OBJ) {3047 goto l_type_error; 3048} 3049} else if (is_binary (OBJ) & binary_bitsize (OBJ) = 0) {/* tail was binary */3050 Len + = binary_size (OBJ); 3051} else if (is_not_nil (OBJ) {3052 goto l_type_error; 3053} 3054} 30553056 destroy_estack (s); 3057 return Len; 30583059 l_type_error: 3060 destroy_estack (s); 3061 return-1; 3062}

From the source code, we can see that iolist is defined as follows:
1. []
2. Binary
3. list, each element is int (0-255) or binary or iolist.
Binary indicates bitsize % 8 = 0.
Int is 0-255

Root @ Ubuntu:/usr/src/OTP # ERL
Erlang r13b04 (erts-5.7.5) [Source] [SMP: 2: 2] [RQ: 2] [async-threads: 0] [hipe] [kernel-Poll: false]

Eshell v5.7.5 (abort with ^ g)
2> iolist_size (<> ).
0
3> iolist_size (<1:1> ).
** Exception error: bad argument
In function iolist_size/1
Called as iolist_size (<1:1>)
4> iolist_size (<> ).
1
5> iolist_size ([]).
0
6> iolist_size (<1, 2> ).
2
7> iolist_size ([1, 2]).
2
8> iolist_size ([1, 2, <1, 2>]).
4
9> iolist_size ([1, 2, <1, 2>, [2]).
5
10> iolist_size ([1, 2, <1, 2>, [2]).
5
11> iolist_size ([<1:1>]).
** Exception error: bad argument
In function iolist_size/1
Called as iolist_size ([<1:1>])
12>

Iolist is used to send data to the port. because the underlying system calls such as writev Support Vector writing, unnecessary flat operations such as iolist_to_binary are avoided, which greatly improves the efficiency.
Recommended multi-purpose. Reproduced: http://mryufeng.iteye.com/blog/634867

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.