xsl-list
[Top] [All Lists]

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

2016-08-18 02:40:31


On 18 Aug 2016, at 04:46, Mailing Lists Mail daktapaal(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>
  • [xsl] Use of copy-of(.), Mailing Lists Mail daktapaal(_at_)gmail(_dot_)com
    • Re: [xsl] Use of copy-of(.), Michael Kay mike(_at_)saxonica(_dot_)com <=