[Gambas-user] Is the &= efficient?

Benoît Minisini g4mba5 at gmail.com
Tue Mar 5 21:26:15 CET 2019


Le 05/03/2019 à 21:21, Cedron Dawg a écrit :
> It has been mentioned in the past that Gambas doesn't have garbage collection, that objects are allocated in place.  In regular BASICS, and Java, etc, when a string append is performed, space for the new string is created, the old string copied, and the new string copied at the end.
> 
> So the question is, if one is appending a whole block of strings like this:
> 
>          x &= "long string constant"
>          x &= "long again " & maybewith & "some replacement termst"
>          .
>          .
>          .
>               
> Is it better to allocate a memory area and print to the memory instead?
> 
> Syntactically, I think the former is cleaner looking, but if there is a performance hit, that is not worth it.  The two variations are easily interchangeable via search and replace, so is not something I need to know immediately, but I'd like to know which is better.
> 
> Not worth a deep dive into the code yet though.
> 
> Thanks,
> Ced
> 
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 

&= is just syntactic sugar. A &= B is the same thing as A = A & B, and 
so using &= for adding a string to a buffer does a lot of useless 
allocations.

Two workarounds:

1) Using a second string as an intermediate buffer.

2) Using OPEN STRING instruction. It implements a string buffer, and so 
does less allocations.

Regards,

-- 
Benoît Minisini


More information about the User mailing list