Safe to run task.synchronize/desynchronize in renderstep?

I have been working on implementing parallel Luau into some parts of my game and I figured the best place to start would be the RenderStepped loop that runs during the main gameplay portion of my experience. However, while researching I discovered that task.desyncronize() and task.synchronize() yield for a brief moment of time to allow for the switch to parallel/serial execution.

My main question is, is it safe to call these functions during a RenderStepped loop (since I know it’s generally a bad idea to yield in a RunService loop, plus it’s been said that the switch between execution modes is relatively expensive), and if so, would it even be beneficial to me in my case with how often I would be switching between parallel and serial?

Here is an example of what my code is doing for anyone curious:

--this all runs inside of a RenderStepped loop
task.desynchronize()
			
--do various CFrame calculations, raycasts, other math stuff for joint positioning, character rotation, camera offset, aim assist, etc
	
task.synchronize()
	
--set camera CFrame, joint C0s, position character, etc
1 Like

No, don’t do that. This will take a great amount of yielding and more memory usage. If this is your core gameplay and you are trying to run it parallel, well, don’t. You’d be better off running it in the main thread. But, if you really want to, you can use :ConnectParallel() and then task.synchronize(). It’s generally better to use parallel for heavy tasks, and if you have such a heavy task on RenderStepped it needs a lot of optimising…

Basically, it’s likely that the cons of parallel execution greatly outweigh the pros in this situation.

Yielding in an event callback will make the thread ineligible for reuse, which will pretty much kill any performance gain.

If you really want to desync, you’ll have to wrap it in a while loop.

task.spawn(function()
	while true do
		local DeltaTime = RunService.PreRender:Wait()
		
		task.desynchronize()
		-- stuff
		task.synchronize()
		-- more stuff
	end
end)
1 Like

I gotcha, that definitely makes sense. I am not experiencing any performance issues from my main RenderStepped loop but I had been reading up on the parallel Luau stuff and figured I would try to implement it, but honestly I don’t think anything in my project is doing enough heavy calculations to make good use of it for now. Thanks for the advice!

1 Like

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