Setting one parts size being more expensive than the other

somehow, setting the size of the WhiteCylinder is like, 25x more expensive than setting BlackCylinders size, when it has less calculations to do

and its not even the calculations its straight up setting the size itself
so this really confuses me

image

Full script
--!optimize 2
repeat task.wait() until script.Parent.Enabled

local RS = game:GetService("RunService")
local mainscript = script.Parent
local Event = mainscript:FindFirstChild("Event")
local Query = mainscript:FindFirstChild("Query")
local whiteCylinder
local blackCylinder
local sound
local analyzer
local root
local listener
local out
local cam = workspace.CurrentCamera
local baseFOV = cam.FieldOfView
local currentFOV = cam.FieldOfView
local eventconnection
local renderconnection

local v3 = Vector3
local v3n = Vector3.new

local baseWhite = v3n(0.3,2,2)
local baseBlack = v3n(0.2,3,3)

local profilebegin, profileend = debug.profilebegin, debug.profileend

local min, max = math.min, math.max

local SETTINGS = {
	VOLUME = 0.75,
	BOOSTER = 1,
	RANGES = {0, 550, 725},
	COLORS = {
		Color3.fromRGB(0,0,0),
		Color3.fromRGB(80, 0, 255),
		Color3.fromRGB(100, 90, 255)
	},
	FOV_BOBBING = true,
	MODE = "LEGACY"
}

SETTINGS.MODE=mainscript:GetAttribute("MODE")
SETTINGS.MIN_RANGE = mainscript:GetAttribute("MIN_RANGE")
SETTINGS.MAX_RANGE = mainscript:GetAttribute("MAX_RANGE")

SETTINGS = Query:InvokeServer()

function clerp(cur, new, alpha)
	return cur + (new - cur) * alpha
end

local function generateDistanceCurve(minDist: number, maxDist: number, steps: number?)
	local step: number = tonumber(steps) or 20
	local curve = {}
	local range = maxDist - minDist
	for i = 0, step do
		local t = i / step
		local distance = minDist + t * range
		local value = (1 - t)^2
		curve[distance] = value
	end
	return curve
end

local function wireUp(source: Instance, target: Instance): Wire
	local wire = Instance.new("Wire")
	wire.Parent = target
	wire.SourceInstance = source
	wire.TargetInstance = target
	return wire
end

function find(parent: Instance, name: string, kind: string)
	local obj
	repeat
		for _,v in pairs(parent:GetChildren()) do
			if v.Name == name and v:IsA(kind) then
				obj = v
			end
		end
		task.wait()
	until obj
	return obj
end

function init()
	if not cam:FindFirstChild("AUDIO_LISTENER") then
		listener = Instance.new("AudioListener")
		listener.Parent = cam
		listener.Name = "AUDIO_LISTENER"
		listener:SetDistanceAttenuation(generateDistanceCurve(SETTINGS.MIN_RANGE, SETTINGS.MAX_RANGE))
	end

	if not cam:FindFirstChild("AUDIO_OUT") then
		out = Instance.new("AudioDeviceOutput")
		out.Parent = cam
		out.Name = "AUDIO_OUT"
	end

	wireUp(listener, out)
end

init()

local lastWhiteSize = v3n(0,0,0)
local lastBlackSize = v3n(0,0,0)
local lastWhiteColor = Color3.new(0,0,0)
local SIZE_EPSILON = 0.01
local COLOR_EPSILON = 0.01

eventconnection = Event.OnClientEvent:Connect(function(kind, args)
	if kind == "SettingsUpdate" then
		SETTINGS = args
		if listener then
			listener:SetDistanceAttenuation(generateDistanceCurve(SETTINGS.MIN_RANGE, SETTINGS.MAX_RANGE))
		end
	end
end)

renderconnection = RS.RenderStepped:Connect(function()
	profilebegin("FindMissingReferences")
	if not whiteCylinder then
		whiteCylinder = find(mainscript, "WhiteCylinder", "BasePart")
	end
	if not blackCylinder then
		blackCylinder = find(mainscript, "BlackCylinder", "BasePart")
	end
	if not root then
		root = find(mainscript.Parent, "HumanoidRootPart", "BasePart")
	end
	if not sound then
		if SETTINGS.MODE == "LEGACY" then
			sound = find(root, "AUDIO", "Sound")
		elseif SETTINGS.MODE == "NEW" then
			sound = find(root, "AUDIO", "AudioPlayer")
		end
	end
	if not analyzer and sound and SETTINGS.MODE=="NEW" then
		analyzer = find(sound, "AUDIO_ANALYZER", "AudioAnalyzer")
	end
	profileend()

	if sound and root and blackCylinder and whiteCylinder and sound.IsPlaying then
		profilebegin("CalculateLoudness")
		local peak = SETTINGS.MODE=="NEW" and (analyzer.PeakLevel * 1000 * 0.75 / sound.Volume) or sound.PlaybackLoudness
		local loudness = peak / 50 * SETTINGS.BOOSTER
		profileend()

		profilebegin("ResizeCylinders")

		profilebegin("CalculateAlphaAndLerp")
		local alpha = min(loudness / 87.5, 1)
		local whitesize = whiteCylinder.Size
		local blacksize = blackCylinder.Size

		local targetWhite = v3n(baseWhite.X, baseWhite.Y + loudness, baseWhite.Z + loudness)
		local targetBlack = v3n(baseBlack.X, baseBlack.Y + loudness, baseBlack.Z + loudness) * 1.065

		whitesize = clerp(whitesize, targetWhite, alpha)
		blacksize = clerp(blacksize, targetBlack, alpha / 3)
		profileend()

		profilebegin("ApplySizesAndClampBlack")

		profilebegin("Setting White Size")
		local whiteDiff = whitesize - lastWhiteSize
		local whiteDiffSq = whiteDiff.X*whiteDiff.X + whiteDiff.Y*whiteDiff.Y + whiteDiff.Z*whiteDiff.Z
		if whiteDiffSq > SIZE_EPSILON*SIZE_EPSILON then
			whiteCylinder.Size = whitesize
			lastWhiteSize = whitesize
		end
		profileend()

		profilebegin("Setting Black Size")
		local blackY = max(blacksize.Y, lastWhiteSize.Y + 1)
		local blackZ = max(blacksize.Z, lastWhiteSize.Z + 1)
		local blackFinal = v3n(blacksize.X, blackY, blackZ)
		local blackDiff = blackFinal - lastBlackSize
		local blackDiffSq = blackDiff.X*blackDiff.X + blackDiff.Y*blackDiff.Y + blackDiff.Z*blackDiff.Z
		if blackDiffSq > SIZE_EPSILON*SIZE_EPSILON then
			blackCylinder.Size = blackFinal
			lastBlackSize = blackFinal
		end
		profileend()

		profileend()
		
		profileend()

		profilebegin("UpdateColor")
		local pl = loudness * 50
		local color = SETTINGS.COLORS[#SETTINGS.COLORS]
		for i = 1, #SETTINGS.RANGES - 1 do
			local low, high = SETTINGS.RANGES[i], SETTINGS.RANGES[i + 1]
			if pl >= low and pl <= high then
				local t = (pl - low) / (high - low)
				color = SETTINGS.COLORS[i]:Lerp(SETTINGS.COLORS[i + 1], t)
				break
			end
		end
		local dr = color.R - lastWhiteColor.R
		local dg = color.G - lastWhiteColor.G
		local db = color.B - lastWhiteColor.B
		if dr*dr + dg*dg + db*db > COLOR_EPSILON*COLOR_EPSILON then
			whiteCylinder.Color = color
			lastWhiteColor = color
		end
		profileend()

		profilebegin("FOVBobbing")
		if SETTINGS.FOV_BOBBING then
			local distance = (cam.CFrame.Position - root.Position).Magnitude
			local factor = ((loudness / 7.5) ^ 2)
			local t = (distance - SETTINGS.MIN_RANGE) / (SETTINGS.MAX_RANGE - SETTINGS.MIN_RANGE)
			t = math.min(t, 1)
			local targetFOV = baseFOV - factor * (1 - t)
			currentFOV = currentFOV + (targetFOV - currentFOV) * (alpha*1.2)
			cam.FieldOfView = currentFOV
		end
		profileend()
	end
end)

script.Destroying:Connect(function()
	RS = nil
	Event = nil
	whiteCylinder = nil
	blackCylinder = nil
	sound = nil
	analyzer = nil
	root = nil
	cam = nil
	baseFOV = nil
	eventconnection:Disconnect()
	renderconnection:Disconnect()
	baseWhite = nil
	baseBlack = nil
	min, max = nil, nil
	table.clear(SETTINGS)
end)

Because pieces of code are not the same?


:brain: moment.

Try manually counting the time spent setting white and black size via the os.clock() function.

It can measure down to 100 ns. Find the time both sections take to run. Maybe the if statement in the white one runs more often.

how does it run more often if its ran once per frame…
Your logic is not making any sense here

Test how long it takes to run. Simple first step.


Yeah somehow white is taking significantly longer in most scenarios

Yeah for some reason white is WAY SLOWER. Make it so the if statement is just always false so it doesn’t run. I want to narrow down stuff.

Even setting whites size directly with no if statement was already significantly slower
so its quite literally setting the size itself that takes that long (as seen from the micro profiler)

So get rid of the if statement from both black and white and see the time difference.

I don’t think you read that correctly

I said that even without the if statement and extra calculations (so setting the size directly) still took significantly longer
How do i know this? I tested via the micro profiler.

I understand. I think the time difference is that the if statement happens to be true for white more often than for black. In the times the if statement for white is false, then it’s time is much lower, closer to black.

Anyways, the time white takes in its worse case(100 microseconds) would let you run at 10,000 FPS anyway, no worries.

Are the properties of both cylinders the same?

Yes
Both are created from the same function

Not what I am saying. And why are you being rude? I am helping you.

What I am saying is that each frame, it’s more likely that the white cylinder has it’s size changed than the black cylinder.

Try commenting out the whiteCylinder.Size update. Also, comment out the dependency lines for blackY and blackZ and just let blackCylinder resize on its own.
check the performance. You will likely see the cost of setting blackCylinder.Size skyrocket

Ill try this tomorrow since its already late for me and i have shut off my computer

But i dont think anything would change for BlackCylinder since BlackY and BlackZ are to ensure BlackCylinder is always 1 stud wider than WhiteCylinder
so that is for cosmetic purposes

Did you figure out what was causing the slowdown of the white cylinder resizing?

No
I unfortunately did not find out