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

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 a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: What's the proper use of ShrinkToFit() for dyn arrays?

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

by Fishytza » Thu May 17, 2018 5:30 am

Understood.

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

by Graf Zahl » Thu May 17, 2018 4: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.

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

by Fishytza » Thu May 17, 2018 4: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().

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

by Graf Zahl » Thu May 17, 2018 3: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.

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

by _mental_ » Thu May 17, 2018 2:51 am

In common case you shouldn't use it, apparently not after each array update.

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

by Fishytza » Wed May 16, 2018 12:28 pm

...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();
			}
		}

Top