<div dir="ltr">Thanks Michael for these explanations,<br>It is very good to know these details. And a good motivation to buy Saxon-SA.<br><br>Rémi<br><br><div class="gmail_quote">On Fri, Oct 3, 2008 at 4:27 PM, Michael Kay <span dir="ltr">&lt;<a href="mailto:mike@saxonica.com">mike@saxonica.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">



<div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">This is a case where Saxon (even Saxon-SA) has stopped 
optimizing when it could have achieved a little more. The relevant part of the 
final expression tree is</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;return&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;sequence&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;choose&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;when&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;compareToInteger op=&quot;ge&quot; 
value=&quot;4&quot;&gt;<div class="Ih2E3d"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;literal value=&quot;3&quot; 
type=&quot;xs:integer&quot;/&gt;<br></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/compareToInteger&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/when&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;then&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;directElement name=&quot;debug&quot; 
validation=&quot;preserve&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;variableReference 
name=&quot;dosmthg&quot;/&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/directElement&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/then&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/choose&gt;<div class="Ih2E3d"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;variableReference 
name=&quot;dosmthg&quot;/&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/sequence&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/return&gt;</div></font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">and it hasn&#39;t noticed that the &lt;when&gt; condition is 
now constant.</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Deciding when to stop optimizing is the trickiest part of 
the exercise - if you go on too long, optimization can take longer than any 
run-time savings it is delivering. In this occasion,&nbsp;two more passes would 
have paid dividends - one to evaluate the &lt;when&gt; condition, the other to 
reduce the &lt;choose&gt; to ().</font></span></div><div class="Ih2E3d">
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">Michael Kay</font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"><a href="http://www.saxonica.com/" target="_blank">http://www.saxonica.com/</a></font></span></div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial"></font></span>&nbsp;</div>
<div dir="ltr" align="left"><span><font size="2" color="#0000ff" face="Arial">&nbsp;</font></span></div><br>
</div><blockquote dir="ltr" style="border-left: 2px solid rgb(0, 0, 255); padding-left: 5px; margin-left: 5px; margin-right: 0px;">
  <div dir="ltr" align="left" lang="en-us">
  <hr>
  <font size="2" face="Tahoma"><div class="Ih2E3d"><b>From:</b> <a href="mailto:talk-bounces@x-query.com" target="_blank">talk-bounces@x-query.com</a> 
  [mailto:<a href="mailto:talk-bounces@x-query.com" target="_blank">talk-bounces@x-query.com</a>] <b>On Behalf Of </b>Rémi 
  Dewitte<br></div><b>Sent:</b> 03 October 2008 14:24<div class="Ih2E3d"><br><b>To:</b> 
  <a href="mailto:talk@x-query.com" target="_blank">talk@x-query.com</a><br></div><b>Subject:</b> [xquery-talk] Re: 
  Optimizations<br></font><br></div><div><div></div><div class="Wj3C7c">
  <div></div>
  <div dir="ltr">Hi again,<br><br>In the same spirit, this is not at all 
  optimized.<br>Maybe I need Saxon SA ?<br><br><br>declare variable $level as 
  xs:integer := 3 ;<br><br>declare function local:debug($l as 
  xs:integer,$what){<br>&nbsp; if($l &gt;=4) then<br>&nbsp;&nbsp;&nbsp; 
  &lt;debug&gt;{$what}&lt;/debug&gt;<br>&nbsp; else ()<br>};<br><br>let $dosmthg 
  := &lt;oki&gt;test 
  trace&lt;/oki&gt;<br><br>return<br>(local:debug($level,$dosmthg),$dosmthg)<br><br><br>Rémi<br><br>
  <div class="gmail_quote">On Fri, Oct 3, 2008 at 3:17 PM, Rémi Dewitte <span dir="ltr">&lt;<a href="mailto:remi@gide.net" target="_blank">remi@gide.net</a>&gt;</span> 
  wrote:<br>
  <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
    <div dir="ltr">Hi,<br><br>I have made a little experiment after reading 
    Michael (Kay) blog posts about optimizations and recent James Fuller 
    article.<br><br>I have a question quite specific to saxon still interesting 
    in general.<br><br>Why in the following example even the function call is 
    not skipped ?<br><br><br>XQuery is ::<br><br>declare variable $level := 
    3;<br><br>declare function local:debug($what){<br>&nbsp; if($level &gt;=4) 
    then<br>&nbsp;&nbsp;&nbsp; &lt;debug&gt;{$what}&lt;/debug&gt;<br>&nbsp; else 
    ()<br>};<br><br>let $dosmthg := &lt;oki&gt;test 
    trace&lt;/oki&gt;<br><br>return<br>(local:debug($dosmthg),$dosmthg)<br><br>When 
    I run saxon with -explain we get <br><br>&lt;query&gt;<br>&nbsp; 
    &lt;globalVariables&gt;<br>&nbsp;&nbsp;&nbsp; &lt;declareVariable 
    name=&quot;level&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;literal value=&quot;3&quot; 
    type=&quot;xs:integer&quot;/&gt;<br>&nbsp;&nbsp;&nbsp; 
    &lt;/declareVariable&gt;<br>&nbsp; &lt;/globalVariables&gt;<br>&nbsp; 
    &lt;declareFunction name=&quot;local:debug&quot; 
    tailRecursive=&quot;false&quot;&gt;<br>&nbsp;&nbsp;&nbsp; &lt;literal 
    value=&quot;()&quot;/&gt;<br>&nbsp; &lt;/declareFunction&gt;<br>&nbsp; 
    &lt;body&gt;<br>&nbsp;&nbsp;&nbsp; &lt;let variable=&quot;dosmthg&quot; 
    as=&quot;element(oki, {<a href="http://www.w3.org/2001/XMLSchema%7Duntyped" target="_blank">http://www.w3.org/2001/XMLSchema}untyped</a>)&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;be&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;directElement 
    name=&quot;oki&quot; 
    validation=&quot;skip&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;valueOf&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;literal value=&quot;test trace&quot; 
    type=&quot;xs:string&quot;/&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;/valueOf&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;/directElement&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;/be&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;return&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;sequence&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;functionCall name=&quot;local:debug&quot; 
    tailCall=&quot;false&quot;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;variableReference 
    name=&quot;dosmthg&quot;/&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;/functionCall&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;variableReference 
    name=&quot;dosmthg&quot;/&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;/sequence&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;/return&gt;<br>&nbsp;&nbsp;&nbsp; &lt;/let&gt;<br>&nbsp; 
    &lt;/body&gt;<br>&lt;/query&gt;<br><br>Cheers,<br>Rémi<br></div></blockquote></div><br></div></div></div></blockquote></div>
</blockquote></div><br></div>