Add in-place math methods (set, add, sub, mul) to Vector and Matrix classes - #5279
Open
MohabCodeX wants to merge 1 commit into
Open
Add in-place math methods (set, add, sub, mul) to Vector and Matrix classes#5279MohabCodeX wants to merge 1 commit into
MohabCodeX wants to merge 1 commit into
Conversation
- Add in-place mutation methods (:set, :add, :sub, :mul, :div, :scale, :addScaled) to Vector2, Vector3, and Vector4 classes. - Add in-place cross product (:setCross) to Vector3 class. - Add in-place matrix operations (:set, :setIdentity, :setZero, :add, :sub, :mul, :invert) to Matrix class. - Return the target instance directly from Lua stack index 1 to enable fluent method chaining.
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.
Fixes #321
Add in-place math and mutation methods to
Vector2,Vector3,Vector4, andMatrixobjects.What was happening before
Doing arithmetic on vectors (e.g.
pos = pos + velordir = (target - pos) * 0.5) creates a brand-new heap object on every single operation and leaves the previous one for Lua's garbage collector.In high-frequency rendering and physics loops (
onClientRender, projectile simulations, drawing radar blips), doing vector calculations per frame easily generates 50,000+ short-lived userdata objects every second, causing continuous GC pressure and micro-stutters.What this PR does
This adds in-place mutation methods that modify existing vector/matrix memory directly, avoiding heap allocations and GC churn. All methods return
self(*this) so you can chain operations cleanly on a single line.The existing
+,-,*,/operators stay completely untouched for backwards compatibility.Performance
+):addScaled)28.96 KBgarbage created0.41 KBmemory delta220 ms43 ms~380 KBallocated0 KBallocatedNew methods
Vector math (
Vector2,Vector3,Vector4)All vector classes now support in-place arithmetic and assignment. Every method accepts either separate numeric components or another vector of the same type, and returns
self:vec:set(x, y, [z, w])orvec:set(otherVec)-- Overwrites the vector's coordinates in-place.vec:add(x, y, [z, w])orvec:add(otherVec)-- In-place addition (this += other).vec:sub(x, y, [z, w])orvec:sub(otherVec)-- In-place subtraction (this -= other).vec:mul(factor)orvec:mul(otherVec)-- Multiplies by a scalar or component-wise.vec:scale(factor)-- Alias for:mul(factor)when scaling uniformly.vec:div(divisor)orvec:div(otherVec)-- Divides by a scalar or component-wise.vec:addScaled(otherVec, scaleFactor)-- Adds another vector multiplied by a scalar (this += other * scale). Useful for physics integration (pos:addScaled(vel, dt)).Vector3-specific:
vec:setCross(otherVec)-- Computes the cross product in-place (this = this x other). Note that existingvec:cross(other)remains unchanged to return a new vector for compatibility.Matrix operations (
Matrix):mat:set(otherMat)-- Copies another matrix's elements in-place.mat:setIdentity()/mat:setZero()-- Resets matrix to identity or zero.mat:add(otherMat)/mat:sub(otherMat)/mat:mul(otherMat)-- In-place matrix arithmetic.mat:invert()-- Inverts the matrix in-place (distinct frommat:inverse()which returns a new copy).Usage example
Testing
You can verify all new methods directly via
crun/srun:1. Vector2 (
set,add,sub,mul,div,scale,addScaled):2. Vector3 (
set,add,sub,mul,div,scale,addScaled):3. Vector3 Cross Product (
setCross):4. Vector4 (
set,add,sub,mul,div,scale,addScaled):5. Matrix (
setIdentity,add,sub,invert):6. Matrix (
set,setZero):