Bill Allombert on Wed, 01 Feb 2012 16:13:42 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: automatic 'return' elimination


On Wed, Feb 01, 2012 at 12:46:36AM +0100, Bill Allombert wrote:
> Dear PARI developers,
> 
> We discovered that the return() function was slow with large input:
> 
> ? bnf=vector(100,i,vector(200,j,i+j));
> ? fun0(bnf)=bnf[4];
> ? fun1(bnf)=return(bnf[4]);
> ? my(bnf=bnf);for(i=1,10^5,fun0(bnf))
> ? ##
>   ***   last result computed in 264 ms.
> ? my(bnf=bnf);for(i=1,10^5,fun1(bnf))
> ? ##
>   ***   last result computed in 805 ms.
> 
> So as a first step, I have commited a patch (41dd7b6) for the internal GP compiler that
> remove 'spurious' returns (like in fun1 above).

As a second step, I have commited a patch which avoid copying the return value
if it is already a clone:

? bnf=vector(100,i,vector(200,j,i+j));
? fun5(bnf)=for(i=1,10,if(i==4,return(bnf[i])));
? fun6(bnf)=for(i=1,10,if(i==4,my(x=bnf[i]);return(x)));
? my(bnf=bnf);for(i=1,10^5,fun5(bnf))
? ##
  ***   last result computed in 1,064 ms.
? my(bnf=bnf);for(i=1,10^5,fun6(bnf))
? ##
  ***   last result computed in 1,672 ms.

Now, they take essentially the same time.

(It is possible to extend the idea further, but this require careful check that 
copy-on-write work correctly.)

Cheers,
Bill.