ActorGroup2 is a rework of my original module ActorGroup, but heavily optimized to use buffers (as in a table to collect results, not a buffer
), a single BindableEvent for results, callbacks, etc to ensure you can do heavy calculations fast.
Documentation:
Constructor
There is currently only 1 constructor for ActorGroup
s.
-
ActorGroup2.new(module: ModuleScript, count: number)
- This constructor creates a new
ActorGroup
with amodule
(similar to the ParallelScheduler module), and an amount of workers madecount
(so for example ifcount
was 128, 128 workers (Actors) would be made.)
- This constructor creates a new
Methods
-
ActorGroup:BulkWorkAsync(callback: (results: {any}) -> (), assignments: {any})
- This method assigns each value in the
assignments
table to a worker, and callscallback
with the results once it is done.
Note that the number of assignments cannot be more than the number of workers, because as said before, each value is assigned to exactly 1 worker.
- This method assigns each value in the
-
ActorGroup:WorkAsync(callback: (result: {any}) -> (), assignment: any)
-
BulkWorkAsync()
but for a single worker.
-
-
ActorGroup:AwaitBulkWorkAsync(assignments: {any}): {any}?
- Yields until the work is finished, then returns the result (unlike `BulkWorkAsync() which requires a callback)
- Yields until the work is finished, then returns the result (unlike `BulkWorkAsync() which requires a callback)
-
ActorGroup:AwaitWorkAsync(assignment: any): {any}?
- Self explanatory.
-
ActorGroup:BatchAssignments(assignments: {any}, batchSize: number?): {{any}}
- This is a utility method to split an array of assignments into batches based on the number of workers.
Useful if your existing code doesn’t implement batches, but you still want to use them.
- This is a utility method to split an array of assignments into batches based on the number of workers.
-
ActorGroup:Destroy()
- Removes the
ActorGroup
from memory.
- Removes the
ActorGroup Best Practices
For the purposes of doing heavy calculations, it is recommended to make your module able to handle multiple calculations at once.
For example, this is reading a 500x500 stud region of Terrain with this best practice in mind, completed in 0.4s:
And this is the same computation, but this time each worker is assigned a single Region3 to compute:
Not only is it slower, it also requires more workers (because each value in the assignments table passed in :BulkWork() is assigned to exactly 1 worker.)
Reading a 1000x1000 stud region of Terrain completed in 0.5s!
Download ActorGroup2 here →
ActorGroup.rbxm (6.4 KB)
If you have any features to suggest, feel free to post it down in the replies section.