From 92815ad54b23fe92742ebca7263bb227149248c1 Mon Sep 17 00:00:00 2001 From: Wouters Dorian Date: Sun, 9 Nov 2014 10:25:39 +0100 Subject: [PATCH] Improved VoxelArea variable locality, thus performance Signed-off-by: Craig Robbins --- builtin/game/voxelarea.lua | 42 ++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/builtin/game/voxelarea.lua b/builtin/game/voxelarea.lua index 93bbf73a8..6d926c940 100644 --- a/builtin/game/voxelarea.lua +++ b/builtin/game/voxelarea.lua @@ -18,10 +18,11 @@ function VoxelArea:new(o) end function VoxelArea:getExtent() + local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge return { - x = self.MaxEdge.x - self.MinEdge.x + 1, - y = self.MaxEdge.y - self.MinEdge.y + 1, - z = self.MaxEdge.z - self.MinEdge.z + 1, + x = MaxEdge.x - MinEdge.x + 1, + y = MaxEdge.y - MinEdge.y + 1, + z = MaxEdge.z - MinEdge.z + 1, } end @@ -31,45 +32,50 @@ function VoxelArea:getVolume() end function VoxelArea:index(x, y, z) - local i = (z - self.MinEdge.z) * self.zstride + - (y - self.MinEdge.y) * self.ystride + - (x - self.MinEdge.x) + 1 + local MinEdge = self.MinEdge + local i = (z - MinEdge.z) * self.zstride + + (y - MinEdge.y) * self.ystride + + (x - MinEdge.x) + 1 return math.floor(i) end function VoxelArea:indexp(p) - local i = (p.z - self.MinEdge.z) * self.zstride + - (p.y - self.MinEdge.y) * self.ystride + - (p.x - self.MinEdge.x) + 1 + local MinEdge = self.MinEdge + local i = (p.z - MinEdge.z) * self.zstride + + (p.y - MinEdge.y) * self.ystride + + (p.x - MinEdge.x) + 1 return math.floor(i) end function VoxelArea:position(i) local p = {} + local MinEdge = self.MinEdge i = i - 1 - p.z = math.floor(i / self.zstride) + self.MinEdge.z + p.z = math.floor(i / self.zstride) + MinEdge.z i = i % self.zstride - p.y = math.floor(i / self.ystride) + self.MinEdge.y + p.y = math.floor(i / self.ystride) + MinEdge.y i = i % self.ystride - p.x = math.floor(i) + self.MinEdge.x + p.x = math.floor(i) + MinEdge.x return p end function VoxelArea:contains(x, y, z) - return (x >= self.MinEdge.x) and (x <= self.MaxEdge.x) and - (y >= self.MinEdge.y) and (y <= self.MaxEdge.y) and - (z >= self.MinEdge.z) and (z <= self.MaxEdge.z) + local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge + return (x >= MinEdge.x) and (x <= MaxEdge.x) and + (y >= MinEdge.y) and (y <= MaxEdge.y) and + (z >= MinEdge.z) and (z <= MaxEdge.z) end function VoxelArea:containsp(p) - return (p.x >= self.MinEdge.x) and (p.x <= self.MaxEdge.x) and - (p.y >= self.MinEdge.y) and (p.y <= self.MaxEdge.y) and - (p.z >= self.MinEdge.z) and (p.z <= self.MaxEdge.z) + local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge + return (p.x >= MinEdge.x) and (p.x <= MaxEdge.x) and + (p.y >= MinEdge.y) and (p.y <= MaxEdge.y) and + (p.z >= MinEdge.z) and (p.z <= MaxEdge.z) end function VoxelArea:containsi(i) -- 2.25.1