• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Optimizing Looping All Streamer Objects
#1
Hi!?

It's my first time posting here but I would like to know is there any possibilities that I can optimize this code? It causes a huge lag spike in the server when used in a live server. It occurs when this loop is called when removing a furniture/land object or removing the entire furniture/land object.?(This also happens when the script has to reload the furniture/land object)

The lag can also be noticed?when deleting a house (which removes all the furniture in the process)

Things to know; My server has 16,000 objects so it is no surprise that the loop will check each single objects in the server.

I will be showing the functions (codes) that calls the loop:

PHP Code:
RemoveFurniture(objectid)
{
? ? if(
IsValidDynamicObject(objectid) && Streamer_GetExtraInt(objectidE_OBJECT_TYPE) == E_OBJECT_FURNITURE)
{
new
? ? ? ? 
id Streamer_GetExtraInt(objectidE_OBJECT_INDEX_ID);

? ? 
DeleteFurnitureObject(objectid);

? ? 
mysql_format(mysql_connectionqueryBuffersizeof(queryBuffer), "DELETE FROM furniture WHERE id = %i"id);
? ? 
mysql_tquery(mysql_connectionqueryBuffer);
}
}

DeleteFurnitureObject(objectid)
{
if(
IsValidDynamicObject(objectid) && Streamer_GetExtraInt(objectidE_OBJECT_TYPE) == E_OBJECT_FURNITURE)
{
? ? new 
Text3D:textid Text3D:Streamer_GetExtraInt(objectidE_OBJECT_3DTEXT_ID);

? ? ? ? if(
IsValidDynamic3DTextLabel(textid))
? ? ? ? {
? ? ? ? ? ? 
DestroyDynamic3DTextLabel(textid);
? ? ? ? }

? ? ? ? 
DestroyDynamicObject(objectid);
}
}

RemoveAllFurniture(houseid)
{
? ? if(
HouseInfo[houseid][hID] > 0)
{
? ? for(new 
0<= Streamer_GetUpperBound(STREAMER_TYPE_OBJECT); )
? ? {
? ? ? ? if(
IsValidDynamicObject(i) && Streamer_GetExtraInt(iE_OBJECT_TYPE) == E_OBJECT_FURNITURE && Streamer_GetExtraInt(iE_OBJECT_EXTRA_ID) == HouseInfo[houseid][hID])
? ? ? ? {
? ? ? ? ? ? 
DeleteFurnitureObject(i);
}
}

mysql_format(mysql_connectionqueryBuffersizeof(queryBuffer), "DELETE FROM furniture WHERE houseid = %i"HouseInfo[houseid][hID]);
mysql_tquery(mysql_connectionqueryBuffer);
}
}

ReloadFurniture(objectidlabels)
{
if(
IsValidDynamicObject(objectid) && Streamer_GetExtraInt(objectidE_OBJECT_TYPE) == E_OBJECT_FURNITURE)
{
? ? new
? ? ? ? 
id Streamer_GetExtraInt(objectidE_OBJECT_INDEX_ID);

? ? 
DeleteFurnitureObject(objectid);

? ? 
mysql_format(mysql_connectionqueryBuffersizeof(queryBuffer), "SELECT * FROM furniture WHERE id = %i"id);
? ? 
mysql_tquery(mysql_connectionqueryBuffer"SQL_LoadFurnitures""i"labels);
}
}

ReloadAllFurniture(houseid)
{
? ? if(
HouseInfo[houseid][hID] > 0)
{
? ? for(new 
0<= Streamer_GetUpperBound(STREAMER_TYPE_OBJECT); )
? ? {
? ? ? ? if(
IsValidDynamicObject(i) && Streamer_GetExtraInt(iE_OBJECT_TYPE) == E_OBJECT_FURNITURE && Streamer_GetExtraInt(iE_OBJECT_EXTRA_ID) == HouseInfo[houseid][hID])
? ? ? ? {
? ? ? ? ? ? 
DeleteFurnitureObject(i);
}
}

mysql_format(mysql_connectionqueryBuffersizeof(queryBuffer), "SELECT * FROM furniture WHERE houseid = %i"HouseInfo[houseid][hID]);
mysql_tquery(mysql_connectionqueryBuffer"SQL_LoadFurnitures""i"HouseInfo[houseid][hLabels]);
}


I will be editing this thread if more codes are needed.
  Reply
#2
Every time a house is removed, its looping thru every object in your server just to check if its furniture or not. That causes the lag because pawn is unthreaded, meaning the server waits for the loop to get finished to continue its operation. So imagine waiting for a loop to go thru 16,000 objects.

Use y_iterate. If you dont know how to theres a documentation somewhere. If you dont want to use iterate, use arrays. Everytime a house adds a furniture, assign the objectid of that furniture to that array.
Away
  Reply
#3
(2020-10-01, 10:19 AM)justinnn Wrote: Every time a house is removed, its looping thru every object in your server just to check if its furniture or not. That causes the lag because pawn is unthreaded, meaning the server waits for the loop to get finished to continue its operation. So imagine waiting for a loop to go thru 16,000 objects.



Use y_iterate. If you dont know how to theres a documentation somewhere. If you dont want to use iterate, use arrays. Everytime a house adds a furniture, assign the objectid of that furniture to that array.



I've been planning to assign the furniture/land objects into an array but the problem is it will limit the furnitures/land objects that it can make since the higher the limit for the array, the higher the memory is being used in the script and I don't want that.



I didn't know that y_iterate can be used on looping through objects, may I know how I can convert this code into y_iterate??



Thanks!
  Reply
#4
All information about y_interate (which is y_foreach) can be found on the Github page: https://github.com/pawn-lang/YSI-Include...iterate.md
  Reply


Forum Jump: