Raised This Month: $51 Target: $400
 12% 

Some way to optimise this...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Matheus28
Senior Member
Join Date: Aug 2009
Old 12-24-2009 , 15:36   Some way to optimise this...
Reply With Quote #1

This part of my plugin is taking 5 seconds to compile:
PHP Code:
#define MAX_MAPS 128
#define MAX_MAPNAME 64
#define MAX_MAPLEVELS 40
#define MAX_LEVELNAME 64
#define MAX_ROUNDENTNAME 64
#define MAX_LEVELBOTS 100
#define MAX_BOTNAME 64

#define NUM_SURVIVORS 4
#define MIN_BOTS_SLOTS 6

new String:maps[MAX_MAPS][MAX_MAPNAME];
new 
maps_levels_num[MAX_MAPS];
new 
String:maps_levels_name[MAX_MAPS][MAX_MAPLEVELS][MAX_LEVELNAME];
new 
String:maps_levels_description[MAX_MAPS][MAX_MAPLEVELS][MAX_LEVELNAME];
new 
maps_levels_type[MAX_MAPS][MAX_MAPLEVELS];
new 
String:maps_levels_round_ent[MAX_MAPS][MAX_MAPLEVELS][MAX_ROUNDENTNAME];
new 
String:maps_levels_bots[MAX_MAPS][MAX_MAPLEVELS][MAX_LEVELBOTS][MAX_BOTNAME]; 
There is some way of optimise this code?
Matheus28 is offline
jackpf
Senior Member
Join Date: Dec 2009
Location: Uhm...
Old 12-24-2009 , 15:44   Re: Some way to optimise this...
Reply With Quote #2

Quote:
Originally Posted by Matheus28 View Post
This part of my plugin is taking 5 seconds to compile:
And? :/

Is the actual plugin lagging?
jackpf is offline
Dragonshadow
BANNED
Join Date: Jun 2008
Old 12-24-2009 , 15:46   Re: Some way to optimise this...
Reply With Quote #3

Quote:
Originally Posted by jackpf View Post
And? :/

Is the actual plugin lagging?
this.

Compile time doesn't matter as long as the plugin doesn't lag
Dragonshadow is offline
Matheus28
Senior Member
Join Date: Aug 2009
Old 12-24-2009 , 15:48   Re: Some way to optimise this...
Reply With Quote #4

Ok, I was just scared with that compile time
Matheus28 is offline
Kevin_b_er
SourceMod Donor
Join Date: Feb 2009
Old 12-24-2009 , 16:07   Re: Some way to optimise this...
Reply With Quote #5

4 MB of static data in a bunch of massive multidimensional arrays? Might take it a moment to work.
Kevin_b_er is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 12-25-2009 , 15:39   Re: Some way to optimise this...
Reply With Quote #6

Whoa that's a lot of memory. There's gotta be some way to cut down on mem usage. But you would need to recode everything. Servers have plenty of memory these days, you should be fine.
__________________
Greyscale is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 12-25-2009 , 15:46   Re: Some way to optimise this...
Reply With Quote #7

The arrays are actually 33 764 480 elements total. I'm pretty sure there's a more effective way to store this. What is the plugin actually supposed to do? Maybe you need another storage method.

If it's just getting spesific stuff at certain indexes it's no big deal. But if you start looping through them all you might notice lag depending on what you do in each loop iteration.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)

Last edited by rhelgeby; 12-25-2009 at 15:54.
rhelgeby is offline
Send a message via MSN to rhelgeby
Theme97
Senior Member
Join Date: Mar 2009
Old 12-25-2009 , 20:23   Re: Some way to optimise this...
Reply With Quote #8

Dynamic arrays or keyvalues?
__________________
I now have very little time to work on stuff and my main computer (which is the only one that can run TF2) is not cooperating with me, so don't expect many updates from me anytime soon.

[ALL] Karaoke
[TF2] Intel Timer | Crabmod | Randomizer | Stopwatch | Crits 4 All
Theme97 is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 12-26-2009 , 01:00   Re: Some way to optimise this...
Reply With Quote #9

None of them, they are too slow for such big arrays. Keyvalues can be used to read/save to file, but but not as a internal storage method. Pure arrays is the fastest method.

What I mean about storage method is the array structure. There's a lot of strings which may take some time to parse when one of the arrays are 30 million elements big.

I think space can be saved by making separate string tables (arrays), so each map name and bot name will get their unique ID (index in array).

Like this one:
PHP Code:
new String:maps_levels_bots[MAX_MAPS][MAX_MAPLEVELS][MAX_LEVELBOTS][MAX_BOTNAME]; 
It has 32 768 000 elements total. Lets remove that that last dimension (64 byte string space). Also notice that I removed the string tag, we're only storing numbers now:
PHP Code:
new maps_levels_bots[MAX_MAPS][MAX_MAPLEVELS][MAX_LEVELBOTS]; 
The new array is now only 512 000. That's a lot of space saved (64 times smaller)! Let the last dimension be a reference (index) to a string in another table; the bot string table:
PHP Code:
new String:bot_names[SOME_NAME_LIMIT][MAX_BOTNAME]; 
Now the big maps_levels_bots will only contain numbers which is references to strings. Space will not be wasted on short or repeated strings.

Even if you make a limit of 100 bot names, that bot_names array will only be 6 400 elements.

Have a look at the array sizes:
Old size was 32 768 000 elements. By putting the strings in a separate table that reduces the size 64 times.

The new size is now only 518 400 elements. That saved you ~30 million elements by just changing data structure.

If you're going to have a unique bot name per bot level, per level, per map, the size will still be ~30 million elements total for the array and string table array. But if you're going to reuse bot names you will save a lot of space because we eliminate the repeated unused space.

In general, avoid using more than three dimensions in an array. It will easily grow, especially when we put 64 byte strings in the fourth dimension. Just multiply each dimension with eachother, and if number of elements pass one million you should rethink the array structure.

Off topic:
I'm not sure about this, but I think a cell is 32 bit, which is 4 bytes. What about a one-dimensional array with 30 million elements (not string tagged or other tags, just cells), will that one use 120 MB of memory? I know that strings are optimized (8 bit per element?).
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)

Last edited by rhelgeby; 12-26-2009 at 01:16.
rhelgeby is offline
Send a message via MSN to rhelgeby
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:49.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode