Page MenuHomeFeedback Tracker

Expose array Reserve and ShrinkToFit for better array management for advance uses
Closed, ResolvedPublic

Description

Any and all good dynamic arrays (std::vector, AutoArray) have commands to grow and shrink allocations. We already have resize, but resize sets the objects to nil or a default state. What we need is to allocate the memory to hold x amount and set maxCount to x. This will allow us to pushBack without having to reallocate memory every push. If I know what size I need for my array, but I don't want a default values Reserve will vastly speed up the code because of no new memory allocation.

Example:

private _array = [];
_array reverse 100;
for "_i" from 1 to 100 do 
{
    _array pushBack _i;
};
// _array is now [1..100] without having to allocate memory

Now sometimes it is nice to allocate more than needed and shrink the array to fit the size of elements.

private _array = [];
_array reverse 100;
for "_i" from 1 to 50 do 
{
    _array pushBack _i;
};
shrinkToFit _array;
// _array is [1..50] and only takes up 50 elements free the other 50 elements

Details

Severity
Feature
Resolution
Open
Reproducibility
N/A
Operating System
Windows 10 x64
Category
General

Event Timeline

killerswin2 added a comment.EditedSep 3 2023, 3:52 AM

after doing some testing, this might not be worth it.

private _array = [];
for "_i" from 1 to 10000000-1 do 
{
	_array pushBack _i;
};

Execution Time: 5234.0000 ms | Cycles: 1/10000 | Total Time: 5234 ms

private _array = [];
_array reserve 10000000-1;
for "_i" from 1 to 10000000-1 do 
{
	_array pushBack _i;
};

Execution Time: 5070.0000 ms | Cycles: 1/10000 | Total Time: 5070 ms

you would need to be in the millions of elements before you could see results. It might actually help in scheduled tho, as you would have less allocations and a little more throughput

dedmen closed this task as Resolved.Sep 19 2023, 3:01 PM
dedmen claimed this task.
dedmen added a subscriber: dedmen.

allow us to pushBack without having to reallocate memory every push

That is already not happening. Same as std::vector it overallocates.

Reserve will vastly speed up the code

SQF is quite slow. Memory allocation of resizing an array is barely noticable unless you have thousands of elements which you surely don't.