Parallel script performance

I would like to know if this body leaning script I made is well optimized for what it is. You can do it by rating the script from 1 to 10

local task_sync = task.synchronize
local math_atan2 = math.atan2
local math_cos = math.cos
local math_sin = math.sin
local CFrame_fromEulerAnglesYXZ = CFrame.fromEulerAnglesYXZ

local HumanoidRootPart:BasePart = script.Parent.Parent:WaitForChild("HumanoidRootPart")
local RootJoint = HumanoidRootPart:WaitForChild("RootJoint")
local BaseCFrame:CFrame = RootJoint.C0
local AssemblyLinearVelocity = HumanoidRootPart.AssemblyLinearVelocity
local HRPCFrameLookVector = HumanoidRootPart.CFrame.LookVector
local Transformed = BaseCFrame*CFrame_fromEulerAnglesYXZ(0,0,0)
local LocalLinearVelocity = 0
local LVX,LVZ = 0,0
local Magnitude = 0

game:GetService("RunService").Heartbeat:ConnectParallel(function(DT)
	AssemblyLinearVelocity = HumanoidRootPart.AssemblyLinearVelocity
	Magnitude = AssemblyLinearVelocity.Magnitude
	if Magnitude > 1 then
		HRPCFrameLookVector = HumanoidRootPart.CFrame.LookVector
		LocalLinearVelocity = math_atan2(HRPCFrameLookVector.Z,HRPCFrameLookVector.X)-math_atan2(AssemblyLinearVelocity.Z,AssemblyLinearVelocity.X)
		LVX,LVZ = math_cos(LocalLinearVelocity),math_sin(LocalLinearVelocity)
		Magnitude *= 0.01
		Transformed = Transformed:Lerp(BaseCFrame*CFrame_fromEulerAnglesYXZ(LVX*Magnitude,LVZ*Magnitude,0),DT*10) 
		task_sync()
		RootJoint.C0 = Transformed 
	else
		Transformed = Transformed:Lerp(BaseCFrame,DT*10)
		task_sync()
		RootJoint.C0 = Transformed 
	end
end)

2 Likes

I like it, nice job. You’ve done a great deal to optimize this.

1 Like

Is this lua :shock: :shock: :shock: im traumitized

1 Like

using all the trig probably isn’t neccesary, and it’s probably slower too, using the dot product would be better for calculating the LocalLinearVelocity

LVX = HRPCFrame.RightVector:Dot(AssemblyLinearVelocity)
LVZ = HRPCFrame.LookVector:Dot(AssemblyLinearVelocity)

haven’t tested this code, and my memory is sorta foggy, but i’m fairly confident this achieves the same result w/o any trig

1 Like

I am unsure if using parallel lua in this case is actually faster than running the code in serial. Parallel lua introduces a good amount of overhead, and your script might be faster in serial because of that. Parallel lua is mainly useful when doing a lot of WorldRoots, raycasts, or like giant tables you are serializing/deserializing.
One other thing to note is that changing the properties of objects (in your case RootJoint.C0) is surprisingly not that fast and that operation alone could be slower than the math you are doing (though I would have to test it be sure).

To test the performance, you can do:

game:GetService("RunService").Heartbeat:ConnectParallel(function(DT)
	local StartTime = os.clock()

	AssemblyLinearVelocity = HumanoidRootPart.AssemblyLinearVelocity
	Magnitude = AssemblyLinearVelocity.Magnitude
	if Magnitude > 1 then
		HRPCFrameLookVector = HumanoidRootPart.CFrame.LookVector
		LocalLinearVelocity = math_atan2(HRPCFrameLookVector.Z,HRPCFrameLookVector.X)-math_atan2(AssemblyLinearVelocity.Z,AssemblyLinearVelocity.X)
		LVX,LVZ = math_cos(LocalLinearVelocity),math_sin(LocalLinearVelocity)
		Magnitude *= 0.01
		Transformed = Transformed:Lerp(BaseCFrame*CFrame_fromEulerAnglesYXZ(LVX*Magnitude,LVZ*Magnitude,0),DT*10) 
		task_sync()
		RootJoint.C0 = Transformed 
	else
		Transformed = Transformed:Lerp(BaseCFrame,DT*10)
		task_sync()
		RootJoint.C0 = Transformed 
	end

	print(os.clock() - StartTime)
end)

This will give you how many seconds it takes to run the code
To test it in serial, replace the ConnectParallel with Connect, and remove the task.sync()

How many instances of this script are running?

2 Likes

I never thought syncing took that much time.
It only took 0.000009 seconds to compute and 0.000145 seconds to sync. Thanks for telling me, mate

1 Like

How many instances of that script are you running?

I realized the code I provided to check how much time it takes doesn’t really work when having multiple instances of the code running (it will print multiple times). To effectively measure how much time it takes you’d need to run all the instances from a for loop (in serial) or in parallel have some sort of callback when all the parallel tasks are done. It’s a lot easier to just look in the micro profiler

If there is only 1 instances of that script (and 1 actor) then parallel lua isn’t effective since it can only use 1 cpu thread, unless you have other code that is also running under an actor

1 Like

I’m mostly separating a bunch of code between actors; one actor usually has 5-7 scripts. Also, consider the fact that the more players in the game, the more code of such will be here

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.