[erlang-questions] Preallocated binary — save memory

Max Lapshin max.lapshin@REDACTED
Wed Apr 10 22:50:12 CEST 2013


I've looked at erlc -S bc.erl:

{function, bc, 1, 2}.
  {label,1}.
    {line,[{location,"bc.erl",3}]}.
    {func_info,{atom,bc},{atom,bc},1}.
  {label,2}.
    {allocate_zero,1,1}.
    {line,[{location,"bc.erl",3}]}.
    {gc_bif,bit_size,{f,0},1,[{x,0}],{x,1}}.
    {line,[]}.
    {gc_bif,'div',{f,0},2,[{x,1},{integer,336}],{x,1}}.
    {line,[]}.
    {gc_bif,'*',{f,0},2,[{x,1},{integer,45}],{x,1}}.
    {move,{x,0},{y,0}}.
    {move,{x,1},{x,0}}.
    bs_init_writable.
    {move,{x,0},{x,1}}.
    {move,{y,0},{x,0}}.
    {call_last,2,{f,8},1}.



It is really cool!  Erlang understands that resulting binary will be
exactly 45 * (size(Input) div 42) bytes and preallocates it.
This is a very important reason to use list and binary comprehensions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130411/a69e7f5d/attachment.htm>


More information about the erlang-questions mailing list