<div dir="ltr">To be honest this is this kind of feature (like differed evaluation of variables) I really like in XQuery and specifically in saxon. It allows a logging module without paying any runtime penalty if a level is disabled. With the limit of not being able to change logging level at runtime but I am ok with that.<br>
<br>Rémi<br><br><div class="gmail_quote">On Fri, Oct 3, 2008 at 4:50 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">It&#39;s now handling this case. It was actually doing the 
final optimization pass, the problem was that the &quot;compareToInteger&quot; expression, 
which is only introduced by the optimizer in a previous phase, wasn&#39;t being 
recognized as a candidate for further rewrite when the second operand became 
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">Optimizers only improve by trying them out on new test 
cases, so it&#39;s always good to get feedback like this.</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><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"><b>From:</b> <a href="mailto:remidewitte@gmail.com" target="_blank">remidewitte@gmail.com</a> 
  [mailto:<a href="mailto:remidewitte@gmail.com" target="_blank">remidewitte@gmail.com</a>] <b>On Behalf Of </b>Rémi 
  Dewitte<br><b>Sent:</b> 03 October 2008 15:38<br><b>To:</b> Michael 
  Kay<br><b>Cc:</b> <a href="mailto:talk@x-query.com" target="_blank">talk@x-query.com</a><br><b>Subject:</b> Re: [xquery-talk] Re: 
  Optimizations<br></font><br></div><div><div></div><div class="Wj3C7c">
  <div></div>
  <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" target="_blank">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><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><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>
    <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"></font></span>&nbsp;</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><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><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>
      <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></div></div></blockquote></div>
</blockquote></div><br></div>