<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6001.18099" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=155244714-03102008><FONT face=Arial 
color=#0000ff size=2>It's now handling this case. It was actually doing the 
final optimization pass, the problem was that the "compareToInteger" expression, 
which is only introduced by the optimizer in a previous phase, wasn't being 
recognized as a candidate for further rewrite when the second operand became 
constant.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=155244714-03102008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=155244714-03102008><FONT face=Arial 
color=#0000ff size=2>Optimizers only improve by trying them out on new test 
cases, so it's always good to get feedback like this.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=155244714-03102008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=155244714-03102008><FONT face=Arial 
color=#0000ff size=2>Michael Kay</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=155244714-03102008><FONT face=Arial 
color=#0000ff size=2><A 
href="http://www.saxonica.com/">http://www.saxonica.com/</A></FONT></SPAN></DIV><BR>
<BLOCKQUOTE dir=ltr 
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
  <DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
  <HR tabIndex=-1>
  <FONT face=Tahoma size=2><B>From:</B> remidewitte@gmail.com 
  [mailto:remidewitte@gmail.com] <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> talk@x-query.com<BR><B>Subject:</B> Re: [xquery-talk] Re: 
  Optimizations<BR></FONT><BR></DIV>
  <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">mike@saxonica.com</A>&gt;</SPAN> wrote:<BR>
  <BLOCKQUOTE class=gmail_quote 
  style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
    <DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff size=2>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 face=Arial color=#0000ff 
    size=2></FONT></SPAN>&nbsp;</DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff 
    size=2>&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="ge" value="4"&gt;
    <DIV 
    class=Ih2E3d><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;literal value="3" 
    type="xs:integer"/&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="debug" 
    validation="preserve"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &lt;variableReference 
    name="dosmthg"/&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="dosmthg"/&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 face=Arial color=#0000ff 
    size=2></FONT></SPAN>&nbsp;</DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff size=2>and it 
    hasn't noticed that the &lt;when&gt; condition is now 
    constant.</FONT></SPAN></DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff 
    size=2></FONT></SPAN>&nbsp;</DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff size=2>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 face=Arial color=#0000ff 
    size=2></FONT></SPAN>&nbsp;</DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff size=2>Michael 
    Kay</FONT></SPAN></DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff size=2><A 
    href="http://www.saxonica.com/" 
    target=_blank>http://www.saxonica.com/</A></FONT></SPAN></DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff 
    size=2></FONT></SPAN>&nbsp;</DIV>
    <DIV dir=ltr align=left><SPAN><FONT face=Arial color=#0000ff 
    size=2></FONT></SPAN>&nbsp;</DIV><BR></DIV>
    <BLOCKQUOTE dir=ltr 
    style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: rgb(0,0,255) 2px solid; MARGIN-RIGHT: 0px">
      <DIV lang=en-us dir=ltr align=left>
      <HR>
      <FONT face=Tahoma size=2>
      <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="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
        <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="level"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;literal value="3" 
        type="xs:integer"/&gt;<BR>&nbsp;&nbsp;&nbsp; 
        &lt;/declareVariable&gt;<BR>&nbsp; &lt;/globalVariables&gt;<BR>&nbsp; 
        &lt;declareFunction name="local:debug" 
        tailRecursive="false"&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;literal 
        value="()"/&gt;<BR>&nbsp; &lt;/declareFunction&gt;<BR>&nbsp; 
        &lt;body&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;let variable="dosmthg" 
        as="element(oki, {<A href="http://www.w3.org/2001/XMLSchema%7Duntyped" 
        target=_blank>http://www.w3.org/2001/XMLSchema}untyped</A>)"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        &lt;be&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        &lt;directElement name="oki" 
        validation="skip"&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="test trace" 
        type="xs:string"/&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="local:debug" 
        tailCall="false"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        &lt;variableReference 
        name="dosmthg"/&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="dosmthg"/&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></BLOCKQUOTE></BODY></HTML>