In need of dynamic array - Best solution?
#1

I am creating a dialog with all online faction members. However, the string that contains all the text could need a lot of cells, depending on the amount of members. I would rather create a dynamic array than using a big string size.

However, as far as I know, creating a dynamically sized array is impossible in PAWN (other than with malloc, which I'd rather not fuck around with).

How would you solve this problem (keeping optimization and performance in mind)?
Reply
#2

I know from experience that dialogs only show up to 50 lines.
I don't exactly know the maximum width you can display on a single line, but I think I read once that 3500 characters would be the maximum limit for dialogs.

Just create a global array (new LargeDialog[3000] in your script and use that to form your big dialogs, make the size 3000 to be safe.

Each cell is 4 bytes large (each index can hold a 32-bit integer value), so that array is only 12 kilobytes in size.
Since pawn doesn't support dynamic arrays and you can't adjust the size of them during runtime, wasting 12 kilobytes of RAM shouldn't be an issue these days.
Computers have alot of memory since they went 64-bit, and servers have even more memory.
My home-pc has 12 gigabytes of RAM.
When you make the calculation, you'll see that such an array consumes about 1/10.000th of a percentage, or 0.0001% of my pc's total memory. You can have 1000 of those arrays and consume "only" 12 megabytes.
So don't worry about performance or optimizing it.

If you're concerned about performance, re-allocating arrays (or resizing them during runtime) has a bigger performance-hit compared to just creating a large enough array to begin with.
Some languages support resizing arrays during runtime and even keep the data inside them, unless the array is completely filled and you make it smaller, then the data in the excess indices will be dropped.

They do this by re-creating a new array of your desired size, then they copy the data and delete the original array.
Doing this frequently with large arrays has a much bigger impact on performance compared to creating an array large enough to hold all your expected data and never resize it.
Reply
#3

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
I know from experience that dialogs only show up to 50 lines.
I don't exactly know the maximum width you can display on a single line, but I think I read once that 3500 characters would be the maximum limit for dialogs.

Just create a global array (new LargeDialog[3000] in your script and use that to form your big dialogs, make the size 3000 to be safe.

Each cell is 4 bytes large (each index can hold a 32-bit integer value), so that array is only 12 kilobytes in size.
Since pawn doesn't support dynamic arrays and you can't adjust the size of them during runtime, wasting 12 kilobytes of RAM shouldn't be an issue these days.
Computers have alot of memory since they went 64-bit, and servers have even more memory.
My home-pc has 12 gigabytes of RAM.
When you make the calculation, you'll see that such an array consumes about 1/10.000th of a percentage, or 0.0001% of my pc's total memory. You can have 1000 of those arrays and consume "only" 12 megabytes.
So don't worry about performance or optimizing it.

If you're concerned about performance, re-allocating arrays (or resizing them during runtime) has a bigger performance-hit compared to just creating a large enough array to begin with.
Some languages support resizing arrays during runtime and even keep the data inside them, unless the array is completely filled and you make it smaller, then the data in the excess indices will be dropped.

They do this by re-creating a new array of your desired size, then they copy the data and delete the original array.
Doing this frequently with large arrays has a much bigger impact on performance compared to creating an array large enough to hold all your expected data and never resize it.
Yeah, I'm used to other programming languages where I can just calculate an array size and use it. The way you use is currently what I'm doing too, however I was wondering if there was a hacky other way

32 GB ram here, but I just like to over-optimize I guess
Reply
#4

None that I know off.
Maybe someone wrote a plugin for such issues, but I would just go for a big-enough array to begin with and keep it that way.

PAWN is a pretty static language, we'll all have to deal with this.

I would rather use a language that can handle dynamic arrays and use real objects and OOP programming instead of a static language, but we can't choose this unfortunately.

I'm also looking for ways to make my own script as flexible as possible, but sometimes I'll have to accept these issues as well.
My script needs to be able to add gas-stations, police-stations, speedcams, routes, locations, cargotypes, houses, businesses, and ALOT more during runtime.
Since arrays can't be resized during runtime, I'll just have to create large-enough arrays, and they consume more memory than a simple string of size 3000.

My data may become so large that 1000 indices is too small to hold it all, and even when I allow admins to add new data during runtime.
Reply
#5

You could always look into plugins vectoral pawn specifically.

https://sampforum.blast.hk/showthread.php?tid=364285
Reply
#6

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
y went 64-bit, and servers have even more memory.
My home-pc has 12 gigabytes of RAM.
When you make the calculation, you'll see that such an array consumes about 1/10.000th of a percentage, or 0.0001% of my pc's total memory. You can have 1000 of those arrays and consume "only" 12 megabytes.
So don't worry about performance or optimizing it.
This is completely irrelevant since actual live SA-MP servers usually aren't run on extremely powerful boxes. My VPS is doing just fine with 256mb of RAM, but suddenly that "only 12 megabytes" becomes 4%.
Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
This is completely irrelevant since actual live SA-MP servers usually aren't run on extremely powerful boxes. My VPS is doing just fine with 256mb of RAM, but suddenly that "only 12 megabytes" becomes 4%.
In such cases, even with 1000 of those arrays won't be a problem.
Using only 1 array of 12 kilobytes to form large dialogs isn't a problem, that would be 0.004% of your server's RAM.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)