ZeroAllocJobScheduler 2.0.0
See the version list below for details.
dotnet add package ZeroAllocJobScheduler --version 2.0.0
NuGet\Install-Package ZeroAllocJobScheduler -Version 2.0.0
<PackageReference Include="ZeroAllocJobScheduler" Version="2.0.0" />
<PackageVersion Include="ZeroAllocJobScheduler" Version="2.0.0" />
<PackageReference Include="ZeroAllocJobScheduler" />
paket add ZeroAllocJobScheduler --version 2.0.0
#r "nuget: ZeroAllocJobScheduler, 2.0.0"
#:package ZeroAllocJobScheduler@2.0.0
#addin nuget:?package=ZeroAllocJobScheduler&version=2.0.0
#tool nuget:?package=ZeroAllocJobScheduler&version=2.0.0
A high-performance, allocation-free C# job scheduler for game development and data-oriented programming. It runs the parallel work of Arch, the ECS, but fits any game loop.
- ๐๏ธ FAST > Work stealing, lock-free queues and cache-friendly data. A small job costs a fraction of a microsecond to schedule and finish!
- โป๏ธ ZERO ALLOC > Scheduling, waiting, dependencies and parallel-for do not create garbage, and tests make sure it stays that way.
- ๐ด POLITE > Idle workers sleep, so your game keeps its CPU for rendering, audio and the rest. No spinning cores!
- ๐ค BARE MINIMUM > Three calls to remember:
Schedule,Flush,Wait. Not bloated, no hidden costs. - ๐งฉ COMPLETE > Dependencies, child jobs, parallel-for, SIMD batches, cancellation and exception handling are included.
- ๐ข SUPPORT > Targets .NetStandard 2.1, so it runs on .Net 6 and 8 and may be used with Unity or Godot!
Download the package and get started today!
dotnet add PROJECT package ZeroAllocJobScheduler --version 2.0.0
โฉ Quickstart
Let's say you want to run some work on all your cores... sounds complicated? It's not! Create a scheduler, schedule a job, flush it and wait for it.
using Schedulers;
// A job is a class or a struct
public struct HelloJob : IJob
{
public void Execute() => Console.WriteLine("Hello from a worker!");
}
using var scheduler = new JobScheduler(); // one worker per processor
var handle = scheduler.Schedule(new HelloJob()); // prepares the job
scheduler.Flush(handle); // hands it to the workers
scheduler.Wait(handle); // returns when it is done, and helps working meanwhile
Keep one scheduler for the lifetime of your program. Creating it is the only part that allocates. Jobs can be structs or classes.
๐ก Highlights
That is all you need to start. If you want to know more about the techniques under the hood, read how it works. There's more to explore, for example...
๐ค Our promise
- Bare minimum - No overengineering, no hidden costs
- Incredibly fast and allocation free
- No busy waiting, an idle scheduler uses no CPU
- Every feature is tested, including stress tests for the threading code
๐ Features
- Jobs as classes or as structs, without boxing
- Dependencies: a job can wait for any number of other jobs
- Child jobs: wait for a parent and you wait for its children
- Parallel-for over ranges, with optional SIMD batches
- Cancellation of jobs, including children and dependents
- Exceptions in jobs do not kill workers, and surface where you wait
- Safe, recycled handles: old handles are recognized
- Idle policy that adapts to your machine, and can be replaced
- And much more...
๐ Dependencies and children
var load = scheduler.Schedule(new LoadJob());
var physics = scheduler.Schedule(new PhysicsJob());
var render = scheduler.Schedule(new RenderJob());
scheduler.AddDependency(render, load); // render waits for load AND physics
scheduler.AddDependency(render, physics);
scheduler.Flush(load); // flush the jobs without dependencies, the rest follows
scheduler.Flush(physics);
scheduler.Wait(render);
var parent = scheduler.Schedule(new ParentJob());
var child = scheduler.Schedule(new ChildJob(), parent); // waiting for the parent also waits for its children
scheduler.Flush(parent);
scheduler.Flush(child);
scheduler.Wait(parent);
๐งฎ Parallel-for
using Schedulers.Utils;
public struct Scale : IJobParallelFor
{
public float[] Data;
public void Execute(int index) => Data[index] *= 2f;
}
var data = new float[100_000];
var handle = scheduler.ScheduleParallelFor(new Scale { Data = data }, data.Length);
scheduler.Flush(handle);
scheduler.Wait(handle);
Implement IJobParallelForBatch and use ScheduleParallelForBatch to receive Execute(int start, int count) with whole batches, for example to work with SIMD.
Like every other handle, the result can be waited for, cancelled and used for dependencies.
๐ Cancellation
scheduler.Cancel(handle); // skips jobs that did not start, including children and dependents
if (scheduler.IsCancelled(handle)) return; // lets a running job stop by itself, when it has its handle
๐ฅ Exceptions
try { scheduler.Wait(handle); }
catch (AggregateException e) { /* the exceptions of the jobs that threw */ }
scheduler.JobExceptionHandler = exception => Console.WriteLine(exception); // or get notified right away
๐ด Idle workers
Workers without jobs look for work for a short time and then sleep. How long is chosen from the wake-up time of your machine. To override that:
using Schedulers.Idle;
var scheduler = new JobScheduler(0, new FixedIdleSpinPolicy(TimeSpan.FromMilliseconds(0.1))); // or your own IIdleSpinPolicy
๐ Performance
The scheduler is built for the jobs of a game frame: many small ones, a few big ones, and a lot of parallel-for. Care is taken to find a healthy balance between CPU performance, memory and an idle machine.
| Scenario | NuGet 1.1.2 | Now | |
|---|---|---|---|
| 20,000 independent jobs | 68.8 ms | 4.2 ms | 16x |
| One job that waits for 1,000 jobs | 5.2 ms | 0.35 ms | 15x |
| 100 parallel-fors of 2,000 elements, like a frame of queries | 10.4 ms | 2.0 ms | 5x |
| Parallel-for over 1,000,000 elements | 1.8 ms | 0.29 ms | 6x |
| Chain of 20,000 dependent jobs | 21.4 ms | 7.8 ms | 2.7x |
| First job after a pause of 2 ms | 250 us | 53 us | 5x |
| Allocation for 20,000 struct jobs | 480 KB | 0 | |
| CPU usage while idle | 0 | 0 |
Measured against the last NuGet release with BenchmarkDotNet on one laptop, so take the numbers as an order of magnitude.
Compared with Parallel.For, Task and the ThreadPool it allocates nothing where they allocate kilobytes to megabytes, and it is faster for large ranges and for many queries at once. It is not faster everywhere: ThreadPool work items are a bit faster for 1,000 empty jobs, and Task.ContinueWith chains that run inline beat a short chain of dependent jobs.
Have a look at the benchmarks for all scenarios, and how they were measured!
๐ Documentation
- ๐ How it works > What happens to a job, and why it is fast.
- ๐ Benchmarks > Every scenario, compared with the last NuGet release.
โ ๏ธ Good to know
- At most 65,535 jobs can be scheduled and unfinished at the same time.
- Handles are recycled once their job has finished. Using an old handle is safe: waiting for it returns at once, cancelling it does nothing and flushing it throws.
Waitalso executes jobs on the calling thread, which may include jobs you did not wait for.- Running jobs are never interrupted, cancellation is cooperative.
- The tests and benchmarks ran on Linux x64 with .Net 8 so far. ARM64 and Windows are not covered yet.
IParallelJobProducerandParallelJobProducer<T>still work but are obsolete. UseIJobParallelForBatchinstead.
๐ ๏ธ Contributing
Found a bug, or have an idea? Open an issue or a pull request! The solution contains the tests (JobScheduler.Test) and the benchmarks (JobScheduler.Benchmarks, and JobScheduler.Benchmarks.Baseline for the NuGet package).
Please run the tests before you send a pull request, they include stress tests for the threading code.
Every push is built and tested on Linux, Windows and macOS. A pull request with the label benchmark also runs the benchmarks. A release is made by starting the workflow by hand: enter the version and the release notes, and choose to publish. It pushes the package to NuGet and GitHub, and creates the GitHub release.
๐ License
Apache 2.0, see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- CommunityToolkit.HighPerformance (>= 8.2.2)
NuGet packages (7)
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. |
GitHub repositories (3)
Showing the top 3 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.
|
|
|
genaray/Arch.ZeroAllocJobScheduler
A high-performance alloc free c# Jobscheduler.
|
|
|
annulusgames/Arch.Unity
Arch ECS integration for Unity.
|