Reputation: 41
I'm using a particular method in my Xquery as given below
declare function ctrlv($node, $node-id as xs:string)
{
<z:b>
{
(attribute {"xml:id"}{$node/@xml:id})
}</z:b>
};
This particular function is looped through 11,000 times with diff $node. The given method alone takes 50-60% of whole response time(around 8secs). When i carried out performance profiling it showed $node/@xml:id takes the maximum time. How to improve performance in this case? Please help
Upvotes: 2
Views: 666
Reputation: 7840
Is your computer particularly slow? The profiler tells me that this runs in 250-ms.
declare function local:do($node)
{
element b { $node/@xml:id }
};
distinct-values(
(1 to 10 * 1000) ! local:do(<test xml:id="a123"/>))
Another thing to try is an XSLT implementation. You'll still need to evaluate $node/@xml:id
11,000 times, but getting to that expression happen a little faster.
Upvotes: 0
Reputation: 7335
If you are writing the modified document back to the database with the same uri, try using the xdmp:node-replace() function to modify the attribute values in place instead of constructing a new document tree in memory and writing it back to the database.
If you aren't, what happens in the loop? Are you constructing a new document with each iteration or does the iterator recurse over the tree with a typeswitch, constructing new attributes and their ancestor elements but copying all other nodes?
Upvotes: -2
Reputation: 3732
There are incremental steps to improving speed of these things. Creation of XML elements is somewhat expensive (although .3 ms is not that bad ... but if you do 10k of those it does add up)
Here are some steps when you identify the most frequenly used expressions. They are suggestions of what to do after you identify performance issues, not before, because most often the performance impact is minimal and code readability and maintainability is more important. But once you identify a hot spot consider these:
limit function calls - something this simple can be done inline without a function. Limit binding to variables Limit for loops Limit dynamic construction of elements and attributes
Of course you cant do all of these but you can decide where the pain is most and apply the concepts there. For example the above call could instead be inlined as
<z:b xml:id="{$node/@xml:id}"/>
Try placing this inline where you tried the function and see what the results are.
Also note that the profiler can sometimes give misleading information. Many expressions are lazily evaluated and tend to attribute their time to where they are used instead of where they are declared.
Upvotes: 1
Reputation: 6218
I am not quite sure why you construct a new attribute with the same name as you get from $node
and insert the value of @id
. As there should be some overhead using the construction of an element I would guess the following is faster (and should provide the same output):
declare function ctrlv($node, $node-id as xs:string)
{
<z:b>
{$node/@xml:id}
</z:b>
};
Also, $node-id
is never used in your function, so you might want to remove it as an argument.
Upvotes: 2