xsl-list
[Top] [All Lists]

RE: [xsl] ($possibly-empty, $default)[1] idiom optimisation

2010-03-24 05:32:49

I think it's reasonable to assume that a half-decent processor, given an
expression like

(EXP)[1]

will stop evaluating the sequence EXP after the first item has been found.
So with

($possibly-empty, $default)[1]

the value of $default won't be computed unless it's needed.

Of course there might be other optimizations that interfere and cause the
evaluation to be done eagerly: for example in the case of (//equation)[1]
Saxon will actually build an index of all the equations in the document so
that subsequent evaluations of (//equation) are faster.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay  

-----Original Message-----
From: Andrew Welch [mailto:andrew(_dot_)j(_dot_)welch(_at_)gmail(_dot_)com] 
Sent: 24 March 2010 10:21
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] ($possibly-empty, $default)[1] idiom optimisation

Hi,

This is often used as a nice way to avoid an if-then-else 
check to display $default if $possibly-empty is empty:

($possibly-empty, $default)[1]

Out of interest, is it likely to be optimised to avoid 
evaluating $default if the first item is not empty, because 
if $default takes a reasonable amount of work to evaluate 
it's effectively wasted most of the time (if $default is 
rare).  If it takes more work to evaluate $default than to 
test whether $possibly-empty is empty each time, then this is 
just a short cut rather than good practice.

It reminds me of advice I heard (for Java) to wrap all log 
statements in an if test to check the log level first to 
avoid the unnecessary creation of the log message.  For 
example, constructing a large message for a debug log by 
combining several different things, only for the constructed 
string to be discarded because the log level is info. 
(testing the log level a 1000 times is far better than 
creating and discarding 1000 strings)


--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>