Page 1 of 1

Is the string being copied?

Posted: Sat Jun 22, 2024 3:07 am
by AZJIO
Do I then get an intermediate state in which I have two copies of the 200 MB string? Do I have to worry about passing a Result.String string to the function so that I don't have to copy the string when the result is returned, thereby limiting the maximum string size due to memory constraints?

Code: Select all

Procedure.s MyProcedure()
	Protected Result.s
	Result = Space(100000000)
	ProcedureReturn Result
EndProcedure

Result2 = MyProcedure()

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 9:28 am
by STARGÅTE
You have anyway at least two copies of the large string.
Space() needs 200 MB and when it is assigned to Result it needs another 200 MB of space.
And when I'm right, Space() do not release its memory to be faster when calling it again.

So during the return of the string from procedure you need 600 MB ...

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 11:40 am
by mk-soft
String passed ByRef. However, this only works with structured strings, as you have to pass the pointer to the pointer of the string

Code: Select all

Procedure MyProcedure(*pString.String)
	*pString\s = Space(100000000)
EndProcedure

pText.string

MyProcedure(@pText)
Debug StringByteLength(pText\s)

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 11:56 am
by Fred
PB uses a temp buffer where all string ops goes, so basically you need 3 times the space in this case (original string -> copied to temp buffer -> copied to new allocated buffer).

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 3:15 pm
by Cyllceaux
I know it is stupid, but is there a chance to deactivate this buffer? or cleaning it?

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 5:05 pm
by Fred
No, it's how it was designed.

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 6:23 pm
by PBJim
Fred wrote: Sat Jun 22, 2024 5:05 pm No, it's how it was designed.
The temp buffer that you mention, presumably is returned to memory when a procedure ends? If not, is the buffer per-thread?

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 7:05 pm
by Fred
Yes it's per thread, it get reduced automatically if the next operation doesn't need that much memory when the buffer is big.

Re: Is the string being copied?

Posted: Sat Jun 22, 2024 7:28 pm
by PBJim
Fred wrote: Sat Jun 22, 2024 7:05 pm Yes it's per thread, it get reduced automatically if the next operation doesn't need that much memory when the buffer is big.
It's good to know, as developers, something of how work is performed internally, as it can affect the way in which we approach solutions — thanks for that. Likewise to the OP for raising it too.