Page 1 of 1

Lua string concatenation

Posted: Wed Dec 01, 2021 7:40 pm
by S1N74X
Hello,

need your opinion.
Currently iam fiddeling around with Pointeroffsets.
I have an repeating pattern. But in between the offsets are changing.
Spoiler

Code: Select all

local startp = '[[[[pb]+120]+' -- start of Pointerpath
local endp = ']+11A0]' -- end of Pointerpath
local offsetResult = nil 

local offsets = {0,8,10,18,20,28,30,38,40,48,50} -- offset Table

for i=1,#offsets  do
offsetResult =  startp .. offsets[i] .. endp
print(offsetResult) -- or readFloat or whatever
end
The Output :
[[[[pb]+120]+0]+11A0]
[[[[pb]+120]+8]+11A0]
....
Works as expected.
My question is :
String concatenation is claimed to be slow.
What is your experience ?
How would you solve such a Problem?

Usefull hints or suggestions are welcome.

Re: Lua string concatenation

Posted: Thu Dec 02, 2021 1:44 am
by TheByteSize
How about use Assembly instead? ;)

Re: Lua string concatenation

Posted: Thu Dec 02, 2021 9:30 am
by panraven
Most pure lua running time should be neglectable compare to inter-process memory read/write.
lua string concatenation is said to be inefficient mostly only when creating too many intermediate string within a heavy loop.
It seems your case is fine if just a few strings created once and done, ie the address strings are fixed, only the interpreted address content of the fixed symbols eg. pb or [pb] changed with time, which should be handled by CE internally.

Re: Lua string concatenation

Posted: Thu Dec 02, 2021 3:08 pm
by S1N74X
TheByteSize wrote:
Thu Dec 02, 2021 1:44 am
How about use Assembly instead? ;)
Thx for your reply.
I am not experienced enough yet to do this in pure ASM
Still learning :)

Re: Lua string concatenation

Posted: Thu Dec 02, 2021 3:18 pm
by S1N74X
panraven wrote:
Thu Dec 02, 2021 9:30 am
Most pure lua running time should be neglectable compare to inter-process memory read/write.
lua string concatenation is said to be inefficient mostly only when creating too many intermediate string within a heavy loop.
It seems your case is fine if just a few strings created once and done, ie the address strings are fixed, only the interpreted address content of the fixed symbols eg. pb or [pb] changed with time, which should be handled by CE internally.
Thank you for your Answer.
After taking a nap, i came up with this solution.
Spoiler

Code: Select all

--on script activation
local startp = '[[[[pb]+120]+' -- ppstart
local endp = ']+11A0]' -- ppend
local offsetResult = nil 
local offsets = {0,8,10,18,20,28,30,38,40,48,50} -- offsets
local results = {} -- result of concatination

for i=1,#offsets  do
offsetResult =  startp .. offsets[i] .. endp -- build pp string
table.insert(results,offsetResult) -- store pp string
end
 
-- in timed condition
for j=1,#results do
print(results[j]) -- or readFloat or whatever
end

No one said i have to do the concationation at "runtime" :)