[Tutorial] When to use INI and when not
#1

When to use INI and when not

Introduction
First of all , every SA-MP programmer needs to know one important thing, PAWN is lot lot lot slower than natives. Since every INI Reader does most of the work in plain PAWN code , it would be very slow.I see many budding SA-MP programmers using INI Files to store their player database though there are much faster methods available such as SQLite,MySQL,etc.This whole tutorial is dedicated to those who still make use of INI to store large chunks of information including player database.This little tut will give an idea when and when not to use INI.

Before we begin, go through the following conversation.
Quote:
Originally Posted by Yashas
I am wondering if native functions are faster than the ones I make.

Код:
 for(new i = 400000;i;i--)
  strcmp(x1,x2);
 new b = GetTickCount();
 printf("SD TST:%d",b-a);

 a = GetTickCount();
 for(new i = 400000;i;i--)
 {
    new j =0;
    while(1)
    {
        if(x1[j] != x2[j++])
            break;
    }
 }
For PAWN it seems that native is twice as fast as mine!

My version had to be faster because it cannot be optimized further ,in fact it doesn't even function like strcmp and does less work than strcmp.

I had made my own parser which actually checks every character,does all the copying,all comparing,... - So is this bad?I thought I would avoid the function overhead by doing it but....

Thanks
Quote:
Originally Posted by ******
No, natives are WAY faster than PAWN code! If you found it was twice as fast I would suggest you retest as I would suspect it is far more than that.
Was that mind blowing? o.O
As I have already said INI Readers make heavy use of PAWN code with all kinds of loops and...therefore INI Readers will be extremely slow when compared with SQLite(SA:MP natives) or MySQL(plugin).

I wonder who was the first guy who started using INI is store a player database.The sole purpose of INI when invented was to store settings for an application.

How does an INI Reader work?
Since I myself have written an INI Processor I have a very good picture of what happens when you read data from INI files.Every INI processor first opens the file then stores all the content in a huge buffer.It then parsers the whole file.You then call a Read function which will again search the whole cache for the key.Let's see what basically happens when you want to read the contents of this file.

Код:
Name=Yashas
Score=1234
Money=12345
Searching for 'Name' won't take that much time since it is the first key.While searching for 'Score' Key, the INI Reader first checks 'Name' to see if it matches with 'Score' then checks for 'Score'. If you wanted 'Money' then it will have to first check 'Name' then 'Score' and then finally 'Money'.

To read a file with N keys, you will have to do (N(N+1)/2) string comparisons.Not just that, it then has to separate out the key value from the cache which is again very slow.

You might have already realized how slow could this be.Though y_ini and eINI provide an alternative for loading data using callbacks which is more efficient , they are still slow.

Writing information to INI files is more worse.The INI readers will first find the key,then use format lot of times.... in fact writing is 4-10x slower than reading for eINI & y_ini for a small file.

What if you had just edit one key? You need to open the file ,load everything, change the key, make a new file and copy all the data.Wow, isn't it amazing?

When you must NOT use INI?
Since INI Processing takes lot of time, you must not use them when there is data to be loaded or written frequently.For example, a player database.You must also avoid using INI when you have to load huge pieces of information since it could probably lag your server badly sometimes.The server may even freeze for a while.

Here are few situations where you must avoid using INI:
  • Player Database
  • Houses and other similar stuff
  • Loading a file which is half a MB
Other Alternatives to INI
There are many alternatives to INI.I will be mentioning two of them here.

MySQL:
You can use MySQL which is by design is efficient in searching and updating bits of information.Using MySQL also gives you further advantages.You can share information between your server,UCP,website,etc easily.PHP has ready-made functions which allow you to use MySQL.You can even make your own desktop/mobile app for your server which can access information from MySQL.

If you have never heard or used MySQL, here are few nice tutorials to get you started.
Using BlueG's MySQL plugin R7 and newer (with cache)
MySQL plugin ORM usage tutorial

SQLite
This another alternative to INI. SQLite support comes with SA:MP server by default and does not require any additional plugin.

How to use SQLite

If you find that hard, here is ready-made include for making User Database for you.
BUD by Slice
Suggestion by Gammix

It might be a cumbersome task to convert all your code to MySQL or SQLite from INI but its worth taking all the trouble.I am sure that you won't regret later.There are some tools which allow you to convert your code which you will be able to find using the magic button.

When can you use INI?
You can use INI where you are not going to frequently open and close files.I personally use INI Files to store MySQL Configuration and language strings.I load them only once during the server start up so its not a problem.
  • One-Time Reading/Writing
  • If data is not read or written frequently
  • To store server configuration,settings,etc
Conclusion
I hope you now understand when and when not to use INI.

Feel free to suggest something.
Reply
#2

I didn't realize INI files process is like that, I sure will be switching to one of the suggestions you provide above.
Reply
#3

This is just pure common sense you gather throughout the years.
Reply
#4

It is not pure common sense for everyone.Check Scripting Help forum.You'll find at least 3-5 threads where someone is using INI to store player database.

Read the first reply to this thread.Was it common sense?

Maybe this tut sounds silly for you but it isn't for everyone.There are so many who do not know this.And most of them(newbies) check the tutorials section and will go through this tut and will commit the mistake.Or else they would write using INI and later their sever will start lagging then its extremely painful to convert INI code to MySQL or SQLite.
Reply
#5

Thank you for this. (:
Reply
#6

Job, Well done. There are bits I never knew, thank you for this friend. (+1)
Reply
#7

To my opinion you just show some obvious points, MySQL known for better system on saving player database. but dont forget that not everyone can afford MySQL databases. This tutorial make sence only for huge communities and often paid donators. otherwise how im gonna hold mysql database?
I'm using y_ini to save everything in the gamemode im creating and it works great, no lags. Extreamly slow isn't the word to describe y_ini, it isn't the fastest read/write system but it has its positive points, its free and faster then other ini write/read system.
Reply
#8

Quote:
Originally Posted by Yashas
Посмотреть сообщение
To read a file with N keys, you will have to do (N(N+1)/2) string comparisons.Not just that, it then has to separate out the key value from the cache which is again very slow.
Are you sure about this? It's not hard to implement dictionaries that use keys that are returned by string hashing methods. And complexity you pointed (N*(N+1)/2) * M, where is M max string length will be reduced to N*M.
Reply
#9

eINI provides another method where finding a key takes constant time(much faster than MySQL).You can directly tell the position of the key so that it needn't search the whole linked list.

How the parser assigns relative key and section ids?

then use those IDs

Reading (Static)

You can also read a dummy player file and store the relative key ids in variables and use them for all files.The IDs won't change if all the profiles follow a particular format.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)