Constant structures optimisation? (Re: CRC16 in erlang)

Thomas Johnsson thomas@REDACTED
Tue Jan 31 15:58:59 CET 2006

Thomas Lindgren wrote:

>--- Rudolph van Graan <rvg@REDACTED> wrote:
>>Hi Joe,
>>Here is one which I wrote some time ago. It does
>>work, but there  
>>might be some issues. (No guarantees on speed!)
>Here is an easy speedup:
>-define(CRC16Def, {...}).
>crc_index(N) -> element(N+1, ?CRC16Def).
>This is O(1) time rather than O(N) and 1/2 the space
>of the original definition. Pretty good, but there's
No, it is going to be O(N') (where N' is the size of the table), since 
the structure ?CRC16Def will be built every time !
At least that is the way it looks by looking at the -S output of the 

crc_index(N) -> element(N, ?t).

erlc -S stest.erl    =====> stest.S :
{function, crc_index, 1, 6}.

At least at this level of the code it looks like big structure constants 
are built at every call anyway.
The beaviour looks similar for lists.
But perhaps some lower level of the compiler discovers this and puts the 
constant in a constant area?

-- Thomas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the erlang-questions mailing list