- 
 
- 
  Notifications
 You must be signed in to change notification settings 
- Fork 496
Implement lua-vec - vectors as native Lua datatype #1728
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
Conversation
So, whats people's opinion on this?
I'd be really happy to see this in 1.6.
Also it seems like we could without problems add support for matrices as well. A GCObject is 180 bytes, a regular 3x3 matrix is.. 48, which means that the unions size would stay the same.
 
 
 
 ghost
 
 
 
 commented
 Dec 9, 2020 
 
 
 
I like the changes.
@qaisjp suggested that it would be nice to have a separate branch to keep track of our modifications to Lua.
We can have a submodule for Lua. In this way, it will be easy to track the changes.
So, we should create a separate repo?
 
 
 
 ghost
 
 
 
 commented
 Dec 9, 2020 
 
 
 
So, we should create a separate repo?
Hmm, that doesn't seem like a good idea. Let's just create a new branch for Lua. I'd do it like this:
- Download Lua 5.1 original source and commit the code to the branch.
- Copy the Lua source from MTA vendor to the new branch.
- And then create a PR for lua-vec changes.
My approach would be:
- Fork Lua repo to multitheftauto org. This keeps original history instead of destroying the Lua 5.1 history, and makes it easier to cherry-pick improvements later.
- Cherry-pick our custom 5.1 mods from mtasa-blue to a branch on the Lua repo
- Replace vendor/lua in mtasa-blue with a submodule that targets that Lua repo
- Create a PR for lua-vec changes in the Lua repo.
This approach is compatible with the submodule system proposed in #319.
AFAIK There is no official Lua repo on GitHub, if that's what you meant by Fork Lua repo.
I dont think the Lua history is necessary. I'll just create a new repo with the 5.1 source code from Lua website
Okay I think I did it.
Is this  what you meant?
AFAIK There is no official Lua repo on GitHub, if that's what you meant by Fork Lua repo.
I dont think the Lua history is necessary. I'll just create a new repo with the 5.1 source code from Lua website
There's an official mirror on GitHub of the Lua repo, here. It has a tag for every released Lua version, all the way back to 5.1. I agree that keeping the history may come in handy in the future for some cherry-picks, for updating Lua more easily, or whatever, but I would also understand not keeping the original commit history if it's deemed too verbose.
Please look at MTA org's /lua repo.
I have filtered our commits to vendor/lua + added base Lua at the top (source code for 5.1.5 downloaded from Lua website).
Would that fit?
i noticed you call the vector type in Lua only "vec" which is inconsistent to the other types: "number", "table", ...
the type should be "vector"
since Lua uses doubles, shouldn't the vectors not use doubles too?
There would be no benefit of the additional memory required. MTA uses float vectors everywhere, the additiona precision would be lost.
Yeah, the type name will be changed, once this PR gets reviewed or something.. Tbh, we didnt really agree on a name yet. I think vector (perhaps with a capital V) is acceptable. Since it's not vanilla Lua, we might as well stick to our naming convention.
maybe cVector
where c stands for custom
or if someone has better idea he should write it
btw. since my knowledge is not too big about instruction sets
so, only asking if it's possible:
could it be possible that the vectors could somehow utilize something like sse2 for even higher performance?
Please look at MTA org's /lua repo.
I have filtered our commits to vendor/lua + added base Lua at the top (source code for 5.1.5 downloaded from Lua website).
Would that fit?
I'm not sure if you were talking to me, but yeah, it looks good to me. I was just pointing out that the original approach that @qaisjp suggested was possible due to the existence of that mirror.
Anyways, the submodule stuff #2112
The main bottleneck is probably still the Lua side, rather than the actual maths. Not worth the headache. But where I think we might benefit from SSE is our vector stuff.. although I wonder why MSVC doesn't use SSE by default there.. Should be pretty trivial I belive.
...into feature/native-vectors
Edit: Turns out there's 2 lua-vec branches: One which is a first-class value like a number, bool, etc.. and the second one (this) treats it as GCObject's. Obviously the first one is faster, but comes at the expense of making TValue be 16 bytes instead of 8 (Which translates to basically 2x memory usage for Lua values) and probably performance loss as it would take 4 memory fetches to move the value into the register. The GC'd version is probably already fast enough IMHO. Although it uses some hackery (some kind of free list?).
@Pirulax A great amount of time has passed, but do you still want to get this pull request merged?
This is very useful and would be a good update.
You don't plan to continue dealing with this PR in the future?
 
 
 
 YSAFE
 
 
 
 commented
 Jan 20, 2025 
 
 
 
Bump?
 
 
 
 Rilot06
 
 
 
 commented
 Apr 25, 2025 
 
 
 
Any news on this? Would make my current project way easier and faster instead of countless float math operations each time.
I think it can be merged, no?
Edit: Actually nvm, this is just the veclib, i'd still have to change all the parser stuff and whatnot to return this type instead of Vector3...
I don't really have the time to deal with this PR to be honest.
So, if anyone has the time to work on it, then great, we can get this merged... Otherwise it is what it is.
Uh oh!
There was an error while loading. Please reload this page.
This PR aims to fix the issue we have with userdata vectors. (#321)
As discussed on discord (and maybe even in the issue itself), using Lua tables would be only marginally better, and the performance benefit would probably be lost when doing table <=> conversion.
Note: I've tested this with bytecode as well (Compiled all default resources, and they all worked perfectly). So no backwards compatibility issues here.
This code(Lua-vec) was "stolen" from here.
I need opinions: Although I've already discussed this with @Woovie, I'd be great if others could give their feedback.
I think naming the datatype as
vectorwould be totally okay (the function table will be namedvectorin lower case, just so we keep Lua's naming convention. We can just alias it toVectorin a form of a global variable)Features in
lua-vec:.x, or even.xxyz,.xx(like in hlsl)Lua-vecuses no additional memory at all! Its free! [2][1]: A Vector4 needs 4 bytes for
lua_newuserdata, anothersizeof(unsigned long)+sizeof(void*)+sizeof(CLuaVector4D(which is 16 + 4 bytes) inCIdArray[2]: Its value is stored in a
GcObjectunion. This is where the userdata is stored as well, so, compared to the userdata implementation, we use no additional memoryTodo:
veccallable (add __call metamethod), right now vectors can only be created withvec.newArgumentParserandCScriptArgReaderto read the new type as aVector2/3/4DVector2/3/4fromnative vectornative vectorfrom existingVector2/3/4(only way to implement this cleanly is to use.x.y.z)LUA_TVECinCLuaArgument(WriteToBitStreamandReadFromBitStream)Tests:
Resource used: vectest.zip
Scene rendering with
smallpt.lua(from Lua-vec):Command:
runsmallpttests 250 250 4 falseCommand: `runsmallpttests 250 250 4 true`
Syntethic add/create/mul/sub benchmark: