Max Alekseyev on Sat, 23 Oct 2021 03:50:28 +0200


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

Re: memory leak on concatenating two lists


Thank you for fixing the leak. There is however another issue - this time it's performance.
The following code:

L = List( vector(10^7) )
L = concat( L, List([1]) )

takes a several seconds to complete, and it looks like concat() is creating a copy of L rather than just linking it with the second list. 
If this is an intended behavior, I'd like to reiterate my question about an efficient way to concatenate two lists.
That is, given two long lists, how to get a new list that links the two together without moving/copying their content in memory?

Thanks,
Max


On Tue, Oct 19, 2021 at 5:12 PM Bill Allombert <Bill.Allombert@math.u-bordeaux.fr> wrote:
Le Tue, Oct 19, 2021 at 09:46:58AM -0400, Max Alekseyev a écrit :
> The following code illustrates the problem:
>
> {
> L = List();
> while(1,
>    my( temp = List(vector(100,i,i)) );
>    L = concat(L,temp);
>    for(i=1,100, listpop(L));
>    print1(getheap(),"    ",Strchr(13));
> );
> }
>
> While list L remains empty at the end of each while-loop iteration, the
> memory consumption grows indefinitely.
> If I replace "L = concat(L,temp);" with, say,
> for(i=1,#temp, listput(L,temp[i]));
> then memory consumption remains constant. However, this does not look like
> an efficient way to concatenate lists.
> Can concat() be fixed, or what would be an efficient and memory-safe
> approach here?

This should be fixed in master (d43896622).
You can work around it by doing
  L = concat(L,Vec(temp));
or similar ways to avoid concatening two lists.

Thanks for detecting this issue!
Bill