Originally Posted by merlight
I tried to explain that there's no reason to compute the root if you only need it for comparison against some distance threshold. Additionally, each sub-expression should only be computed once -- it's both more efficient and readable.
Like this function, it does way too many avoidable table lookups and an unnecessary sqrt:
Lua Code:
local function IsCoordinateInRange(x, y) if not FyrMM.currentMap.TrueMapSize or not FyrMM.currentMap.PlayerNX or not FyrMM.SV.CustomPinViewRange or not FyrMM.SV.ViewRangeFiltering then return true end return FyrMM.currentMap.TrueMapSize * math.sqrt((x-FyrMM.currentMap.PlayerNX)*(x-FyrMM.currentMap.PlayerNX)+(y-FyrMM.currentMap.PlayerNY)*(y-FyrMM.currentMap.PlayerNY)) <= FyrMM.SV.CustomPinViewRange end
Lua Code:
-- this is used so heavily that I'd localize it at the file level local g_currentMap = FyrMM.currentMap local function IsCoordinateInRange(x, y) local mapSize = g_currentMap.TrueMapSize local nx = g_currentMap.PlayerNX local pinViewRange = FyrMM.SV.CustomPinViewRange if not (mapSize and nx and pinViewRange) then return true end local dx = x - nx local dy = y - g_currentMap.PlayerNY return (dx * dx + dy * dy) * mapSize * mapSize <= pinViewRange * pinViewRange end
|
It is a very fast changing environment, btw this particular function works fine no difference between live and pts. I made heavy changes during past week meantime testing it against pts and speeds are looking acceptable. Though it runs much smoother on live.
PS I also have noticed a faster garbage collection growth while using local variables. This is very worrying.