[erlang-questions] naive execution of guards
Ulf Wiger (TN/EAB)
ulf.wiger@REDACTED
Tue Dec 12 15:36:02 CET 2006
I was curious to see whether the compiler would optimize away repeated
use of the length/1 guard, e.g in the following code:
check_arity(_, L, Min, Max)
when is_list(L), length(L) =< Min, length(L) >= Max ->
true;
but compiling with erlc -S indicates that it doesn't:
{function, check_arity, 4, 8}.
{label,7}.
{func_info,{atom,test},{atom,check_arity},4}.
{label,8}.
{bif,is_list,{f,9},[{x,1}],{x,4}}.
{gc_bif,length,{f,9},5,[{x,1}],{x,5}}.
{bif,'=<',{f,9},[{x,5},{x,2}],{x,5}}.
{gc_bif,length,{f,9},6,[{x,1}],{x,6}}.
{bif,'>=',{f,9},[{x,6},{x,3}],{x,6}}.
{bif,'and',{f,9},[{x,5},{x,6}],{x,5}}.
{bif,'and',{f,9},[{x,4},{x,5}],{x,4}}.
{test,is_eq_exact,{f,9},[{x,4},{atom,true}]}.
{move,{atom,true},{x,0}}.
return.
I guess there are two reasons for doing the optimization:
- one less register to handle
- length/1 is an O(N) bif
I'm sure it's tricky, and I don't know if the above reasons are strong
enough.
BR,
Ulf W
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20061212/1e5b60a3/attachment.htm>
More information about the erlang-questions
mailing list