<!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=348121714-03102008><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 class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><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;<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>&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;<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;</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><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 class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><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 dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2>Michael Kay</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2>http://www.saxonica.com/</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial 
color=#0000ff size=2>&nbsp;</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> talk-bounces@x-query.com 
  [mailto:talk-bounces@x-query.com] <B>On Behalf Of </B>Rémi 
  Dewitte<BR><B>Sent:</B> 03 October 2008 14:24<BR><B>To:</B> 
  talk@x-query.com<BR><B>Subject:</B> [xquery-talk] Re: 
  Optimizations<BR></FONT><BR></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">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></BLOCKQUOTE></BODY></HTML>