SA-MP Forums Archive
Why is this function lagging the server? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Why is this function lagging the server? (/showthread.php?tid=610879)



Why is this function lagging the server? - MayaEU - 29.06.2016

Hello, when my server is saving the cars.cfg, the server will lag for like 5-10 seconds
is there a reason for that?

pawn Код:
forward SaveCars();
public SaveCars()
{
    new idx;
    new File: file2;
    while (idx < sizeof(CarInfo))
    {
        new coordsstring[800];  //                   0  1  2  4  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        format(coordsstring, sizeof(coordsstring), "%d,%f,%f,%f,%f,%d,%d,%s,%s,%d,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%i,%i,%i,%i,%i,%i,%i,%i,%f,%i,%d,%i,%i,%i,%i,%i,%i,%i,%d,%d\n",
        CarInfo[idx][cModel], // 0
        CarInfo[idx][cLocationx], // 1
        CarInfo[idx][cLocationy], // 2
        CarInfo[idx][cLocationz],  // 3
        CarInfo[idx][cAngle],  //4
        CarInfo[idx][cColorOne], //5
        CarInfo[idx][cColorTwo], //6
        CarInfo[idx][cOwner], //7
        CarInfo[idx][cDescription], //8
        CarInfo[idx][cValue], //9
        CarInfo[idx][cPlate], //10
        CarInfo[idx][cOwnedCar], //11
        CarInfo[idx][cLock], //12
        CarInfo[idx][cMod][0], //13
        CarInfo[idx][cMod][1], //14
        CarInfo[idx][cMod][2], //15
        CarInfo[idx][cMod][3], //16
        CarInfo[idx][cMod][4], //17
        CarInfo[idx][cMod][5], //18
        CarInfo[idx][cMod][6], //19
        CarInfo[idx][cMod][7], //20
        CarInfo[idx][cMod][8], //21
        CarInfo[idx][cMod][9], //22
        CarInfo[idx][cDonate], //23
        CarInfo[idx][cInsure], //24
        CarInfo[idx][cTrunkWep][1], //25
        CarInfo[idx][cTrunkAmmo][1], //26
        CarInfo[idx][cTrunkWep][2], //27
        CarInfo[idx][cTrunkAmmo][2], //28
        CarInfo[idx][cTrunkWep][3], //29
        CarInfo[idx][cTrunkAmmo][3], //30
        CarInfo[idx][cTrunkWep][4], //31
        CarInfo[idx][cTrunkAmmo][4], //32
        CarInfo[idx][cTrunkArmour], //33
        CarInfo[idx][cTrunkCounter], //34
        CarInfo[idx][cAlarm], //35
        CarInfo[idx][cTrunkPot], //36
        CarInfo[idx][cTrunkCrack], //37
        CarInfo[idx][cTrunkMatsa], //38
        CarInfo[idx][cTrunkMatsb], //39
        CarInfo[idx][cTrunkMatsc], //40
        CarInfo[idx][cImpounded], //41
        CarInfo[idx][cImpoundedPrice], //42
        CarInfo[idx][cDestroys], //43
        CarInfo[idx][cInsurePrice]); //44
        if(idx == 0)
        {
            file2 = fopen("cars.cfg", io_write);
        }
        else
        {
            file2 = fopen("cars.cfg", io_append);
        }
        fwrite(file2, coordsstring);
        idx++;
        fclose(file2);
    }
}



Re: Why is this function lagging the server? - iKarim - 29.06.2016

You're using file function to save lots of cars, of course it will lag the server, you should consider using SQLite or MySQL for that.


Re: Why is this function lagging the server? - Vince - 29.06.2016

Plus the fact that you're opening and closing the file over and over again instead of just opening it once and closing it once. Pretty much exactly what dini is doing internally and also the reason why it's now considered the worst file management system. Disk IO is inherently extremely slow compared to CPU cache and RAM.


Re: Why is this function lagging the server? - Crayder - 29.06.2016

pawn Код:
forward SaveCars();
public SaveCars()
{
    new idx;
    new File: file2 = fopen("cars.cfg", io_append);
    while (idx < sizeof(CarInfo))
    {
        new coordsstring[800];  //                   0  1  2  4  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        format(coordsstring, sizeof(coordsstring), "%d,%f,%f,%f,%f,%d,%d,%s,%s,%d,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%i,%i,%i,%i,%i,%i,%i,%i,%f,%i,%d,%i,%i,%i,%i,%i,%i,%i,%d,%d\n",
        CarInfo[idx][cModel], // 0
        CarInfo[idx][cLocationx], // 1
        CarInfo[idx][cLocationy], // 2
        CarInfo[idx][cLocationz],  // 3
        CarInfo[idx][cAngle],  //4
        CarInfo[idx][cColorOne], //5
        CarInfo[idx][cColorTwo], //6
        CarInfo[idx][cOwner], //7
        CarInfo[idx][cDescription], //8
        CarInfo[idx][cValue], //9
        CarInfo[idx][cPlate], //10
        CarInfo[idx][cOwnedCar], //11
        CarInfo[idx][cLock], //12
        CarInfo[idx][cMod][0], //13
        CarInfo[idx][cMod][1], //14
        CarInfo[idx][cMod][2], //15
        CarInfo[idx][cMod][3], //16
        CarInfo[idx][cMod][4], //17
        CarInfo[idx][cMod][5], //18
        CarInfo[idx][cMod][6], //19
        CarInfo[idx][cMod][7], //20
        CarInfo[idx][cMod][8], //21
        CarInfo[idx][cMod][9], //22
        CarInfo[idx][cDonate], //23
        CarInfo[idx][cInsure], //24
        CarInfo[idx][cTrunkWep][1], //25
        CarInfo[idx][cTrunkAmmo][1], //26
        CarInfo[idx][cTrunkWep][2], //27
        CarInfo[idx][cTrunkAmmo][2], //28
        CarInfo[idx][cTrunkWep][3], //29
        CarInfo[idx][cTrunkAmmo][3], //30
        CarInfo[idx][cTrunkWep][4], //31
        CarInfo[idx][cTrunkAmmo][4], //32
        CarInfo[idx][cTrunkArmour], //33
        CarInfo[idx][cTrunkCounter], //34
        CarInfo[idx][cAlarm], //35
        CarInfo[idx][cTrunkPot], //36
        CarInfo[idx][cTrunkCrack], //37
        CarInfo[idx][cTrunkMatsa], //38
        CarInfo[idx][cTrunkMatsb], //39
        CarInfo[idx][cTrunkMatsc], //40
        CarInfo[idx][cImpounded], //41
        CarInfo[idx][cImpoundedPrice], //42
        CarInfo[idx][cDestroys], //43
        CarInfo[idx][cInsurePrice]); //44
        fwrite(file2, coordsstring);
        idx++;
    }
    fclose(file2);
}
As Vince said, open the file ONCE and close it ONCE.


Re: Why is this function lagging the server? - MayaEU - 29.06.2016

Thanks alot Crayder rep+


Re: Why is this function lagging the server? - MayaEU - 29.06.2016

and you too vince, thanks alot. rep+