Changes

Jump to navigation Jump to search
818 bytes added ,  10:39, 25 April 2022
==Loop Unrolling==
 
{{Introduced with Version 9.6:}}
Loops with few iterations are ''unrolled'' by the XQuery compiler to enable further optimizations:
(: rewritten to :)
1 ! (. * 2),2 ! (. * 2) (: further rewritten to :)1 * 2, 2 * 2 (: further rewritten to :)2 , 4</syntaxhighlight> Folds are unrolled, too: <syntaxhighlight lang="xquery">let $f := function($a, $b) { $a * $b }return fold-left(2to 5, 1, $f)
(: rewritten to :)
let $f := function($a, $b) { $a * $b }return $f($f($f($f(1, 2), 3), 4), 5)
</syntaxhighlight>
The standard unroll limit is <code>5</code>; it . It can be adjusted via with the {{Option|UNROLLLIMIT}} option, e.g. via a pragma:
<syntaxhighlight lang="xquery">
(# db:unrolllimit 100 10 #) {
for $i in 1 to 10
return db:open('db' || $i )//* $i[text() = 'abc']
}
(: rewritten to :)
1 db:open('db1')//* 1[text() = 'abc'],2 db:open('db2')//* 2[text() = 'abc'],
...
db:open('db10')//*[text() = 'abc'],
</syntaxhighlight>
 
The last example indicates that index rewritings might be triggered by unrolling loops with paths on database nodes.
 
The following expressions can be unrolled:
 
* Simple map expressions
* Simple FLWOR expressions
* Filter expressions
* [[Higher-Order Functions#fn:fold-left|fn:fold-left]], [[Higher-Order Functions#fn:fold-right|fn:fold-right]], {{Function|Higher-Order Functions|fn:fold-left1}}
Care should be taken if a higher value is selected, as memory consumption and compile time will increase.
(: equivalent queries, with identical syntax trees :)
doc('addressbook.xml')//city,
doc('addressbook.xml')/descendant-or-self::node()/child::city
(: rewritten to :)
where $t/buyer/@person = $p/@id
return $t
return <item person='"{ $p/name/text() }'">{ count($a) }</item>,
(: rewritten to :)
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu