Skip to content

Store Vector and Matrix objects as inline userdata - #5280

Open
MohabCodeX wants to merge 2 commits into
multitheftauto:masterfrom
MohabCodeX:feat/vector-matrix-inline-userdata
Open

Store Vector and Matrix objects as inline userdata#5280
MohabCodeX wants to merge 2 commits into
multitheftauto:masterfrom
MohabCodeX:feat/vector-matrix-inline-userdata

Conversation

@MohabCodeX

Copy link
Copy Markdown
Contributor

Fixes #321

Vectors and matrices are now stored directly inside Lua userdata memory buffers instead of allocating separate wrapper objects on the C++ heap.

Performance

Scenario Before (Master Heap Objects) After (Inline Userdata) Difference
100k Vector3 additions (+) 238 ms ~130-150 ms ~40% faster execution (1.65x speedup)
GC finalizers invoked 100,000 C++ callbacks 0 (Normal bulk sweep) No finalizer overhead

Verification and Test Plan

-- Verify vector arithmetic and property access
crun local v1, v2 = Vector3(1, 2, 3), Vector3(10, 20, 30); outputChatBox("Vector3 Addition: " .. tostring(v1 + v2))
-- Expected: "Vector3 Addition: vector3: { x = 11.000, y = 22.000, z = 33.000 }"

-- Run 100k benchmark
crun local t = getTickCount(); local acc = Vector3(0,0,0); for i=1,100000 do acc = acc + Vector3(1,2,3) end; outputChatBox("100k Benchmark: " .. (getTickCount() - t) .. " ms")
-- Expected: "100k Benchmark: ~130-150 ms" (down from ~240 ms on master)

…heftauto#321)

Store Vector2, Vector3, Vector4, and Matrix objects directly within Lua userdata
memory instead of allocating wrapper objects on the C++ heap.

- Removes heap allocation (new/delete) on vector and matrix instantiation.
- Bypasses CIdArray lookups in argument parsers via direct type traits.
- Eliminates __gc finalizer callback overhead during collection cycles.
- Updates memory reporting to reflect actual struct sizes without artificial bias.
@FileEX FileEX added the bugfix Solution to a bug of any kind label Aug 30, 2026
- Remove obsolete vector checks from lua_pushuserdata in LuaCommon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Solution to a bug of any kind

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vectors are incredibly slow

2 participants