Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You don't mind every single possible optimization right? #1

Open
MikuAuahDark opened this issue Feb 4, 2021 · 3 comments
Open

You don't mind every single possible optimization right? #1

MikuAuahDark opened this issue Feb 4, 2021 · 3 comments

Comments

@MikuAuahDark
Copy link

If so, then read along.

io.write(tostring(timeStamp).." = "..state:ToString());
Don't concat when passing string to io.write. io.write accepts arbitrary arguments which has same effect as concatention, so you can do io.write(tostring(timeStamp), " = ", state:ToString()) instead.
io.Write(tostring(timeStamp).." = "..self.stateList[timeStamp]:ToString());
Same as above.
local strWrite = tostring(self.version).." = [ \n";
-- foreach gameStat to save
for _,v in ipairs(Config.gameStats) do
strWrite = strWrite.. " "..v.." = "..tostring(game(v)).."\n";
end
strWrite = strWrite.."]; \n";
-- foreach player
for _,id in ipairs(player(0,"tableliving")) do
strWrite = strWrite..tostring(id).." = ( \n";
for _,v in ipairs(Config.playerStats) do
strWrite = strWrite.." "..v.." = "..player(id,v).."\n";
end
strWrite = strWrite.."); \n";
end
io.write(strWrite);
Let strWrite be table and for every string you want to add, append into the table with strWrite[#strWrite + 1] = str. Then when writing, do io.write(table.concat(strWrite)). Those string concatenation can be expensive (both in terms of performance and memory) once strWrite is large.
function GameState:ToString()
local strReturn = "{ \n"
for _,v in pairs(self.list) do
strReturn = strReturn.." { '"..tostring(v[1]).."'";
for i=2,3 do
strReturn = strReturn..", '"..tostring(v[i]).."'";
end
strReturn = strReturn.." }, \n"
end
strReturn = strReturn .. "}; \n"
return strReturn;
end
Same strategy as above, create temporary table then concatenate in one go with table.concat.
function Assembler:Assemble()
local output = "";
for k,v in ipairs(self.list) do
output = output..tostring(v)..self.delimiter;
end
return output;
end
Same strategy as above.
for _,id in ipairs(player(0,"tableliving")) do
If you're using vanilla Lua 5.1 (not LuaJIT), use pairs.

As a side note: Files opened with io.open is buffered by default. It's usually okay to left the buffer setting as-is, but if you want (which may or may not impact performance), you can set the buffering mode with file:setvbuf.

@VADemon
Copy link

VADemon commented Feb 4, 2021

Don't concat when passing string to io.write. io.write accepts arbitrary arguments which has same effect as concatention

Except under the hood in standard Lua, io.write() makes a system call for each argument instead of concatenation. That's slower, especially on Intel CPUs and the endless security patches.

See update below

@MikuAuahDark
Copy link
Author

MikuAuahDark commented Feb 5, 2021 via email

@VADemon
Copy link

VADemon commented Feb 26, 2021

I concur, Lua does a call to fwrite (POSIX) under the hood for each argument of io.write(), but appears to be fastest still.

"Octuple" = File:write(chars[i], chars[i+1], chars[i+2], chars[i+3],chars[i+4], chars[i+5], chars[i+6], chars[i+7])

$ filewrite.lua
// time in seconds, e.g. 0.35 = 350ms
// SIZE = Size of random characters written to file

SIZE in MiB:1
single Character per call: :   0.15
octuple Character per call: :   0.09
concat8x Character per call: :   0.11
tableconcat8x Character per call: :   0.08

SIZE in MiB:2
single Character per call: :   0.35
octuple Character per call: :   0.15
concat8x Character per call: :   0.21
tableconcat8x Character per call: :   0.20

SIZE in MiB:16
single Character per call: :   2.59
octuple Character per call: :   1.37
concat8x Character per call: :   1.58
tableconcat8x Character per call: :   1.63

filewrite.lua.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants