Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] Optimizing Looping All Streamer Objects

 
  • 0 Vote(s) - 0 Average
Pawn Optimizing Looping All Streamer Objects
JaKe Elite
Offline

Burgershot Member
Posts: 13
Threads: 7
Joined: Apr 2019
Reputation: 0
#1
2020-09-30, 10:12 PM (This post was last modified: 2020-09-30, 10:15 PM by JaKe Elite.)
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(objectid, E_OBJECT_TYPE) == E_OBJECT_FURNITURE)
{
new
        id = Streamer_GetExtraInt(objectid, E_OBJECT_INDEX_ID);

    DeleteFurnitureObject(objectid);

    mysql_format(mysql_connection, queryBuffer, sizeof(queryBuffer), "DELETE FROM furniture WHERE id = %i", id);
    mysql_tquery(mysql_connection, queryBuffer);
}
}

DeleteFurnitureObject(objectid)
{
if(
IsValidDynamicObject(objectid) && Streamer_GetExtraInt(objectid, E_OBJECT_TYPE) == E_OBJECT_FURNITURE)
{
    new Text3D:textid = Text3D:Streamer_GetExtraInt(objectid, E_OBJECT_3DTEXT_ID);

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

        DestroyDynamicObject(objectid);
}
}

RemoveAllFurniture(houseid)
{
    if(HouseInfo[houseid][hID] > 0)
{
    for(new i = 0; i <= Streamer_GetUpperBound(STREAMER_TYPE_OBJECT); i ++)
    {
        if(IsValidDynamicObject(i) && Streamer_GetExtraInt(i, E_OBJECT_TYPE) == E_OBJECT_FURNITURE && Streamer_GetExtraInt(i, E_OBJECT_EXTRA_ID) == HouseInfo[houseid][hID])
        {
            DeleteFurnitureObject(i);
}
}

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

ReloadFurniture(objectid, labels)
{
if(
IsValidDynamicObject(objectid) && Streamer_GetExtraInt(objectid, E_OBJECT_TYPE) == E_OBJECT_FURNITURE)
{
    new
        id = Streamer_GetExtraInt(objectid, E_OBJECT_INDEX_ID);

    DeleteFurnitureObject(objectid);

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

ReloadAllFurniture(houseid)
{
    if(HouseInfo[houseid][hID] > 0)
{
    for(new i = 0; i <= Streamer_GetUpperBound(STREAMER_TYPE_OBJECT); i ++)
    {
        if(IsValidDynamicObject(i) && Streamer_GetExtraInt(i, E_OBJECT_TYPE) == E_OBJECT_FURNITURE && Streamer_GetExtraInt(i, E_OBJECT_EXTRA_ID) == HouseInfo[houseid][hID])
        {
            DeleteFurnitureObject(i);
}
}

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

I will be editing this thread if more codes are needed.
justinnn Away

Burgershot Member
Posts: 3
Threads: 1
Joined: Jul 2020
Reputation: 0
Location: Manila
#2
2020-10-01, 10:19 AM (This post was last modified: 2020-10-01, 10:21 AM by justinnn.)
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.
JaKe Elite
Offline

Burgershot Member
Posts: 13
Threads: 7
Joined: Apr 2019
Reputation: 0
#3
2020-10-01, 05:29 PM
(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!
Kwarde
Offline

Burgershot Member
Posts: 99
Threads: 2
Joined: Sep 2020
Reputation: 8
Location: The Netherlands
#4
2020-10-02, 08:54 AM
All information about y_interate (which is y_foreach) can be found on the Github page: https://github.com/pawn-lang/YSI-Includes/blob/5.x/YSI_Data/y_iterate.md
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode