Hello There, Guest! Login or Register


LVP 2.93 - What needs to happen?
#3
[b]Before:[/b]

Code:
public CGang__ReloadGangData()
{
    new query[128], result[128];
    for(new i; i < MAX_GANGS; i++)
    {
        format(query, sizeof(query), "SELECT gang_name FROM gangs WHERE gang_id = %d", i);
        mysql_query(query);
        mysql_free_result();
        mysql_store_result();
       
        if(mysql_num_rows() > 0)
        {
            mysql_fetch_row_format(result);

            format(GangData[i][g_Name], 128, "%s", result);

            format(query, sizeof(query), "SELECT gang_color FROM gangs WHERE gang_id = %d", i);
            mysql_query(query);
            mysql_free_result();
            mysql_store_result();
            mysql_fetch_row_format(result);

            GangData[i][g_Color] = strval(result);

            #if GANG_HANDLER_DEBUG == 1
                printf("[Gang Handler Debug] Reloaded Gangdata (Gang ID: %d | Gang Name: %s | Gang Color: %d", i, GangData[i][g_Name], GangData[i][g_Color]);
            #endif
        }
    }
    
    return true;
}

* Performing 400 queries.
* Called in the initialization function which currently does another 300, totaling 700 queries on initialization.
* Called in 3 other gang commands.

[b]After:[/b]

Code:
public CGang__ReloadGangData( iPhase )
{
    // Threading - first phase.
    if(iPhase == 0)
    {
        mysql_query_callback(iPhase+1, "SELECT (gang_name, gang_color) FROM gangs", "CGang__ReloadGangData");
        // Query sent, now we wait until its finished before proceeding.
        return;
    }
   
    // query finished - continue

    mysql_store_result();
   
    // Any results?
    if(mysql_num_rows() == 0)
    {
        mysql_free_result();
        return;
    }
   
    new
        result[256],
        iGangID;

    // Alright, we loop through every result found and fetch the data.
    while(mysql_fetch_row_format(result))
    {
        // Quick bit of error checking - shouldn't be necessary though
        if(iGangID >= MAX_GANGS)
        {
            break;
        }

        // Format the gang name
        if(mysql_fetch_field_row(result, "gang_name"))
        {
            format(GangData[iGangID][g_Name], 128, "%s", result);
        }
        // don't forget the colour
        if(mysql_fetch_field_row(result, "gang_color"))
        {
            GangData[iGangID][g_Color] = strval(result);
        }

        // Increment the gang id and we're done!
        iGangID++;

        #if GANG_HANDLER_DEBUG == 1
            printf("[Gang Handler Debug] Reloaded Gang Data (Gang ID: %d | Gang Name: %s | Gang Color: %d", iGangID, GangData[iGangID][g_Name], GangData[iGangID][g_Color]);
        #endif

    }

    // All done!
    mysql_free_result();
}

* Performs 1, threaded query
* Is called only once, on initialization. No longer needs to be called from gang commands
* Probably about 500% faster



--

That's just an example of how one of the functions can be HIGHLY optimized. thiaZ, read and learn!


P.S Blame the forum for the indention
Reply


Messages In This Thread
LVP 2.93 - What needs to happen? - by Matthias - 11-06-2010, 09:06 AM
Re: LVP 2.93 - What needs to happen? - by Jay - 11-06-2010, 11:26 AM
Re: LVP 2.93 - What needs to happen? - by Jay - 11-06-2010, 02:36 PM
Re: LVP 2.93 - What needs to happen? - by MrBondt - 11-06-2010, 02:48 PM
Re: LVP 2.93 - What needs to happen? - by Jay - 11-06-2010, 03:00 PM