• 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!

TFS 1.X+ A big table can lag the server?

pepsiman

---
Joined
Nov 17, 2017
Messages
128
Solutions
5
Reaction score
85
Hi, i have a table with a lot of data (strings and integers). I declare it once, but, that may cause lag or something due to memory required to it?
This is the data which contains
2priqz6.jpg
 
Solution
@pepsiman
For LUA I would calculate like this:
- each number [big, small, decimal, floating] - 8 bytes [64bit architecture]
- each string - number of letters + 8 bytes [to store length]
- each table - 8 bytes [to store number of child elements]
- 'objects' are just tables with some key-value pairs
If you define table in LUA script, you can check script size. It will be close to memory usage.

Storing table with size 1GB won't lag server. It can only lag, if there is milion keys in one big table and you iterate over it with 'for'/'while'.
If there is 100 keys, each with 10MB value, it won't lag when you use 'for'/'while'.

When you read table by known key (number/string) execution time should be same:
PHP:
local 1gbTable...
@pepsiman
For LUA I would calculate like this:
- each number [big, small, decimal, floating] - 8 bytes [64bit architecture]
- each string - number of letters + 8 bytes [to store length]
- each table - 8 bytes [to store number of child elements]
- 'objects' are just tables with some key-value pairs
If you define table in LUA script, you can check script size. It will be close to memory usage.

Storing table with size 1GB won't lag server. It can only lag, if there is milion keys in one big table and you iterate over it with 'for'/'while'.
If there is 100 keys, each with 10MB value, it won't lag when you use 'for'/'while'.

When you read table by known key (number/string) execution time should be same:
PHP:
local 1gbTable = {/*... a lot of data ...*/}
local knownKeyText = 'asd'
local dataAsd = 1gbTable[knownKeyText]
local knownKeyNumber = 123
local data123 = 1gbTable[knownKeyNumber]
No matter how big table it is or how many keys it has.

EDIT:
If you want to optimize memory usage or CPU consumption of some LUA script. There is a short documentation (25 pages) with examples of slow code and optimized version:
https://www.lua.org/gems/sample.pdf
 
Last edited:
Solution
or you can use this:
the empty table/loop is not necessary, just an example i used to calculate a table with 1000000 values
Lua:
local kb = collectgarbage("count")
local t = {}
for i = 1, 1000000 do
    t[i] = i
end
print(string.format("Estimated memory allocated for table: %.2f MB.", (collectgarbage("count") - kb) / 1024))
my result: Estimated memory allocated for table: 8.00 MB.
 
Back
Top