Bill Allombert on Sat, 23 Oct 2021 12:33:52 +0200


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

Re: memory leak on concatenating two lists


Le Fri, Oct 22, 2021 at 09:49:48PM -0400, Max Alekseyev a écrit :
> 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.

Yes, concat does not modify its arguments.

> 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?

The only way under GP is to use listput, for example

listappend(~L,V)=for(i=1,#V,listput(~L,V[i]))

L=List();
listappend(~L,[1..10])
listappend(~L,[1..10])
L

%4 = List([1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10])

Cheers,
Bill.