Re: [xsl] XPath Expressions: Separate predicates or one predicate with AND?

Subject: Re: [xsl] XPath Expressions: Separate predicates or one predicate with AND?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 22 Feb 2017 10:13:29 -0000
> On 22 Feb 2017, at 00:10, Dimitre Novatchev dnovatchev@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>> But it occurs to me that in this case the expressions are functionally
identical: the set of predicates is effectively an AND group.
>
>> Is there any reason to prefer one form expression over the other?
>
> No, but the ordering of the sub-expressions (assuming the processor
> does shortcuts) can be really important. That is, place first the
> sub-expression that most-often would make the whole expression false()
> (in case of "and"-ing) or true() (in case of "or"-ing).

Technically, they should probably be ordered by the value of C*P where C is
the cost of evaluation and P is the probability of being false.
>
> I believe that shortcutting isn't specified as mandatory in the XPath
> specifications, but I might be wrong. Certainly, any serious optimizer
> will do shortcutting.
>

XPath 1.0 (in effect) mandates short-cutting, XPath 2.0 and later do not.
That's because the XML database people thought it important to allow
optimizers to evaluate the conditions in any order, for example to take
advantage of indexes.

Saxon will in fact do some re-ordering of predicates on occasions. It can't
assess the probability of a predicate returning false, but it does crudely
estimate the cost of evaluation of each predicate, and will try to evaluate
the cheapest predicates first. This is done after normalizing X[Y][Z] and X[Y
and Z] to a common representation (all assuming of course that the predicates
are non-positional).

Michael Kay
Saxonica

Current Thread