• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

[DISCUSSION] setStorage vs Global Variables - Speed Test

Arn

Member
Joined
Mar 8, 2010
Messages
282
Reaction score
18
Hi all,

I'm working on a new OT, and I wanted to figure out which way of storing player information was faster. You can either do something like this:

setStorage(100, 1337)

or you can store the same value in a global variable:

GLOBAL_X = 1337

Now, I wrote two scripts in talkactions that count to some huge number (2 million, 10 million, etc) and then compares the time. So lets look at version 1:

Code:
function onSay(cid, words, param, channel)

	doCreatureSay(cid, getWorldUpTime())
	
	local x = getStorage(100)
	
	while (getStorage(100) ~= 2000000) do
	x = x + 1
	doSetStorage(100, x)
	end
	
	doCreatureSay(cid, getWorldUpTime())
	doCreatureSay(cid, getStorage(100))
	
	
	return true
end

You can see that for the while loop (which counts to 2 million), we get the storage once, and we set the storage once. We do this 2 million times, and then compare the time. Lets look at version two.

Code:
function onSay(cid, words, param, channel)

	doCreatureSay(cid, getWorldUpTime())
	
	local x = GLOBAL_X
	
	while (GLOBAL_X ~= 2000000) do
	x = x + 1
	GLOBAL_X = x
	end
	
	doCreatureSay(cid, getWorldUpTime())
	doCreatureSay(cid, GLOBAL_X)
	
	
	return true
end

We do the same exact thing, but we replace getStorage with GLOBAL_X and we replace doSetStorage with GLOBAL_X = x. We do this 2 million times as well.

Now lets compare the results (on my server):

counting to 2 million with Storage = 8 seconds
counting to 2 million with Globals = 1 second

counting to 10 million with Storage = crashed my server and made my virtual machine unresponsive. I'm at work and can't log back in -.-
counting to 10 million with Globals = 1 second

edit: was able to reconnect.
counting to 100 million with Globals = 6 seconds

So my conclusion is that storing to globals is much faster than playerstorage or globalstorage. Thoughts? Comments?

Thanks,

-Arn

- - - Updated - - -

Nobody cares? Nobody has any input at all? :(
 
Last edited:
I'm not sure but i think every restart/reload/crash you will lose your Global vars. So you should add saving them to server's save and that's more work than simple doSetStorage.
 
Well, you would need to create two dimensional array, first dimension would be player_guid, second storage id
And onShutdown(do we have globalevent like that?) loop everything with setting storages
and onStartup do the reversed thing

But well, I'm wondering why storages use so much time is it Lua call of cpp function that takes so much time or sth? Cause storages are dumped to db at save anyway (right?)
 
Because mysql queries are slow and that's like common knowledge. No need of testing it.
 
BUT I belived that storages are cached in memory and db accessed only onSave and onLogin
 
Because mysql queries are slow and that's like common knowledge. No need of testing it.

I've been working on OT's for quite some time. I would not assume that this is common knowledge.

And, a restart/reload/crash would lose all the data yes, but thats fine, since its only temporary data that supplements the regular data.
 
Well basic fail of lua in OTS is that you cant share global values between lua stats (actions, talksactions etc)
Except that I think that speed is preety much similar.
 
Well basic fail of lua in OTS is that you cant share global values between lua stats (actions, talksactions etc)
Except that I think that speed is preety much similar.

If I declare a global outside of talkactions, I can still call and modify it from within talkactions. The same thing with spells. The global should be the same and still be accessible from either spells or talkactions. Am I incorrect?
 
If I declare a global outside of talkactions, I can still call and modify it from within talkactions. The same thing with spells. The global should be the same and still be accessible from either spells or talkactions. Am I incorrect?

No, tarjei is right. You can't do this (and that's the main con of current tfs luastate).
 
If I declare a global outside of talkactions, I can still call and modify it from within talkactions. The same thing with spells. The global should be the same and still be accessible from either spells or talkactions. Am I incorrect?
Yea you are correct, that's a pretty easy way to share storages through talkactions and stuff.

I've been testing this a few months back a few times aswell, as mentioned before, storages are hold in memory until server saves or player logout.
I've created myself an easy storage system with I/O on lua with arrays which works for some stuff better then queries do.

Tbh. has someone ever tried to make a db system in lua only? I think it could be a good alternative compareable to like xml was back then.
 
Last edited:
No, tarjei is right. You can't do this (and that's the main con of current tfs luastate).

What exactly is the limitation? Surely there has to be a way to fix this or some kind of workaround.
 
What exactly is the limitation? Surely there has to be a way to fix this or some kind of workaround.

Because each action has it's own luastate and different luastates can't share variables. I remember that someone tried to use one luastate for all actions and succeded but it had some serious issues.
 
Well, I am currently testing influence of otc lua engine on tfs engine. So far it's preety impressive. You have power of all that otc engine give + you have just one lua state. Meaning global variables are accesible from every script XD
 
Well, I am currently testing influence of otc lua engine on tfs engine. So far it's preety impressive. You have power of all that otc engine give + you have just one lua state. Meaning global variables are accesible from every script XD

Do you have any additional information on this? I'm looking now, but I'm very interested in seeing how hard this would be to implement. Any information (how to install/change, bugs, etc) would be useful.
 
Back
Top