feat(citizen-scripting-lua): table.size function
Goal of this PR
Add table.size function to lua runtime so developers can use it to get size of tables. Mostly useful for hex tables because # will always return 0.
How is this PR achieving the goal
By declaring Lua_TableSize function and setting it in global table.
This PR applies to the following area(s)
ScRT: Lua
Successfully tested on
Game builds: ..
Platforms: Linux
Checklist
- [ x ] Code compiles and has been tested successfully.
- [ x ] Code explains itself well and/or is documented.
- [ x ] My commit message explains what the changes do and what they are for.
- [ x ] No extra compilation warnings are added by these changes.
Any benchmark to compare the performance of using this instead of doing the size in Lua ?
Is there really a reason to add this vs the user having their own extension functions (similar to how gmod does it)
Any benchmark to compare the performance of using this instead of doing the size in Lua ?
I did a benchmark and c++ version is faster than Lua.
Lua table.size: 0.216406 seconds
C++ table.size: 0.0784628 seconds
Is there really a reason to add this vs the user having their own extension functions (similar to how gmod does it)
I would say it can help developers so they don't have to write these functions in every script they make. I know that frameworks such as ESX provide functions like this but people who makes standalone scripts or "multi-framework" have to make their own. It will simply make their work a little easier/faster.
May I ask what the table size was used for those results ?
Isn't the recommended way to store the size inside a extra value in lua? Then you do not traverse the table to calculate it.
May I ask what the table size was used for those results ?
function table.size(tbl)
local count = 0
for _ in pairs(tbl) do
count = count + 1
end
return count
end
I think there won't be any huge difference with count+=1 method. If i'm wrong u can correct me.
Isn't the recommended way to store the size inside an extra value in lua? Then you do not traverse the table to calculate it.
I have tried one thing but i had a problem with implementing it. I only found a way to do this but it's inside lua source code, so I would have to make pr there. If u have some idea how to make it, show/describe it.
Is there really a reason to add this vs the user having their own extension functions (similar to how gmod does it)
I would say it can help developers so they don't have to write these functions in every script they make. I know that frameworks such as ESX provide functions like this but people who makes standalone scripts or "multi-framework" have to make their own. It will simply make their work a little easier/faster.
My main concern here is that people will constantly want more and more functions added for convenience which adds more possible breakage points for Cfx if there's ever another Lua version upgrade.
It just seems so unnecessary. It's not hard to implement in your own resource or load from an external resource.
I think there won't be any huge difference with count+=1 method.
I would assume he meant how large was the table being tested, rather than what was the Lua function.
My main concern here is that people will constantly want more and more functions added for convenience which adds more possible breakage points for Cfx if there's ever another Lua version upgrade.
So their functions will simply be assessed in terms of usefulness as this one.
It just seems so unnecessary. It's not hard to implement in your own resource or load from an external resource.
Of course it's not hard but implementing it for the x time is frustrating.
I would assume he meant how large was the table being tested, rather than what was the Lua function.
10.000.000