[Tutorial] Why you should not use Dini
#1

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:

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");
Saving:

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]);
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.
Reply
#2

Useful, i didn't know about dini_Set so thanks about that, and yes people do use dini because it is easy, but they don't know how slow it is comparing to nowdays saving system

My rule is to use ini files for configurations, and MySQL for user database stuff

Anyways thanks for the tutorial.
Reply
#3

I don't think this is a tutorial 0_o (Correct me if I am wrong)
Reply
#4

Quote:
Originally Posted by gtakillerIV
View Post
I don't think this is a tutorial 0_o (Correct me if I am wrong)
Correcting....

Yes it is not a turorial but it's a useful topic so.... Yeah
Reply
#5

About time someone created something like this. I'll make sure to redirect all ignorant people here.
Reply
#6

This topic would stay better here: http://forum.sa-mp.com/forumdisplay.php?f=84

Good explanation MadeMan, I hope more people will start to use Y_INI/MySQL.
Reply
#7

Just something I'd like to add myself (personal opinion): Dini is not bad. There's practically nothing wrong with dini, it just does what it's supposed to do (write and read from ini files). The main point is that it's terribly inefficient and slow compared to other (and especially newer) INI systems like y_ini
Reply
#8

Useful tutorial. Especially to those who are using DINI.
Reply
#9

Usefull, but this is not a tutorial in my opinion. Show people easiest way to convert dini to Y_INI or mysql, then you can call it tutorial.
Reply
#10

Quote:
Originally Posted by MadeMan
View Post
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
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
I think you are correct that dini isn't a good way of saving stats, but you actually didn't say why it is worse then other methods. For example, I would like to know how you would save only one value and that is more effective then this I've quoted. From this tutorial I can only see that Dini shouldn't be used when saving more then one value. Anyway, thank you for tutorial.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)