[erlang-questions] Preallocated binary — save memory

Anthony Ramine <>
Thu Apr 11 09:58:15 CEST 2013


Correct URL:

	https://gist.github.com/nox/5359459/raw/0b86154804b43b9043a3fed00debe284f4702f10/prealloc_bin.S

-- 
Anthony Ramine

Le 11 avr. 2013 à 09:52, Anthony Ramine a écrit :

> Hello Max.
> 
> I put together some BEAM assembly to write a function that just use bs_init_writable. Do you have any idea which size the initial buffer should be in your code? Could you try using prealloc_bin:new/1[1] and see if it improve things?
> 
> 	https://gist.github.com/nox/5359459/raw/0b8615/prealloc_bin.S
> 
> Regards,
> 
> -- 
> Anthony Ramine
> 
> Le 10 avr. 2013 à 22:50, Max Lapshin a écrit :
> 
>> 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.
>> 
>> 
> 



More information about the erlang-questions mailing list