Skip to content

A high-performance Job System that can be used on .Net and Unity platforms.

License

Notifications You must be signed in to change notification settings

TheLight-233/LuminJob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LuminJob

A high-performance Job System that can be used on .Net and Unity platforms.

LuminJob是一个可以在.Net和Unity平台使用的高性能任务系统,它具有以下优点:

  • 简化的Api
  • 轻松依赖多JobHandle
  • 基于.Net线程池
  • 适配struct/class/record
  • 适配Burst(Unity平台)
  • 几乎无Runtime GC(沟槽的.Net ThreadPool会装箱)
  • 通过源生成器兼容AOT

Installation 

LuminJob依赖于LuminObjectPool,LuminDelegate。因此使用前请确保安装了LuminObjectPool和LuminDelegate。

Quick Start

定义执行任务的类或结构体,并继承 ILuminJob/ILuminParallelJob/ILuminParallelForJob 接口

源生成器会为继承了接口的类或结构体生成代码,因此必须标记 partial

public partial class TestJob : ILuminJob
{
    public int num;
    
    public void Execute()
    {
    }
}

你可以通过以下两种方法调用任务

static void Main()
{
    var test = new TestJob();

    LuminJob.RunJob(test); // 方法一:通过静态类LuminJob调用,该方法返回LuminJobHandle
    test.Schedule();       // 方法二:通过源生成器生成的方法Schedule调用,该方法返回LuminJobHandle
}

About Struct

当任务源声明为结构体时,任务的调度将不会修改原始的结构体副本,这与Unity的Job System一样。

因此如果您想保留调度结果,可以使用Unity CollectionLuminCollection 或 声明为 class

About Burst

LuminJob会在.Net Standard2.1平台下,为声明为结构体并且符合Burst条件的生成Burst调度代码。

该情况下可以适配Burst的高性能实现 (需要您为Execute方法和结构体上标记[BurstCompile]

LuminJob会为您的任务源生成以下包装代码:

[BurstCompile]
public static void WarpIParallelJob(ref global::JobTest.PackTest instance, int index)
{
    instance.Execute(index);
}

public delegate void WarpIParallelJobDelegate(ref global::JobTest.PackTest instance, int index);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public LuminJobHandle Schedule(int arrayLength, int batchSize)
{
    var ptr = BurstCompiler.CompileFunctionPointer<WarpIParallelJobDelegate>(WarpIParallelJob);
    var luminPtr = Unsafe.As<Unity.Burst.FunctionPointer<WarpIParallelJobDelegate>, LuminJobCore.FunctionPointer<WarpIParallelJobDelegate>>(ref ptr);
    return LuminJob.RunParallelJob(this, arrayLength, batchSize, luminPtr);
}

About Multiple JobHandle

要想依赖多个LuminJobHandle执行完,必须调用生成的Schedule方法重载

LuminJob最多支持15个LuminJobHandle的依赖

static void Main()
{
    var test = new TestJob();
    var job1 = new TestJob();
    var job2 = new TestJob();

    var handle1 = job1.Schedule();
    var handle2 = job2.Schedule();
    
    test.Schedule(handle1, handle2).Wait();
}

License

This library is licensed under the MIT License.

About

A high-performance Job System that can be used on .Net and Unity platforms.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages