<!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> </DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial
color=#0000ff size=2>
<return><BR>
<sequence><BR>
<choose><BR>
<when><BR>
<compareToInteger op="ge"
value="4"><BR>
<literal value="3"
type="xs:integer"/><BR>
</compareToInteger><BR>
</when><BR>
<then><BR>
<directElement name="debug"
validation="preserve"><BR>
<variableReference
name="dosmthg"/><BR>
</directElement><BR>
</then><BR>
</choose><BR>
<variableReference
name="dosmthg"/><BR>
</sequence><BR>
</return></FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial
color=#0000ff size=2>and it hasn't noticed that the <when> 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> </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, two more passes would
have paid dividends - one to evaluate the <when> condition, the other to
reduce the <choose> to ().</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </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> </DIV>
<DIV dir=ltr align=left><SPAN class=348121714-03102008><FONT face=Arial
color=#0000ff size=2> </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> if($l >=4) then<BR>
<debug>{$what}</debug><BR> else ()<BR>};<BR><BR>let $dosmthg
:= <oki>test
trace</oki><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><<A href="mailto:remi@gide.net">remi@gide.net</A>></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> if($level >=4)
then<BR> <debug>{$what}</debug><BR> else
()<BR>};<BR><BR>let $dosmthg := <oki>test
trace</oki><BR><BR>return<BR>(local:debug($dosmthg),$dosmthg)<BR><BR>When
I run saxon with -explain we get <BR><BR><query><BR>
<globalVariables><BR> <declareVariable
name="level"><BR> <literal value="3"
type="xs:integer"/><BR>
</declareVariable><BR> </globalVariables><BR>
<declareFunction name="local:debug"
tailRecursive="false"><BR> <literal
value="()"/><BR> </declareFunction><BR>
<body><BR> <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>)"><BR>
<be><BR> <directElement
name="oki"
validation="skip"><BR>
<valueOf><BR>
<literal value="test trace"
type="xs:string"/><BR>
</valueOf><BR>
</directElement><BR>
</be><BR>
<return><BR>
<sequence><BR>
<functionCall name="local:debug"
tailCall="false"><BR>
<variableReference
name="dosmthg"/><BR>
</functionCall><BR>
<variableReference
name="dosmthg"/><BR>
</sequence><BR>
</return><BR> </let><BR>
</body><BR></query><BR><BR>Cheers,<BR>Rémi<BR></DIV></BLOCKQUOTE></DIV><BR></DIV></BLOCKQUOTE></BODY></HTML>