Karim.Belabas on Fri, 4 May 2001 11:50:02 +0200 (MET DST)

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

Re: Polynomials and garbage

On Fri, 4 May 2001, Mark Chimley wrote:
> Many thanks for all the help I have received so far. Here is another small
> problem: How do you garbage collect with polynomials? My polynomial, H, has
> H pointing at the stack, H[1] and H[2] point somewhere else (one is
> definitely at the heap, probably because the first coefficient is gun), and
> H[3] points at the stack. As these are quite clearly disconnected, how can
> gerepile or gerepileupto be used on H? I have tried gerepileupto and H is
> not updated, i.e. it stays in the garbage zone.

I assume you mean H[2], H[3], H[4]  (since for type t_POL, H[1] is a codeword
and not a coefficient). The ordinary way to deal with such situations is:

  ulong av = avma;

  return gerepileupto(av, gcopy(H));

the trick being that gcopy() returns a connected object which is obviously
the last object created on the stack, hence eligible for gerepileupto.

It is possible to be more efficient [no point in copying then updating gun],
but it's messy and error-prone. I really don't recommend it. 

One such way would be to clone the relevant components out of the stack,
garbage-collect using a trivial avma = av, then recreate a t_POL, forcecopy
the saved components from heap to stack, then delete (gunclone) the clones.
This is automated in the gerepilemany() procedure, but gerepilemany is only
convenient for distinct objects, not really for distinct components of an
object which is itself part of the garbage hence needs to be updated.

Again the initial solution is much safer and about as efficient (unless you
need to clean up a single component out of 1000...)


P.S: As a rule, in libpari programming, garbage collection (copies, clones,
gerepiles, etc.) uses 0.5% to 1% of computing time (if not, you are doing
something wrong, or working with too small a stack). There is no real point
in over-optimizing...

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/