What's the proper use of ShrinkToFit() for dyn arrays?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Fishytza
Posts: 781
Joined: Wed Feb 23, 2011 11:04 am
Preferred Pronouns: No Preference
Contact:

What's the proper use of ShrinkToFit() for dyn arrays?

Post by Fishytza »

...Or rather, should I use it at all?

Right now it looks like this:

For adding stuff

Code: Select all

		array.Push(thing);
		array.ShrinkToFit();
For removing stuff

Code: Select all

		if( array.Size() )
		{
			uint todelete = array.Find(thing);
			if( todelete < array.Size() )
			{
				array.Delete(todelete);
				array.ShrinkToFit();
			}
		}
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: What's the proper use of ShrinkToFit() for dyn arrays?

Post by _mental_ »

In common case you shouldn't use it, apparently not after each array update.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: What's the proper use of ShrinkToFit() for dyn arrays?

Post by Graf Zahl »

The only use for it is if you have dynamically created a larger set of data, and once you know that you won't need to reallocate it any further, shrink it to the final size.
This function will always cause a reallocation and copy of contents so only use it when you are 100% certain that the array won't change its size anymore. Otherwise it can easily destroy performance.
User avatar
Fishytza
Posts: 781
Joined: Wed Feb 23, 2011 11:04 am
Preferred Pronouns: No Preference
Contact:

Re: What's the proper use of ShrinkToFit() for dyn arrays?

Post by Fishytza »

Graf Zahl wrote:...only use it when you are 100% certain that the array won't change its size anymore. Otherwise it can easily destroy performance.
Does this include not changing for the rest of the game session? Because, as it is right now, that piece of code up there is used by a event handler. Specifically, the first part is in WorldThingSpawned(), the second part is in WorldThingDestroyed().
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: What's the proper use of ShrinkToFit() for dyn arrays?

Post by Graf Zahl »

If you know that the array will change in the future, do not use ShrinkToFit. Repeatedly calling it is very inefficient. This is for stuff where you parse a definition file, add element by element to the array, and when you are done want to free all the empty reserved elements at the end. And that's pretty much the ONLY reasonable use case for it.
User avatar
Fishytza
Posts: 781
Joined: Wed Feb 23, 2011 11:04 am
Preferred Pronouns: No Preference
Contact:

Re: What's the proper use of ShrinkToFit() for dyn arrays?

Post by Fishytza »

Understood.
Post Reply

Return to “Scripting”