Server memory optimization
#1

Hey!

I just realized something which may save some memory in my server. I don't know though, so I'm asking you.

Currently I have this on OnGameModeInit:
pawn Код:
for(new i=0; i <= MAX_BUILDINGS; i++) {
            new query[100];
        mysql_format(mysql, query, sizeof(query), "SELECT * FROM `buildings` WHERE `ID`='%d'", i);
                mysql_tquery(mysql, query, "LoadBuilding", "i", i);
    }
Then LoadBuilding looks like this:
pawn Код:
forward LoadBuilding(building);
public LoadBuilding(building) {
    new rows, fields;
    cache_get_data(rows, fields, mysql);
    if(!rows) return 1;

    cache_get_field_content(0, "Name", BuildingInfo[building][bName], mysql, 64);

        BuildingInfo[building][bOwner] = cache_get_field_content_int(0, "Owner");
    BuildingInfo[building][bOwnedBy] = cache_get_field_content_int(0, "OwnedBy");
    BuildingInfo[building][bInterior] = cache_get_field_content_int(0, "Interior");
    BuildingInfo[building][bVirtualWorld] = cache_get_field_content_int(0, "VirtualWorld");
    BuildingInfo[building][bSale] = cache_get_field_content_int(0, "Sale");
    BuildingInfo[building][bPrice] = cache_get_field_content_int(0, "Price");
    BuildingInfo[building][bID] = cache_get_field_content_int(0, "ID");

    BuildingInfo[building][bEntreX] = cache_get_field_content_float(0, "EntreX");
    BuildingInfo[building][bEntreY] = cache_get_field_content_float(0, "EntreY");
    BuildingInfo[building][bEntreZ] = cache_get_field_content_float(0, "EntreZ");
    BuildingInfo[building][bIntX] = cache_get_field_content_float(0, "IntX");
    BuildingInfo[building][bIntY] = cache_get_field_content_float(0, "IntY");
    BuildingInfo[building][bIntZ] = cache_get_field_content_float(0, "IntZ");

    CreateBuildingPickup(building);

    return 1;
}


I realized I'm actually sending a new query for every row in the database. Would it be better to send one query and use the loop to store data in the array like this?

pawn Код:
mysql_tquery(mysql, "SELECT * FROM `buildings`", "LoadBuilding");
pawn Код:
forward LoadBuilding();
public LoadBuilding() {
    new rows, fields;
    cache_get_data(rows, fields, mysql);
    if(!rows) return 1;

    for(new i = 0; i <= rows; i++) {
    cache_get_field_content(i, "Name", BuildingInfo[building][bName], mysql, 64);

    BuildingInfo[i][bOwner] = cache_get_field_content_int(i, "Owner");
    BuildingInfo[i][bOwnedBy] = cache_get_field_content_int(i, "OwnedBy");
    BuildingInfo[i][bInterior] = cache_get_field_content_int(i, "Interior");
    BuildingInfo[i][bVirtualWorld] = cache_get_field_content_int(i, "VirtualWorld");
    BuildingInfo[i][bSale] = cache_get_field_content_int(i, "Sale");
    BuildingInfo[i][bPrice] = cache_get_field_content_int(i, "Price");
    BuildingInfo[i][bID] = cache_get_field_content_int(i, "ID");

    BuildingInfo[i][bEntreX] = cache_get_field_content_float(i, "EntreX");
    BuildingInfo[i][bEntreY] = cache_get_field_content_float(i, "EntreY");
    BuildingInfo[i][bEntreZ] = cache_get_field_content_float(i, "EntreZ");
    BuildingInfo[i][bIntX] = cache_get_field_content_float(i, "IntX");
    BuildingInfo[i][bIntY] = cache_get_field_content_float(i, "IntY");
    BuildingInfo[i][bIntZ] = cache_get_field_content_float(i, "IntZ");

    CreateBuildingPickup(i);
    }

    return 1;
}
Reply
#2

Or you could do:
PHP код:
mysql_tquery(mysql"SELECT * FROM buildings""LoadBuildings"""); 
And then in the function that you call with the query above:
PHP код:
forward LoadBuildings();
public 
LoadBuildings() {
    for(new 
0cache_get_row_count(); != j++) if(MAX_BUILDINGS) {
        
// code
    
}
    return 
true;

Reply
#3

Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
Or you could do:
PHP код:
mysql_tquery(mysql"SELECT * FROM buildings""LoadBuildings"""); 
And then in the function that you call with the query above:
PHP код:
forward LoadBuildings();
public 
LoadBuildings() {
    for(new 
0cache_get_row_count(); != j++) if(MAX_BUILDINGS) {
        
// code
    
}
    return 
true;

Could you please describe the difference of your loop and mine if you have the patience?
Reply
#4

Quote:
Originally Posted by introzen
Посмотреть сообщение
Would it be better to send one query and use the loop to store data in the array like this?
Yes. I praise you for finding this out yourself. Got a tutorial in my sig (don't know which one) that's on this topic, as well.

Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
PHP код:
    for(new 0cache_get_row_count(); != j++) if(MAX_BUILDINGS) { 
Unrelated, but pro tip: you can put multiple conditions in the loop header itself. If the condition isn't met the loop stops. Whereas with a nested if the loop keeps on running until it's done, even if there's no more space to store data. So in this case you might write:
PHP код:
for(new 0cache_get_row_count(); && MAX_BUILDINGS++) 
Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
Unrelated, but pro tip: you can put multiple conditions in the loop header itself. If the condition isn't met the loop stops. Whereas with a nested if the loop keeps on running until it's done, even if there's no more space to store data. So in this case you might write:
PHP код:
for(new 0cache_get_row_count(); && MAX_BUILDINGS++) 
Thank you, Vince. I appreciate it.

Quote:
Originally Posted by introzen
Посмотреть сообщение
Could you please describe the difference of your loop and mine if you have the patience?
Other than you saving on (a little bit of) memory by not declaring two extra variables for cache_get_data, I doubt that it matters whether you use my syntax or yours. Mine is a bit more compact.
Reply
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
Yes. I praise you for finding this out yourself. Got a tutorial in my sig (don't know which one) that's on this topic, as well.



Unrelated, but pro tip: you can put multiple conditions in the loop header itself. If the condition isn't met the loop stops. Whereas with a nested if the loop keeps on running until it's done, even if there's no more space to store data. So in this case you might write:
PHP код:
for(new 0cache_get_row_count(); && MAX_BUILDINGS++) 
Thank you! Could you explain what the difference of these conditions do and why they're important?

PHP код:
for(new 0MAX_BUILDINGS++) 
PHP код:
for(new 0cache_get_row_count(); && MAX_BUILDINGS++) 
My guess is that if I run the first loop with MAX_BUILDINGS defined 50, the loop will go around 50 times?
However if I use the second one, the loop will run for as many rows that are returned by the query, so that if the query only returns 5 rows, the loop won't go around 45 times for nothing, which also works vice-versa with
PHP код:
&& MAX_BUILDINGS
which makes the loop stop, if the query should return 200 rows and I've defined the maximum number of buildings to be 50?
Reply
#7

Neither loops are related to each other. The first loop will keep on looping and will continue to assign NULL data to the variables even after it passed the amount of rows you have because you're not taking the amount of rows in consideration.

The second loop will continue to loop for i < j AND i < MAX_BUILDINGS. You can't exceed MAX_BUILDINGS because I'm sure that your variable to access the enum is a 2D array of which the first set is limited by MAX_BUILDINGS:
PHP код:
new BuildingInfo[MAX_BUILDINGS][...]; 
So allowing your loop to go past this constant will result in array out of bounds errors.
Reply
#8

Quote:
Originally Posted by Vince
Посмотреть сообщение
Yes. I praise you for finding this out yourself. Got a tutorial in my sig (don't know which one) that's on this topic, as well.



Unrelated, but pro tip: you can put multiple conditions in the loop header itself. If the condition isn't met the loop stops. Whereas with a nested if the loop keeps on running until it's done, even if there's no more space to store data. So in this case you might write:
PHP код:
for(new 0cache_get_row_count(); && MAX_BUILDINGS++) 
Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
Neither loops are related to each other. The first loop will keep on looping and will continue to assign NULL data to the variables even after it passed the amount of rows you have because you're not taking the row amount in consideration.

The second loop will continue to loop for i < j AND i < MAX_BUILDINGS. You can't exceed MAX_BUILDINGS because I'm sure that your variable to access the enum is a 2D array of which the first set is limited by MAX_BUILDINGS:
PHP код:
new BuildingInfo[MAX_BUILDINGS][...]; 
So allowing your loop to go past this constant will result in array out of bounds errors.
Thank you for clarifying. I appreciate the gesture.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)