19.08.2012, 15:52
I'm sure most scripters already know that "Dini is not good", but this thread attempts to explain WHY.
If you look at the source code (dini.inc), you see that the main functions that read/write data from/to a file are dini_Get and dini_Set. Other functions, like dini_IntSet and dini_Float, are just wrappers for the 2 main functions. So if you use dini_IntSet in your script, you actually use dini_Set and if you use dini_Float, you actually use dini_Get.
That's why I'm only going to talk about dini_Set and dini_Get.
So, let's say you want to save some player data to a file. Why not use Dini? It's easy!
Loading:
Saving:
Using Dini is easy, but let's look how it works.
What dini_Get does:
1) opens the file
2) reads the file line by line (first line, second line, etc) until it finds the keyword
3) closes the file and returns the value
dini_Get is actually not so bad. You can use it if you need to get only 1 value from the file. The inefficiency comes from reading multiple values as it opens and re-reads the file for every value. So in the example above, it reads the file 4 times. The same job can be done by reading it only once, no matter how many values you want to get.
dini_Set however, is the main reason why "Dini is not good".
What dini_Set does:
1) opens the source file for reading
2) opens a temp file for writing
3) reads the source file line by line (first line, second line, etc) until the end of the file
4) while reading, it writes the lines to temp file and if it finds the keyword, then replaces the value with the new one
5) if the keyword was not found in the source file, it writes it with the new value to the end of the temp file
6) closes both files
7) opens the temp file for reading
8) opens the source file for writing
9) reads the temp file line by line until the end of the file
10) while reading, it writes the lines back to source file
11) closes both files
12) removes the temp file
As you can see, a lot is done when dini_Set is called. In the example above, all this is done 4 times! dini_Set opens and reads/writes both files 2 times. So if you want to save 4 values, it will open and close the file 8 times! Again, the same job can be done by writing it only once, no matter how many values you want to save.
Saving 4 values to INI file is already problematic. Imagine saving 20 values for each player when 100 players are connected!
In my opinion, Dini is a real "hard drive killer" and should be avoided as much as possible.
TIP: When choosing INI system, make sure it reads and writes the file only once.
If you look at the source code (dini.inc), you see that the main functions that read/write data from/to a file are dini_Get and dini_Set. Other functions, like dini_IntSet and dini_Float, are just wrappers for the 2 main functions. So if you use dini_IntSet in your script, you actually use dini_Set and if you use dini_Float, you actually use dini_Get.
That's why I'm only going to talk about dini_Set and dini_Get.
So, let's say you want to save some player data to a file. Why not use Dini? It's easy!
Loading:
pawn Code:
PlayerInfo[playerid][pMoney] = dini_Int(filename, "Money");
PlayerInfo[playerid][pBank] = dini_Int(filename, "Bank");
PlayerInfo[playerid][pScore] = dini_Int(filename, "Score");
PlayerInfo[playerid][pSkin] = dini_Int(filename, "Skin");
pawn Code:
dini_IntSet(filename, "Money", PlayerInfo[playerid][pMoney]);
dini_IntSet(filename, "Bank", PlayerInfo[playerid][pBank]);
dini_IntSet(filename, "Score", PlayerInfo[playerid][pScore]);
dini_IntSet(filename, "Skin", PlayerInfo[playerid][pSkin]);
What dini_Get does:
1) opens the file
2) reads the file line by line (first line, second line, etc) until it finds the keyword
3) closes the file and returns the value
dini_Get is actually not so bad. You can use it if you need to get only 1 value from the file. The inefficiency comes from reading multiple values as it opens and re-reads the file for every value. So in the example above, it reads the file 4 times. The same job can be done by reading it only once, no matter how many values you want to get.
dini_Set however, is the main reason why "Dini is not good".
What dini_Set does:
1) opens the source file for reading
2) opens a temp file for writing
3) reads the source file line by line (first line, second line, etc) until the end of the file
4) while reading, it writes the lines to temp file and if it finds the keyword, then replaces the value with the new one
5) if the keyword was not found in the source file, it writes it with the new value to the end of the temp file
6) closes both files
7) opens the temp file for reading
8) opens the source file for writing
9) reads the temp file line by line until the end of the file
10) while reading, it writes the lines back to source file
11) closes both files
12) removes the temp file
As you can see, a lot is done when dini_Set is called. In the example above, all this is done 4 times! dini_Set opens and reads/writes both files 2 times. So if you want to save 4 values, it will open and close the file 8 times! Again, the same job can be done by writing it only once, no matter how many values you want to save.
Saving 4 values to INI file is already problematic. Imagine saving 20 values for each player when 100 players are connected!
In my opinion, Dini is a real "hard drive killer" and should be avoided as much as possible.
TIP: When choosing INI system, make sure it reads and writes the file only once.