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

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
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.

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

Postby FishyClockwork » Wed May 16, 2018 1:28 pm

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

Right now it looks like this:

For adding stuff
Code: Select allExpand view
      array.Push(thing);
      array.ShrinkToFit();

For removing stuff
Code: Select allExpand view
      if( array.Size() )
      {
         uint todelete = array.Find(thing);
         if( todelete < array.Size() )
         {
            array.Delete(todelete);
            array.ShrinkToFit();
         }
      }
User avatar
FishyClockwork
-... .-.. ..- -...
 
Joined: 23 Feb 2011

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

Postby _mental_ » Thu May 17, 2018 3:51 am

In common case you shouldn't use it, apparently not after each array update.
_mental_
 
 
 
Joined: 07 Aug 2011

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

Postby Graf Zahl » Thu May 17, 2018 4:19 am

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
Graf Zahl
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany

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

Postby FishyClockwork » Thu May 17, 2018 5:07 am

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
FishyClockwork
-... .-.. ..- -...
 
Joined: 23 Feb 2011

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

Postby Graf Zahl » Thu May 17, 2018 5:35 am

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
Graf Zahl
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany

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

Postby FishyClockwork » Thu May 17, 2018 6:30 am

Understood.
User avatar
FishyClockwork
-... .-.. ..- -...
 
Joined: 23 Feb 2011


Return to Scripting

Who is online

Users browsing this forum: No registered users and 1 guest