Implement Native Lua Vector Datatype - #5297
Open
MohabCodeX wants to merge 1 commit into
Open
Conversation
- Add LUA_TVEC as first-class 128-bit 4D float vector in Lua core runtime - Implement in-VM fast-path arithmetic (OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_UNM, OP_LEN) - Support fast field indexing, direct component assignment, and swizzling - Add value-based table key hashing and vector equality comparisons - Provide core vector library with helper math methods and unpack support - Integrate LUA_TVEC into CScriptArgReader, CLuaArgument, and CLuaArguments Replaces multitheftauto#1728 Fixes multitheftauto#321
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces #1728
Fixes #321
This is basically the last part of the OOP/vector optimization work from #5278, #5279, #5280 and #5290. It completes the native vector idea that was originally started by @Pirulax in #1728.
The main goal here was to make vectors actual native Lua values instead of relying on userdata/metatables and C function calls. With these changes in place, OOP element properties are now faster than procedural calls (6ms vs. 41ms), while native vector math is 12.6× faster than legacy OOP vectors, getting close to raw CPU performance.
What changed
vector/LUA_TVECas a native 128-bit 4D floating point type in the Lua runtime.+,-,*,/,^, unary-and length calculations now have VM fast paths, without going through metamethods or C functions..x,.y,.z,.w, numeric indexing ([1]to[4]) and swizzling like.xy,.xyz, etc.CScriptArgReader,CLuaArgumentevent serialization and the JSON converters.vectorlibrary.Lua API
Creating a vector:
x,yandzare required.wis optional and defaults to0.0.Example:
Available methods
Both method and static versions are available:
vec:dot(otherVec)/vector.dot(v1, v2)vec:cross(otherVec)/vector.cross(v1, v2)vec:length()/vector.length(v)vec:lengthSquared()/vector.lengthSquared(v)vec:normalize()/vector.normalize(v)vec:distance(otherVec)/vector.distance(v1, v2)vec:distanceSquared(otherVec)/vector.distanceSquared(v1, v2)Constants:
vector.zero=vector(0, 0, 0, 0)vector.one=vector(1, 1, 1, 1)vector.forward=vector(0, 1, 0, 0)vector.right=vector(1, 0, 0, 0)vector.up=vector(0, 0, 1, 0)Benchmarks
500,000 operations per test:
+,-,*)2,918 ms230 ms12.7x:dot,:length)263.6 ms56.3 ms4.7x.x,.y,.z)695.3 ms21.3 ms33x3,877 ms307.3 ms12.6xSounds a bit unrealistic? Yeah, test it yourself :)))
vector_benchmark.zip
For comparison, running around 1.5 million synchronous operations in a single frame previously froze the game for roughly 4 seconds and triggered the black loading screen. With native vectors, the same workload causes only a frame drop of around a second or less, so normal gameplay shouldn't be affected.