Christian,

Hi. Thanks for the FLWOR trick, behaving in constant time. However it seems to generates an unexpected 5X time overhead, and I can't use it straightforwardly as workaround. Can you reproduce it, since this overhead might be due to eclipse ?

declare function local:test(){fn:for-each(1 to 10000000, function($a){2*$a})};
declare function local:test1(){count(local:test())};
declare function local:test2(){let $ data := local:test() return for $n in (1 to 10) return count($data)};

prof:time(local:test1()) ouput : 639.12 ms
prof:time(local:test2()) ouput : 2431.67 ms



2015-01-13 1:59 GMT+01:00 Christian Grün <christian.gruen@gmail.com>:
Hi Jean-Marc,

interesting one; it seems that $data is inlined and thus evaluated
more than once. This doesn't happen if a FLWOR expression is used:

  let $data := fn:for-each ...
  for $n in 1 to $i
  return local:test2($data, $n)

Should be easy to resolve.

Thanks for your (always concise) examples,
Christian


On Mon, Jan 12, 2015 at 9:08 PM, jean-marc Mercier
<jeanmarc.mercier@gmail.com> wrote:
> Hello,
>
> I am facing some performances issues, probably due to unexpected query
> reevaluation. Might it be due to the optimizer ? Here is a code to test
>
> declare function local:test($i){
>     let $data := fn:for-each(1 to 10000000, function($a){2*$a} )
>  (:     let $data := xquery:eval("fn:for-each(1 to 10000000,
> function($a){2*$a} )") :)
>      return (1 to $i) ! local:test2($data,.)
> };
>
> declare function local:test2($data, $dummy) { count($data) };
>
> (1 to 10) ! prof:time(local:test(.))
>
> Output :
>
> 683.76 ms
> 1137.95 ms
> 1727.68 ms
> 2151.06 ms
> 2694.84 ms
> 3189.57 ms
> 3725.1 ms
> 4277.51 ms
> 4815.74 ms
> 5417.79 ms
>
> Note that using xquery:eval (toggling the comment inside local:test). seems
> to perform correctly. However I can't use this trick as a workaround.
>
> (1 to 10) ! prof:time(local:test(.))
> Ouput :
>
> 2472.71 ms
> 825.61 ms
> 3013.14 ms
> 986.81 ms
> 2982.97 ms
> 778.24 ms
> 1195.33 ms
> 3280.7 ms
> 976.96 ms
> 913.69 ms