Karim BELABAS on Tue, 12 Dec 2000 17:06:57 +0100 (MET)


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

Re: return and the heap


[David Bremner:]
> Over the last week or so I have been plaguing the list with my
> troubles with heap usage in gp. I seem to have narrowed it down to the
> fact that return makes a copy of the result of a recursive call (or 
> something)
> 
> With the following script, the commented out version of the recursive 
> call leaves only one thing on the heap, whereas the return leaves one
> for every recursive call.  
> 
> foo(count)=
> {
> 
>  if(count<=0, return(0));
> 
> \\ foo(count-1);
>    return(foo(count-1));
> }

Indeed, there were a number of situations where return() could clutter the
heap. The following patch should clear this problem [should apply cleanly to
any version more recent than 2.0.17, in particular 2.1]

Cheers,

    Karim.

Index: src/language/anal.c
===================================================================
RCS file: /home/megrez/cvsroot/pari/src/language/anal.c,v
retrieving revision 1.45
diff -c -r1.45 anal.c
*** src/language/anal.c	2000/11/15 11:43:41	1.45
--- src/language/anal.c	2000/12/12 15:56:01
***************
*** 3239,3245 ****
--- 3239,3247 ----
  GEN
  return0(GEN x)
  {
+   GEN y = br_res;
    br_res = x? gclone(x): NULL;
+   if (y) gunclone(y);
    br_status = br_RETURN; return NULL;
  }
  
-- 
Karim Belabas                    email: Karim.Belabas@math.u-psud.fr
Dep. de Mathematiques, Bat. 425
Universite Paris-Sud             Tel: (00 33) 1 69 15 57 48
F-91405 Orsay (France)           Fax: (00 33) 1 69 15 60 19
--
PARI/GP Home Page: http://www.parigp-home.de/