I'm not sure if this is a bug or if I'm just talking bullshit so I'd like to discuss it here first before I'll report it as a bug. Sorry if it bothers anyone; just tell me and I'll wipe the thread.
Arrays seem to allocate unnecessary amount of memory. The syntax for creating an array is this:
ArrayCreate(cellsize=1, reserved=32). The problem is with the
reserved value, which is basically the amount of elements that are pre-allocated to prevent the array's resizing all the time. However, if this value is <= 8, it's not taken into account at all because it seems the minimum value is 8 from this code:
https://github.com/alliedmodders/amx...astructs.h#L53
This would be fine, however the array always gets initialized with cursize=0:
https://github.com/alliedmodders/amx...astructs.h#L75
So if I want to create a dynamic array that I'm sure will have X elements and no higher, where X is for example two, that's 4x more memory allocated than necessary.
The solution to this should be simply initializing the array with cursize=1.
Again, I'm not sure if this is correct, so please discuss ^_^
Test code:
PHP Code:
#define RESERVED_MEMORY X
#include < amxmodx >
public plugin_init( )
{
register_concmd( "getmemory", "_getmemory" )
}
public _getmemory( )
{
new Array: iArray
for( new i; i < 100000; i ++ )
{
iArray = ArrayCreate( 32, RESERVED_MEMORY )
ArrayPushCell( iArray, 12345678 )
ArrayPushString( iArray, "some string" )
ArrayPushCell( iArray, 12345678 )
ArrayPushString( iArray, "some string" )
ArrayPushCell( iArray, 12345678 )
ArrayPushString( iArray, "some string" )
ArrayPushCell( iArray, 12345678 )
ArrayPushString( iArray, "some string" )
}
}
With X=1 and 8, the memory usage was 120MB. With X=9, the memory usage was a bit higher, like 130-140MB. With pushing more than 8 elements into the array with X<=8, the memory usage doubled right away to 240MB which is caused by this code:
https://github.com/alliedmodders/amx...astructs.h#L59
That could also be changed as it's still a waste in most cases.