Re: [xsl] Use of copy-of(.)

Subject: Re: [xsl] Use of copy-of(.)
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 18 Aug 2016 07:40:26 -0000
> On 18 Aug 2016, at 04:46, Mailing Lists Mail daktapaal@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Dear all,
> Is it a fair assumption that we should not use xxxx/copy-of(.) in the select
of a variable when we know that the node set xxxx i am selecting is going to
be huge ? Mainly because it defeats the purpose of variables ? Or does it not
matter ?
> Dak
>

(Note: same applies to xsl:copy-of)

It's best not to copy nodes unless you actually need to.

An optimizer may be able to optimize an unnecessary copy away. For example
when you write

<out>
  <xsl:copy-of select="in"/>
</out>

you are technically asking for the input to be copied twice, which you could
avoid by writing

<out>
  <xsl:sequence select="in"/>
</out>

but there's a good chance that any decent optimizer will generate identical
code for the two cases.

When you bind to a variable it's more difficult to optimize an unnecessary
copy away

<xsl:variable name="header"  as="element(header)*" select="//header"/>

versus

<xsl:variable name="header" as="element(header)*">
  <xsl:copy-of select="//header"/>
</xsl:variable>

because the two variables actually behave differently (consider $header[. is
(//header)[2]] ). Saxon in such cases will sometimes try to avoid the memory
cost of copying by using a "virtual copy", which is essentially a tree that
shares data with the original as far as possible. But this certainly doesn't
eliminate all the costs (and to some extent it uses more time in order to use
less memory).

copy-of() as a function, rather than as an instruction, was introduced
specifically for XSLT 3.0 streaming use cases, but of course it's not confined
to that scenario, and it's possibly even easier to use it unnecessarily than
to use xsl:copy-of unnecessarily.

Michael Kay
Saxonica

Current Thread