Erlang: compute data literature (constant) at compile time?

This might be a naive question, and I suspect the answer is yes, but I’m not lucky to find terms like erlang compiler optimization constants here and elsewhere, etc.

Anyway, can the erlang compiler (will) create a data structure that is constant or literal at compile time, and use this instead of creating code that creates the data structure again and again? I will give a simple example of the game.

test() -> sets:from_list([usd, eur, yen, nzd, peso]). 

Maybe (will) the compiler just insert the set there at the output of the function, and not calculate it every time?

I ask if I want to have a lookup table in the program that I am developing. A table is simply constants that can be calculated (at least theoretically) at compile time. I would just like to compute a table once, and don't need to compute it every time. I know that I can do this in other ways, for example, to compute a thing and save it in a process dictionary, for example (or, possibly, an ets or mnesia table). But I always start simple, and for me the simplest solution is to do it as an example of the toy above, if the compiler optimizes it.

If this does not work, is there any other way to achieve what I want? (I think I could look at parsing conversions if they work for this, but is it getting more complicated than I would like?)

IT'S SIMPLE. I used compile: file / 2 with the 'S' option to create the following. I'm not an erlang installation expert, but it seems like optimization is not being performed:

 {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}}. 
+4
source share
3 answers

No, the erlang compiler does not partially evaluate the calls to external modules that set is. You can use the ct_expand module of the famous parse_trans to achieve this effect.

+4
source

ensuring that the set is not a native data type for erlang, and (essentially) is just a library written in erlang, I don’t think it is possible for the compiler to create sets at compile time.

+1
source

As you can see, the sets are not optimized in erlang (like any other library written in erlang). The way to solve your problem is to calculate the set once and pass it as a parameter to functions or use ETS / Mnesia.

+1
source

Source: https://habr.com/ru/post/1395271/


All Articles