Further optimization?

How would i further optimize this module script?

local RIGHT = Vector3.new(1, 0, 0)
local UP 	= Vector3.new(0, 1, 0)
local BACK 	= Vector3.new(0, 0, 1)
local PartCache = require(game.ServerScriptService:WaitForChild("Cache"))

--

local function vec3Func(f, v)
	return Vector3.new(f(v.x), f(v.y), f(v.z))
end

local function slice(part, wPoint, axis)
	local pSize, pCF = part.Size, part.CFrame
	local pRot = pCF - pCF.p
	
	local length = axis:Dot(pSize)
	local mSize = pSize - axis*length
	local max, min = length/2, -length/2
	local split = axis:Dot(pCF:PointToObjectSpace(wPoint))
	
	if (split >= min and split <= max) then
		local l1, l2 = max - split, split - min
		local p1, p2 = pCF*(axis*(l1/2 + split)), pCF*(axis*(-l2/2 + split))
		
		local part1 = nil
		local part2 = nil
		
		local Size1 = mSize + axis*l1
		local Size2 = mSize + axis*l2
		if Size1.X < 0.5 or Size1.Y < 0.5 or Size1.Z < 0.5 then
			else
			part1 = part
			part1.CFrame = CFrame.new(p1) * pRot
			part1.Size = Size1
		end
		
		if Size2.X < 0.5 or Size2.Y < 0.5 or Size2.Z < 0.5 then
		elseif part1 ~= nil then
			part2 = part:Clone()
			part2.CFrame = CFrame.new(p2) * pRot
			part2.Size = Size2
			part2.Parent = part1.Parent
		end
		
		return part1, part2
	end
	
	return part
end

local function cut(part, canvas)
	local cf, size2 = part.CFrame, part.Size/2
	local canvasCF = canvas[1].CFrame
	
	-- slice up the canvas
	for _, enum in next, Enum.Axis:GetEnumItems() do
		local axis = Vector3.FromAxis(enum)
		local p1, p2 = cf*(axis*size2), cf*(-axis*size2)
		
		local sliceAxis = canvasCF:VectorToObjectSpace(cf:VectorToWorldSpace(axis))
		sliceAxis = vec3Func(function(c) return math.min(1, math.abs(c)) end, sliceAxis)
		
		for i = 1, #canvas do
			local a, b = slice(canvas[i], p1, sliceAxis)
			if a then
				canvas[#canvas+1] = a
			end
			if b then
				canvas[#canvas+1] = b
			end
		end
		
		for i = 1, #canvas do
			local a, b = slice(canvas[i], p2, sliceAxis)
			if a then
				canvas[#canvas+1] = a
			end
			if b then
				canvas[#canvas+1] = b
			end
		end
	end
	
	-- remove the canvas slices that overlap with the part
	local lookup = {}
	for i,v in pairs(canvas) do
		lookup[canvas[i]] = true
		--if v.Size.X < 0.5 or v.Size.Y < 0.5 or v.Size.Z < 0.5 then
			--v:Destroy()
		--end
	end
	
	part.Size /= Vector3.new(1.5,1.5,1.5)
	local t = part.Touched:Connect(function() end)
	local touching = part:GetTouchingParts()
	part.Size *= Vector3.new(1.5,1.5,1.5)
	t:Disconnect()
	for i = 1, #touching do
		if (lookup[touching[i]]) then
			PartCache:StorePart(touching[i])
		end
	end

	-- return the canvas for further cuting (?)
	return canvas
end

--

return cut

(Someone please fact-check me)

1) Make constants! Stop creating new Vector3 values if they’re all the same.

part.Size *= Vector3.new(1.5,1.5,1.5) --creating a new Vector3 value every single time you change the size

Do what you just did at the top of the script where you aliased common Vectors to variables (right, up, and back vector values)

--these
local RIGHT = Vector3.new(1, 0, 0)
local UP 	= Vector3.new(0, 1, 0)
local BACK 	= Vector3.new(0, 0, 1)

2) Try not to redundantly create new functions. If it’s going to be used multiple times, make it a permanent function.

--new function being created every time this line is ran
sliceAxis = vec3Func(function(c) return math.min(1, math.abs(c)) end, sliceAxis)

3) Although it probably won’t have any impact on improving performance, it is a good practice to alias common globals such as math functions, CFrame, or Vector3 to new variables.

--call these instead of the full function name. It will also make your life easier
local v3 = Vector3.new
local cf = CFrame.new

local foo = v3(1, 2, 3)
--snippet below automatically turns all of the math functions into local variables
local env = getfenv()
for k, v in pairs(math) do
  env[k] = v
end

print(abs(-5))
print(rad(50))

4) Reduce the amount of math.

Try not to repeat mathematical expressions. If you already multiplied a variable by something, reuse the result instead of recalculating it. For example:

local p1, p2 = cf*(axis*size2), cf*(-axis*size2)

--can be rewritten as
local axisS2 = axis*size2
local p1, p2 = cf*axisS2, cf*-axisS2

Additional note: Look around the forum! Luau heavily focuses on optimizations and there are drastic changes compared to native Lua. Native functions might’ve been rewritten, making them faster than others. For example, is ipairs better than pairs for iterating arrays? What about table.insert over t[#t + 1]? Do your own research; it’s like coding in a different language because it is.

3 Likes