Why use parallel luau?

I understand how to use parallel lua now, but I just don’t see the use of it.

Yes, I have read the documentation. Apparently parallel lua can improve performance because you can run threads at the same time.

My question is, how does this exactly improve performance??

For example, I made a topic long ago asking for the use of task.spawn (and coroutines). I found the use of it later, I did need it in a loading icon for my game, but it didn’t really “improve” performance. It just allowed me to perform multiple tasks whilst doing something else in the same script.

task.spawn/coroutines is basically parallel luau except they don’t actually run code at the EXACT same time.

And neither of them have actually improved performance, they have their uses. But roblox is implying that you can actually increase FPS and what not by using parallel luau.

I am still fairly new to using it so could someone please explain?

3 Likes

i donot know but i think that task.spawn()/coroutines spawn virtual threads so it doesnot improve performance but using RealMultiThreading in Actors improves performance because they use real threads rather than not using them

its like you have 2 hands/2threads it would be faster to use both of them rather than one

i may be wrong that task.spawn()/coroutines spawn virtual threads

finally task.spawn()/coroutines spawn virtual threads in the main threads and it donot use a new threads if you want to do RealMultiThreading you should check Actors

2 Likes

Ah yes, they do spawn virtual threads. Problem is though, using 2 hands is yes more efficient. But I mean in terms of roblox PERFORMANCE. Like the performance of the actual game, such as frame rates.

Roblox implies that apparently those will also be boosted, but how is that possible? I don’t get it to be honest.

1 Like

You’re splitting the task between multiple cores; one core doesn’t need to do all the work

1 Like

task.spawn and coroutines aren’t parallel Luau. They’re just separate threads on the same cpu core. Using them does not increase performance.

Parallel computing is used when you can split semi-intensive repetitive tasks that would otherwise run in serial in parallel. For example, instead of loading 8 Minecraft chunks on 1 core, you can load 1 chunk per core in 8 cores as all the chunk calculations follow the same logic. This way, one core isn’t taking all the load and is rather distributed throughout different cores.

7 Likes

So you do half a task in one thread, and the other half in another?

I don’t think that’s how most people (including me) would use it but yeah that’s correct I think. But I would usually do one seperate task in one thread, and then another seperate tasks in another thread with parallel luau if I was using it.

@VegetationBush explains it better.

Taking his Minecraft example, if Minecraft loaded your entire world on one core, it would take a trumendous amount of time, energy, and processing power to render everything in the game and get everything working.

Minecraft loads chunks on multiple cores, which is why multiple chunks can load in at the same time. If Minecraft used one core, you’d have to wait for every single chunk to load before another one could begin. Doing that would decrease performance

1 Like

Parallel scripting utilizes actors to spread tasks across separate cores instead of all tasks being run on all cores. For example if you are using 2 actors and your device has 8 cores, 4 cores will focus the tasks in one actor while the other 4 focus on the rest.

This is especially useful when you are dealing with performance demanding code (for example, a run service event that does intensive calculations and method calls every frame). You don’t want those demanding pieces of code to slow everything else down so you use parallel scripting to separate them into another actor to allow other cores to prioritize it separately.

I feel like everybody in this thread explained what parallel lua is so I’ll just recap.
Parallel Lua is simply just running multiple threads at once (unlike coroutines or task.spawn which both give the illusion of running on different threads).

To use parallel lua, you have to run scripts under different actors and use task.desynchronize in order to run in parallel. When you want to run back in serial, you could use task.desyncronize.

The most common use cases are for heavy calculations (like the person above me said), terrain generation, and NPC’s.
Using parallel lua in these circumstances increase efficiency since you are using multiple cores to do different tasks instead of all of them doing one thing. Like imagine 3 people building a house in Minecraft. Instead of multiple people working on the same part of the house, they all work on different sides to increase efficiency.

Now with how enticing using parallel lua seems, you may wonder why people don’t use it more often. Well it only boils down to two reasons (that I know of). First, it may be difficult for older devices to keep up, which isn’t that huge of a problem in the general scene of things, but the main problem is reading and writing data in parallel which is why Roblox puts some restrictions on parallel lua such as “Read Parallel” which prevents you from setting certain data while in parallel.

If you have a single core doing too much work, it can end up taking longer than the length of 1 frame to complete, which forces the duration of the frame to increase, which is lag. If you have multiple cores sharing the workload, they can finish it faster, ideally before the next frame is supposed to begin.

This document explains it better Parallel Luau | Documentation - Roblox Creator Hub

1 Like

Alot of people have explained to me well now how to use parallel luau and I see that it may be effective to run things on multiple cores, however one core is already huge. It’s very unlikely you’d ever get to a point where you’re running so many expensive operations on a single core that you’d need to switch between mutliple ones.

Games before parallel luau came out ran on a single core and had many expensive operations, and they all ran just fine.

I appreciate the minecraft/NPC examples though.

How many cores are there exactly though?

This usually depends on the system that’s running it. My CPU has 24 cores, so depending on how the system is programmed it could balance the work between all of those. My other computer only has 4 cores though so would only be able to distribute between that. The servers they use though I’m unsure of (and servers often get split up), and the program and the OS configuration (both of which I don’t know) makes this impossible to answer directly. The way this system schedules work though, will speed it up where it can and would run in sequence if it can’t. Roblox could give a specific server number if they always run the same hardware (and configuration), but that would probably be super liable to change depending on how they deploy servers.

Tldr; it’s dynamic.

I mean on the server-side of things or does Roblox not disclose that?