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

feat(citizen-scripting-lua): table.size function #3093

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

someone005
Copy link
Contributor

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.

@github-actions github-actions bot added the ScRT: Lua Issues/PRs related to the Lua scripting runtime label Jan 23, 2025
@Mathu-lmn
Copy link
Contributor

Any benchmark to compare the performance of using this instead of doing the size in Lua ?

@github-actions github-actions bot added the invalid Requires changes before it's considered valid and can be (re)triaged label Jan 24, 2025
@AvarianKnight
Copy link
Contributor

Is there really a reason to add this vs the user having their own extension functions (similar to how gmod does it)

@someone005
Copy link
Contributor Author

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.

@Mathu-lmn
Copy link
Contributor

May I ask what the table size was used for those results ?

@FabianTerhorst
Copy link
Contributor

FabianTerhorst commented Jan 24, 2025

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.

@someone005
Copy link
Contributor Author

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.

@AvarianKnight
Copy link
Contributor

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.

@thelindat
Copy link
Contributor

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.

@someone005 someone005 closed this Jan 24, 2025
@someone005 someone005 reopened this Jan 24, 2025
@someone005
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Requires changes before it's considered valid and can be (re)triaged ScRT: Lua Issues/PRs related to the Lua scripting runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants