![]() |
VOOZH | about |
dotnet add package ZeroAllocJobScheduler --version 1.1.2
NuGet\Install-Package ZeroAllocJobScheduler -Version 1.1.2
<PackageReference Include="ZeroAllocJobScheduler" Version="1.1.2" />
<PackageVersion Include="ZeroAllocJobScheduler" Version="1.1.2" />Directory.Packages.props
<PackageReference Include="ZeroAllocJobScheduler" />Project file
paket add ZeroAllocJobScheduler --version 1.1.2
#r "nuget: ZeroAllocJobScheduler, 1.1.2"
#:package ZeroAllocJobScheduler@1.1.2
#addin nuget:?package=ZeroAllocJobScheduler&version=1.1.2Install as a Cake Addin
#tool nuget:?package=ZeroAllocJobScheduler&version=1.1.2Install as a Cake Tool
👁 Maintenance
👁 Nuget
👁 License
👁 C#
A high-performance alloc-free C# job scheduler.
Schedules and executes jobs on a set of worker threads with automatic pooling of internal handles.
public class HeavyCalculation : IJob
{
public void Execute()
{
Thread.Sleep(50); // Simulate heavy work
Console.WriteLine("Done");
}
}
// Create a new Scheduler, which you should keep the lifetime of your program. This is the only API call that will allocate or generate garbage.
var scheduler = new JobScheduler(new JobScheduler.Config()
{
// Names the process "MyProgram0", "MyProgram1", etc.
ThreadPrefixName = "MyProgram",
// Automatically chooses threads based on your processor count
ThreadCount = 0,
// The amount of jobs that can exist in the queue at once without the scheduler spontaneously allocating and generating garbage.
// Past this number, the scheduler is no longer Zero-Alloc!
// Higher numbers slightly decrease performance and increase memory consumption, so keep this on the lowest possible end for your application.
MaxExpectedConcurrentJobs = 64,
// Enables or disables strict allocation mode: if more jobs are scheduled at once than MaxExpectedConcurrentJobs, it throws an error.
// Not recommended for production code, but good for debugging allocation issues.
StrictAllocationMode = false,
});
// You need to pool/create jobs by yourself. This will, of course, allocate, so cache and reuse the jobs.
var firstJob = new HeavyCalculation();
var firstHandle = scheduler.Schedule(firstJob); // Schedules job locally
scheduler.Flush(); // Dispatches all scheduled jobs to the worker threads
firstHandle.Complete(); // Blocks the thread until the job is complete.
// Call Dispose at program exit, which shuts down all worker threads
scheduler.Dispose();
To set a sequential dependency on a job, simply pass a created JobHandle to JobScheduler.Schedule(job, dependency).
var handle1 = scheduler.Schedule(job1);
var handle2 = scheduler.Schedule(job2, handle1); // job2 will only begin execution once job1 is complete!
scheduler.Flush();
Use Scheduler.CombineDependencies(JobHandle[] handles) to get a new handle that depends on the handles in parallel. That handle can then be passed into future Schedule call as a dependency itself!
// You must create the array of handles, and handle caching/storage yourself.
JobHandle[] handles = new JobHandle[2];
handles[0] = Scheduler.Schedule(job1);
handles[1] = Scheduler.Schedule(job2);
JobHandle combinedHandle = Scheduler.CombineDependencies(handles); // Combines all handles into the array into one
var dependantHandle = Scheduler.Schedule(job3, combinedHandle); // job3 now depends on job1 and job2.
// job1 and job2 can Complete() in parallel, but job3 can only run once both are complete.
dependantHandle.Complete(); // Blocks the main thread until all three tasks are complete.
Rather than using CombineDependencies(), if you just need to block the main thread until a list of handles are complete, you can use this syntax:
JobHandle.CompleteAll(JobHandle[] handles); // Waits for all JobHandles to finish, and blocks the main thread until they each complete (in any order)
JobHandle.CompleteAll(List<JobHandle> handles);
Or, if you don't want to maintain a list or array, you can just call handle.Complete() on all your handles, in any order.
Instead of IJob, you may extend your class from IJobParallelFor to implement foreach-style indexing on a job. This is useful for when you have very many small operations, and it would be inefficient to schedule a whole job for each one; for example, iterating through a giant set of data.
Define an IJobParallelFor like so:
public class ManyCalculations : IJobParallelFor
{
// Execute will be called for each i for the specified amount
public void Execute(int i)
{
// ... do some operation with i here
}
// Finish will be called once all operations are completed.
public void Finish()
{
Debug.Log("All done!");
}
// BatchSize is a measure of how "complicated" your operations are. Detailed below.
public int BatchSize => 32;
// Restrict the number of spawned jobs to decrease memory usage and overhead. Keep this at 0 to use the Scheduler's number of active threads (recommended).
public int ThreadCount => 0;
}
Run your IJobParallelFor with this syntax:
var job = new ManyCalculations();
var handle = scheduler.Schedule(job, 512); // Execute will be called 512 times
However, there are several caveats:
IJobParallelFor. In general, over-parallelization is a bad thing. Only schedule your job in parallel if it is truly iterating a huge amount of times, and make sure to always profile when dealing with multithreaded code.BatchSize for the work inside your job. If you have very many small tasks that complete very quickly, a higher batch size will dispatch more indices to each thread at once, minimizing scheduler overhead. On the other hand, if you have complicated (or mixed-complexity) tasks, a smaller batch size will maximize the ability for threads to use work-stealing and thus might complete faster. The only way to know what batch size to use is to profile your code and see what's faster!IJobParallelFor actually schedules ThreadCount jobs on the backend, decreasing the jobs pool. If you make a lot of these, the amount of jobs in play could quickly increase. For example, on a 16-core CPU, with default settings, spawning 8 IJobParallelFor would spawn 128 jobs on the backend. The scheduler can certainly handle it, but you'll probably want to keep an eye on MaxExpectedCurrentJobs if you want to keep the scheduler truly zero-allocation.| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on ZeroAllocJobScheduler:
| Package | Downloads |
|---|---|
|
Arch
A high performance c# net.7 and net.8 archetype based ECS ( Entity component system ). |
|
|
LayerBase
高性能一体化游戏架构框架。集成模块化层级、轻量级 DI、异步任务(LBTask)与超极速事件总线。专为 Godot/Unity 优化。 |
|
|
Pixillery.Core
The core library for the Pixillery game engine which all other Pixillery engine packages depend on. |
|
|
ABEngine.Arch
A high performance c# net.6 and net.7 archetype based ECS ( Entity component system ). |
|
|
ZeroGdk.Server
A high-performance C# library that powers the server side of the ZeroGdk framework, offering advanced connection management, an Arch-based ECS, and real-time entity networking for scalable multiplayer game servers. |
Showing the top 2 popular GitHub repositories that depend on ZeroAllocJobScheduler:
| Repository | Stars |
|---|---|
|
genaray/Arch
A high-performance C# based Archetype & Chunks Entity Component System (ECS) with optional multithreading.
|
|
|
annulusgames/Arch.Unity
Arch ECS integration for Unity.
|
Added dependencies.
Several improvements and bug fixes.