WireLib.clampPos/Force optimization (#3221)

* WireLib.clampPos/Force optimization

Speeds up WireLib.clampPos by about 2x and WireLib.clampForce by about 5x

* Remove useless nan check

* Forgot

* Use unpack in clampPos

* Missing tab
This commit is contained in:
Astralcircle 2024-12-16 08:09:58 +03:00 committed by GitHub
parent 80c4eb069e
commit a14a2c7b78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -942,11 +942,9 @@ local minx, miny, minz = -16384, -16384, -16384
local maxx, maxy, maxz = 16384, 16384, 16384
local clamp = math.Clamp
function WireLib.clampPos(pos)
pos = Vector(pos)
pos.x = clamp(pos.x, minx, maxx)
pos.y = clamp(pos.y, miny, maxy)
pos.z = clamp(pos.z, minz, maxz)
return pos
local x, y, z = pos:Unpack()
return Vector(clamp(x, minx, maxx), clamp(y, miny, maxy), clamp(z, minz, maxz))
end
function WireLib.setPos(ent, pos)
@ -1014,10 +1012,12 @@ end)
-- Nan never equals itself, so if the value doesn't equal itself replace it with 0.
function WireLib.clampForce( v )
local x, y, z = v:Unpack()
return Vector(
v[1] == v[1] and math.Clamp( v[1], min_force, max_force ) or 0,
v[2] == v[2] and math.Clamp( v[2], min_force, max_force ) or 0,
v[3] == v[3] and math.Clamp( v[3], min_force, max_force ) or 0
clamp( x, min_force, max_force ),
clamp( y, min_force, max_force ),
clamp( z, min_force, max_force )
)
end
@ -1330,4 +1330,4 @@ local typeIDToStringTable = {
-- Silly function to make printouts more userfriendly.
function WireLib.typeIDToString(typeID)
return typeIDToStringTable[typeID] or "unregistered type"
end
end