Is the string being copied?

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Is the string being copied?

Post 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()
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Is the string being copied?

Post 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 ...
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Is the string being copied?

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Is the string being copied?

Post 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).
Cyllceaux
Enthusiast
Enthusiast
Posts: 514
Joined: Mon Jun 23, 2014 1:18 pm

Re: Is the string being copied?

Post by Cyllceaux »

I know it is stupid, but is there a chance to deactivate this buffer? or cleaning it?
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Is the string being copied?

Post by Fred »

No, it's how it was designed.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Is the string being copied?

Post 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?
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Is the string being copied?

Post 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.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Is the string being copied?

Post 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.
Post Reply