<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7650.28">
<TITLE>naive execution of guards</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<BR>
<P><FONT SIZE=2 FACE="Arial">I was curious to see whether the compiler would optimize away repeated use of the length/1 guard, e.g in the following code:</FONT></P>
<P><FONT SIZE=2 FACE="Arial">check_arity(_, L, Min, Max)</FONT>
<BR><FONT SIZE=2 FACE="Arial"> when is_list(L), length(L) =< Min, length(L) >= Max -></FONT>
<BR><FONT SIZE=2 FACE="Arial"> true;</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">but compiling with erlc -S indicates that it doesn't:</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">{function, check_arity, 4, 8}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {label,7}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {func_info,{atom,test},{atom,check_arity},4}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {label,8}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {bif,is_list,{f,9},[{x,1}],{x,4}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {gc_bif,length,{f,9},5,[{x,1}],{x,5}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {bif,'=<',{f,9},[{x,5},{x,2}],{x,5}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {gc_bif,length,{f,9},6,[{x,1}],{x,6}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {bif,'>=',{f,9},[{x,6},{x,3}],{x,6}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {bif,'and',{f,9},[{x,5},{x,6}],{x,5}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {bif,'and',{f,9},[{x,4},{x,5}],{x,4}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {test,is_eq_exact,{f,9},[{x,4},{atom,true}]}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> {move,{atom,true},{x,0}}.</FONT>
<BR><FONT SIZE=2 FACE="Arial"> return.</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">I guess there are two reasons for doing the optimization:</FONT>
<BR><FONT SIZE=2 FACE="Arial">- one less register to handle</FONT>
<BR><FONT SIZE=2 FACE="Arial">- length/1 is an O(N) bif</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">I'm sure it's tricky, and I don't know if the above reasons are strong enough.</FONT>
</P>
<P><FONT SIZE=2 FACE="Arial">BR,</FONT>
<BR><FONT SIZE=2 FACE="Arial">Ulf W</FONT>
</P>
</BODY>
</HTML>