Note that there are some explanatory texts on larger screens.

plurals
  1. POErlang: Compute data structure literal (constant) at compile time?
    primarykey
    data
    text
    <p>This may be a naive question, and I suspect the answer is "yes," but I had no luck searching here and elsewhere on terms like "erlang compiler optimization constants" etc.</p> <p>At any rate, can (will) the erlang compiler create a data structure that is constant or literal at compile time, and use that instead of creating code that creates the data structure over and over again? I will provide a simple toy example.</p> <pre><code>test() -&gt; sets:from_list([usd, eur, yen, nzd, peso]). </code></pre> <p>Can (will) the compiler simply stick the set there at the output of the function instead of computing it every time?</p> <p>The reason I ask is, I want to have a lookup table in a program I'm developing. The table is just constants that can be calculated (at least theoretically) at compile time. I'd like to just compute the table once, and not have to compute it every time. I know I could do this in other ways, such as compute the thing and store it in the process dictionary for instance (or perhaps an ets or mnesia table). But I always start simple, and to me the simplest solution is to do it like the toy example above, if the compiler optimizes it.</p> <p>If that doesn't work, is there some other way to achieve what I want? (I guess I could look into parse transforms if they would work for this, but that's getting more complicated than I would like?)</p> <p>THIS JUST IN. I used compile:file/2 with an 'S' option to produce the following. I'm no erlang assembly expert, but it looks like the optimization isn't performed:</p> <pre><code>{function, test, 0, 5}. {label,4}. {func_info,{atom,exchange},{atom,test},0}. {label,5}. {move,{literal,[usd,eur,yen,nzd,peso]},{x,0}}. {call_ext_only,1,{extfunc,sets,from_list,1}}. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload