Skip to content

Commit

Permalink
WireLib.clampPos/Force optimization (#3221)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Astralcircle authored Dec 16, 2024
1 parent 80c4eb0 commit a14a2c7
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lua/wire/wireshared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit a14a2c7

Please sign in to comment.