Karim Belabas on Tue, 04 Jul 2017 14:04:30 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: gcopy_avma() is wasting stack memory compared to gcopy() |
* Jens Schmidt [2017-07-04 13:01]: > Thanks for your explanation. > > I'm using gcopy_avma() outside threads in some situations where it's > very effective and works fine. Appended is an example that computes > vampire numbers. > > The only difficult is to ensure that there is no memory overlapping when > copying stack objects. During analyzation of repiled stack I noticed > increased consumption with gcopy_avma() and non-recusive types. [...] > for (i = 1; i < lg(D); i++) { /* examine divisors of n */ > p = gel(D, i); > q = divii(n, p); Aside 1: p = gel(D, i); q = gel(D, lg(D)-i); is less expensive (and guaranteed to work). > F = cgetg(3, t_VEC); /* write pair of fangs p,q */ > gel(F, 1) = p; > gel(F, 2) = q; > > gel(R, z++) = F; /* append fangs to result vector */ Aside2 : this can be streamlined as gel(R, z++) = mkvec2(p,q); /* append fangs to result vector */ [...] > R = gcopy_avma(R, <op); /* copy result to top of stack (no memory overlapping) */ This is dangerous: parts of the initial R can be overwritten by gcopy_avma [ it may or may not be correct in your situation, this requires a thorough check... ] > > avma = ltop; /* garbage collection */ > > return R; The standard way to deal with this situation is to replace the above three lines by return gerepilecopy(ltop, R); (which is a little less effective but much less error prone). The timing difference should be negligible. Cheers, K.B. -- Karim Belabas, IMB (UMR 5251) Tel: (+33) (0)5 40 00 26 17 Universite de Bordeaux Fax: (+33) (0)5 40 00 21 23 351, cours de la Liberation http://www.math.u-bordeaux.fr/~kbelabas/ F-33405 Talence (France) http://pari.math.u-bordeaux.fr/ [PARI/GP] `