Note that there are some explanatory texts on larger screens.

plurals
  1. POErlang compiler optimizations
    primarykey
    data
    text
    <p>I'm no compiler optimization expert. I'm not even sure what's "reasonable" to expect as far as compilers or optimizations. I'm just curious and asking questions is all.</p> <p>At any rate, I was coding up some Erlang in a basic form such as this:</p> <pre><code>% TOY EXAMPLE 1 test(X) -&gt; if X-1 &gt; 0 -&gt; yes; X-1 == 0 -&gt; maybe; true -&gt; no end. </code></pre> <p>Then I then optimized it in order not to do the subtract twice:</p> <pre><code>% TOY EXAMPLE 2 test(X) -&gt; Z = X-1, if Z &gt; 0 -&gt; yes; Z == 0 -&gt; maybe; true -&gt; no end. </code></pre> <p>Then I thought "a waste of time - surely the compiler optimizes the first example into the second anyway." So I decided to check by running compile:file with 'S' option for both. Here is output:</p> <pre><code>% TOY EXAMPLE 1 {function, test, 1, 15}. {label,14}. {func_info,{atom,exchange},{atom,test},1}. {label,15}. {gc_bif,'-',{f,16},1,[{x,0},{integer,1}],{x,1}}. {test,is_lt,{f,16},[{integer,0},{x,1}]}. {move,{atom,yes},{x,0}}. return. {label,16}. {gc_bif,'-',{f,17},1,[{x,0},{integer,1}],{x,1}}. {test,is_eq,{f,17},[{x,1},{integer,0}]}. {move,{atom,maybe},{x,0}}. return. {label,17}. {move,{atom,no},{x,0}}. return. % TOY EXAMPLE 2 {function, test, 1, 15}. {label,14}. {func_info,{atom,exchange},{atom,test},1}. {label,15}. {gc_bif,'-',{f,0},1,[{x,0},{integer,1}],{x,0}}. {test,is_lt,{f,16},[{integer,0},{x,0}]}. {move,{atom,yes},{x,0}}. return. {label,16}. {test,is_eq,{f,17},[{x,0},{integer,0}]}. {move,{atom,maybe},{x,0}}. return. {label,17}. {move,{atom,no},{x,0}}. return. </code></pre> <p>They aren't the same. If I'm reading this right (maybe I'm not), the optimization isn't performed.</p> <p>I can see a few possibilities:</p> <ol> <li><p>The optimization can be performed, I'm just not enabling the optimizations because I'm using the wrong function to compile, or not using the correct flags, etc.</p></li> <li><p>The optimization just isn't performed.</p></li> <li><p>Other.</p></li> </ol> <p>Which is it?</p> <p>Note: Please don't get bogged down with talk of "if you use a case statement you can do such-and-such" or "you can avoid this by doing blah-blah." The point is simply to test which optimizations the erlang compiler does or doesn't do, and why or why not.</p> <p>Thanks.</p>
    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