Job system priorities, async waits, parallel map/queue

Major refactor:
- Add job priority tiers and async wait APIs to IJobScheduler
- Implement priority-based job queues and scheduling logic
- Introduce UnsafeParallelHashMap and refactor UnsafeParallelQueue
- Refactor UnsafeSlotMap to chunked storage for scalability
- Update SlotMap/ConcurrentSlotMap for consistency and perf
- Add new benchmarks and unit tests for parallel collections
- Misc: add MemoryUtility.AlignUp, version bumps, test improvements, bug fixes
This commit is contained in:
2026-04-18 11:26:08 +09:00
parent d5616daa05
commit 13802ca6c8
22 changed files with 1459 additions and 267 deletions

View File

@@ -1,5 +1,80 @@
namespace Misaki.HighPerformance.Jobs; namespace Misaki.HighPerformance.Jobs;
internal sealed class WaitItem : IThreadPoolWorkItem
{
private readonly IJobScheduler _scheduler;
private readonly JobHandle _jobHandle;
private readonly TaskCompletionSource _completionSource;
public Task Task => _completionSource.Task;
public WaitItem(IJobScheduler scheduler, JobHandle jobHandle, CancellationToken cancellationToken)
{
_scheduler = scheduler;
_jobHandle = jobHandle;
_completionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
cancellationToken.Register((cs, tk) => ((TaskCompletionSource)cs!).TrySetCanceled(tk), _completionSource);
}
public void Execute()
{
_scheduler.Wait(_jobHandle);
_completionSource.SetResult();
}
}
internal sealed class WaitAllItem : IThreadPoolWorkItem
{
private readonly IJobScheduler _scheduler;
private readonly Memory<JobHandle> _jobHandles;
private readonly TaskCompletionSource _completionSource;
public Task Task => _completionSource.Task;
public WaitAllItem(IJobScheduler scheduler, Memory<JobHandle> jobHandles, CancellationToken cancellationToken)
{
_scheduler = scheduler;
_jobHandles = jobHandles;
_completionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
cancellationToken.Register((cs, tk) => ((TaskCompletionSource)cs!).TrySetCanceled(tk), _completionSource);
}
public void Execute()
{
_scheduler.WaitAll(_jobHandles.Span);
_completionSource.SetResult();
}
}
internal sealed class WaitAnyItem : IThreadPoolWorkItem
{
private readonly IJobScheduler _scheduler;
private readonly ReadOnlyMemory<JobHandle> _jobHandles;
private readonly TaskCompletionSource<JobHandle> _completionSource;
public Task<JobHandle> Task => _completionSource.Task;
public WaitAnyItem(IJobScheduler scheduler, ReadOnlyMemory<JobHandle> jobHandles, CancellationToken cancellationToken)
{
_scheduler = scheduler;
_jobHandles = jobHandles;
_completionSource = new TaskCompletionSource<JobHandle>(TaskCreationOptions.RunContinuationsAsynchronously);
cancellationToken.Register((cs, tk) => ((TaskCompletionSource)cs!).TrySetCanceled(tk), _completionSource);
}
public void Execute()
{
var completedHandle = _scheduler.WaitAny(_jobHandles.Span);
_completionSource.SetResult(completedHandle);
}
}
public interface IJobScheduler public interface IJobScheduler
{ {
/// <summary> /// <summary>
@@ -18,9 +93,10 @@ public interface IJobScheduler
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param>
/// <param name="dependency">A <see cref="JobHandle"/> representing the dependencies that must be completed before this job can begin. /// <param name="dependency">A <see cref="JobHandle"/> representing the dependencies that must be completed before this job can begin.
/// Use <see cref="JobHandle.Invalid"/> if there are no dependencies.</param> /// Use <see cref="JobHandle.Invalid"/> if there are no dependencies.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle Schedule<T>(ref readonly T job, int threadIndex, JobHandle dependency) JobHandle Schedule<T>(ref readonly T job, int threadIndex, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob; where T : unmanaged, IJob;
/// <summary> /// <summary>
@@ -31,7 +107,7 @@ public interface IJobScheduler
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle Schedule<T>(ref readonly T job, int threadIndex) JobHandle Schedule<T>(ref readonly T job, int threadIndex, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob; where T : unmanaged, IJob;
/// <summary> /// <summary>
@@ -39,9 +115,10 @@ public interface IJobScheduler
/// </summary> /// </summary>
/// <typeparam name="T">The type of the job to execute. Must implement <see cref="IJob"/> and be unmanaged.</typeparam> /// <typeparam name="T">The type of the job to execute. Must implement <see cref="IJob"/> and be unmanaged.</typeparam>
/// <param name="job">The job instance to be executed. The job data will be copied internally.</param> /// <param name="job">The job instance to be executed. The job data will be copied internally.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle Schedule<T>(ref readonly T job, JobHandle dependency) JobHandle Schedule<T>(ref readonly T job, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob; where T : unmanaged, IJob;
/// <summary> /// <summary>
@@ -50,9 +127,10 @@ public interface IJobScheduler
/// <typeparam name="T">The type of the job to execute. Must implement <see cref="IJob"/> and be unmanaged.</typeparam> /// <typeparam name="T">The type of the job to execute. Must implement <see cref="IJob"/> and be unmanaged.</typeparam>
/// <param name="job">The job instance to be executed. The job data will be copied internally.</param> /// <param name="job">The job instance to be executed. The job data will be copied internally.</param>
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle Schedule<T>(ref readonly T job) JobHandle Schedule<T>(ref readonly T job, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob; where T : unmanaged, IJob;
/// <summary> /// <summary>
@@ -65,9 +143,10 @@ public interface IJobScheduler
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param>
/// <param name="dependency">A <see cref="JobHandle"/> representing the dependencies that must be completed before this job can begin. /// <param name="dependency">A <see cref="JobHandle"/> representing the dependencies that must be completed before this job can begin.
/// Use <see cref="JobHandle.Invalid"/> if there are no dependencies.</param> /// Use <see cref="JobHandle.Invalid"/> if there are no dependencies.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency) JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor; where T : unmanaged, IJobParallelFor;
/// <summary> /// <summary>
@@ -78,9 +157,10 @@ public interface IJobScheduler
/// <param name="totalIteration">The total number of iterations to be processed by the job.</param> /// <param name="totalIteration">The total number of iterations to be processed by the job.</param>
/// <param name="batchSize">The number of iterations to include in each batch.</param> /// <param name="batchSize">The number of iterations to include in each batch.</param>
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex) JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor; where T : unmanaged, IJobParallelFor;
/// <summary> /// <summary>
@@ -90,10 +170,11 @@ public interface IJobScheduler
/// <param name="job">The job instance to be executed. The job data will be copied internally.</param> /// <param name="job">The job instance to be executed. The job data will be copied internally.</param>
/// <param name="totalIteration">The total number of iterations to be processed by the job.</param> /// <param name="totalIteration">The total number of iterations to be processed by the job.</param>
/// <param name="batchSize">The number of iterations to include in each batch.</param> /// <param name="batchSize">The number of iterations to include in each batch.</param>
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="dependency">The job that this job depends on.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency) JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor; where T : unmanaged, IJobParallelFor;
/// <summary> /// <summary>
@@ -103,10 +184,10 @@ public interface IJobScheduler
/// <param name="job">The job instance to be executed. The job data will be copied internally.</param> /// <param name="job">The job instance to be executed. The job data will be copied internally.</param>
/// <param name="totalIteration">The total number of iterations to be processed by the job.</param> /// <param name="totalIteration">The total number of iterations to be processed by the job.</param>
/// <param name="batchSize">The number of iterations to include in each batch.</param> /// <param name="batchSize">The number of iterations to include in each batch.</param>
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize) JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor; where T : unmanaged, IJobParallelFor;
/// <summary> /// <summary>
@@ -119,9 +200,10 @@ public interface IJobScheduler
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param>
/// <param name="dependency">A <see cref="JobHandle"/> representing the dependencies that must be completed before this job can begin. /// <param name="dependency">A <see cref="JobHandle"/> representing the dependencies that must be completed before this job can begin.
/// Use <see cref="JobHandle.Invalid"/> if there are no dependencies.</param> /// Use <see cref="JobHandle.Invalid"/> if there are no dependencies.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency) JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel; where T : unmanaged, IJobParallel;
/// <summary> /// <summary>
@@ -132,9 +214,10 @@ public interface IJobScheduler
/// <param name="totalIteration">The total number of iterations to be processed by the job.</param> /// <param name="totalIteration">The total number of iterations to be processed by the job.</param>
/// <param name="batchSize">The number of iterations to include in each batch.</param> /// <param name="batchSize">The number of iterations to include in each batch.</param>
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex) JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel; where T : unmanaged, IJobParallel;
/// <summary> /// <summary>
@@ -144,10 +227,11 @@ public interface IJobScheduler
/// <param name="job">The job instance to be executed. The job data will be copied internally.</param> /// <param name="job">The job instance to be executed. The job data will be copied internally.</param>
/// <param name="totalIteration">The total number of iterations to be processed by the job.</param> /// <param name="totalIteration">The total number of iterations to be processed by the job.</param>
/// <param name="batchSize">The number of iterations to include in each batch.</param> /// <param name="batchSize">The number of iterations to include in each batch.</param>
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="dependency">The job that this job depends on.</param>
/// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency) JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel; where T : unmanaged, IJobParallel;
/// <summary> /// <summary>
@@ -157,10 +241,10 @@ public interface IJobScheduler
/// <param name="job">The job instance to be executed. The job data will be copied internally.</param> /// <param name="job">The job instance to be executed. The job data will be copied internally.</param>
/// <param name="totalIteration">The total number of iterations to be processed by the job.</param> /// <param name="totalIteration">The total number of iterations to be processed by the job.</param>
/// <param name="batchSize">The number of iterations to include in each batch.</param> /// <param name="batchSize">The number of iterations to include in each batch.</param>
/// <param name="threadIndex">The index of the thread that is preferred to execute the job. This is used to assign thread-specific data. Use -1 to allow any thread to execute the job.</param> /// <param name="priority">The priority of the job.</param>
/// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job. /// <returns>A <see cref="JobHandle"/> that can be used to track the completion of the scheduled job.
/// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns> /// Returns <see cref="JobHandle.Invalid"/> if the job data allocation fails.</returns>
JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize) JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel; where T : unmanaged, IJobParallel;
/// <summary> /// <summary>
@@ -200,4 +284,31 @@ public interface IJobScheduler
/// <param name="handles">A read-only span containing the job handles to monitor for completion.</param> /// <param name="handles">A read-only span containing the job handles to monitor for completion.</param>
/// <returns>The first job handle from the provided collection that has completed.</returns> /// <returns>The first job handle from the provided collection that has completed.</returns>
JobHandle WaitAny(params ReadOnlySpan<JobHandle> handles); JobHandle WaitAny(params ReadOnlySpan<JobHandle> handles);
/// <summary>
/// Waits asynchronously until the specified job is completed, allowing the calling thread to perform other work while waiting.
/// </summary>
/// <param name="handle">The handle of the job to wait for.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the wait operation.</param>
/// <returns>A task that represents the asynchronous wait operation.</returns>
Task WaitAsync(JobHandle handle, CancellationToken cancellationToken = default);
/// <summary>
/// Waits asynchronously until all specified job handles have completed, allowing the calling thread to perform other work while waiting.
/// </summary>
/// <remarks>
/// The collection handles will be reordered in-place to move completed handles to the front.
/// </remarks>
/// <param name="handles">A read-only memory containing the job handles to monitor for completion.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the wait operation.</param>
/// <returns>A task that represents the asynchronous wait operation.</returns>
Task WaitAllAsync(Memory<JobHandle> handles, CancellationToken cancellationToken = default);
/// <summary>
/// Waits asynchronously until any of the specified job handles has completed, allowing the calling thread to perform other work while waiting, and returns the first completed handle.
/// </summary>
/// <param name="handles">A read-only memory containing the job handles to monitor for completion.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the wait operation.</param>
/// <returns>A task that represents the asynchronous wait operation.</returns>
Task<JobHandle> WaitAnyAsync(ReadOnlyMemory<JobHandle> handles, CancellationToken cancellationToken = default);
} }

View File

@@ -1,8 +1,6 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Jobs; namespace Misaki.HighPerformance.Jobs;
@@ -33,10 +31,11 @@ public enum JobState
Completed = 3 Completed = 3
} }
internal enum HeapType public enum JobPriority
{ {
Native, High = 0,
Managed, Normal = 1,
Low = 2
} }
internal unsafe struct JobInfo internal unsafe struct JobInfo
@@ -95,6 +94,7 @@ internal unsafe struct JobInfo
public int dependentCount; public int dependentCount;
public JobRanges jobRanges; public JobRanges jobRanges;
public JobPriority priority;
public int state; public int state;
public int remainingBatches; public int remainingBatches;

View File

@@ -8,6 +8,24 @@ using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Jobs; namespace Misaki.HighPerformance.Jobs;
public struct JobSchedulerDesc
{
public int ThreadCount
{
get; set;
}
public ThreadPriority ThreadPriority
{
get; set;
}
public object? State
{
get; set;
}
}
/// <summary> /// <summary>
/// Provides a mechanism for scheduling and executing jobs across multiple worker threads. /// Provides a mechanism for scheduling and executing jobs across multiple worker threads.
/// </summary> /// </summary>
@@ -19,7 +37,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
private FreeList _freeList; private FreeList _freeList;
private readonly ConcurrentSlotMap<JobInfo> _jobInfoPool; private readonly ConcurrentSlotMap<JobInfo> _jobInfoPool;
private readonly ConcurrentQueue<JobHandle> _jobQueue; private readonly ConcurrentQueue<JobHandle>[] _jobQueues;
private readonly WorkerThread[] _workerThreads; private readonly WorkerThread[] _workerThreads;
private readonly SemaphoreSlim _workSignal; private readonly SemaphoreSlim _workSignal;
@@ -29,25 +47,68 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
private bool _disposed = false; private bool _disposed = false;
internal bool IsCancellationRequested => _cts.IsCancellationRequested;
internal object? State => _state; internal object? State => _state;
internal bool IsCancellationRequested => _cts.IsCancellationRequested;
public int WorkerCount => _workerThreads.Length; public int WorkerCount => _workerThreads.Length;
/// <summary>
/// Initializes a new instance of the <see cref="JobScheduler"/> class with the specified description.
/// </summary>
/// <param name="desc">The description for the job scheduler.</param>
public JobScheduler(ref readonly JobSchedulerDesc desc)
{
var workerCount = Math.Max(1, desc.ThreadCount);
_freeList = new FreeList(MemoryUtility.AlignOf<IntPtr>(), maxConcurrencyLevel: workerCount);
_jobInfoPool = new ConcurrentSlotMap<JobInfo>(128);
_jobQueues = new ConcurrentQueue<JobHandle>[3];
for (var i = 0; i < 3; i++)
{
_jobQueues[i] = new ConcurrentQueue<JobHandle>();
}
_workSignal = new SemaphoreSlim(0);
_cts = new CancellationTokenSource();
_state = desc.State;
_workerThreads = new WorkerThread[workerCount];
for (var i = 0; i < workerCount; i++)
{
_workerThreads[i] = new WorkerThread(i, this, desc.ThreadPriority);
}
foreach (var worker in _workerThreads)
{
worker.Start();
}
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JobScheduler"/> class with the specified number of worker threads. /// Initializes a new instance of the <see cref="JobScheduler"/> class with the specified number of worker threads.
/// </summary> /// </summary>
/// <param name="threadCount">The number of worker threads to create. If less than 1, at least one thread will be created.</param> /// <param name="threadCount">The number of worker threads to create. If less than 1, at least one thread will be created.</param>
/// <param name="priority">The priority of the worker threads.</param> /// <param name="priority">The priority of the worker threads.</param>
/// <param name="state">The state object for the job scheduler.</param> /// <param name="state">The state object for the job scheduler.</param>
public JobScheduler(int threadCount, ThreadPriority priority = ThreadPriority.Normal, object? state = null) /// <param name="allowManagedJobs">A value indicating whether managed jobs are allowed.</param>
[Obsolete("Use JobScheduler(JobSchedulerDesc) instead.")]
public JobScheduler(int threadCount, ThreadPriority priority = ThreadPriority.Normal, object? state = null, bool allowManagedJobs = false)
{ {
var workerCount = Math.Max(1, threadCount); var workerCount = Math.Max(1, threadCount);
_freeList = new FreeList(MemoryUtility.AlignOf<IntPtr>(), maxConcurrencyLevel: threadCount); _freeList = new FreeList(MemoryUtility.AlignOf<IntPtr>(), maxConcurrencyLevel: workerCount);
_jobInfoPool = new ConcurrentSlotMap<JobInfo>(128); _jobInfoPool = new ConcurrentSlotMap<JobInfo>(128);
_jobQueue = new ConcurrentQueue<JobHandle>(); _jobQueues = new ConcurrentQueue<JobHandle>[3];
for (var i = 0; i < 3; i++)
{
_jobQueues[i] = new ConcurrentQueue<JobHandle>();
}
_workSignal = new SemaphoreSlim(0); _workSignal = new SemaphoreSlim(0);
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
@@ -72,7 +133,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
Dispose(); Dispose();
} }
private void EnqueueJobIfReady(JobHandle handle) private void EnqueueJobIfReady(JobHandle handle, int threadIndex)
{ {
ref var jobInfo = ref _jobInfoPool.GetElementReferenceAt(handle.ID, handle.Generation, out var exist); ref var jobInfo = ref _jobInfoPool.GetElementReferenceAt(handle.ID, handle.Generation, out var exist);
@@ -84,14 +145,20 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
return; return;
} }
var tier = (int)jobInfo.priority;
ConcurrentQueue<JobHandle> jobQueue; ConcurrentQueue<JobHandle> jobQueue;
if (jobInfo.threadIndex >= 0 && jobInfo.threadIndex < _workerThreads.Length) if (jobInfo.threadIndex >= 0 && jobInfo.threadIndex < _workerThreads.Length)
{ {
jobQueue = _workerThreads[jobInfo.threadIndex].LocalQueue; jobQueue = _workerThreads[jobInfo.threadIndex].LocalQueues[tier];
}
else if (threadIndex >= 0 && threadIndex < _workerThreads.Length)
{
// Put into the local thread queue if the scheduling thread is a worker thread. This can improve cache locality and reduce contention on the main queue.
jobQueue = _workerThreads[threadIndex].LocalQueues[tier];
} }
else else
{ {
jobQueue = _jobQueue; jobQueue = _jobQueues[tier];
} }
// Ensure the count of this job handle won't exceed the number of worker threads. // Ensure the count of this job handle won't exceed the number of worker threads.
@@ -107,7 +174,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
} }
} }
private JobHandle CreateJobHandle(ref JobInfo jobInfo, params ReadOnlySpan<JobHandle> dependencies) private JobHandle CreateJobHandle(ref JobInfo jobInfo, int threadIndex, params ReadOnlySpan<JobHandle> dependencies)
{ {
var validDepCount = 0; var validDepCount = 0;
for (var i = 0; i < dependencies.Length; i++) for (var i = 0; i < dependencies.Length; i++)
@@ -199,7 +266,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
// Lower the initial 1 guard lock; Enqueue if met // Lower the initial 1 guard lock; Enqueue if met
if (Interlocked.Decrement(ref infoInPool.dependencyCount) == 0) if (Interlocked.Decrement(ref infoInPool.dependencyCount) == 0)
{ {
EnqueueJobIfReady(handle); EnqueueJobIfReady(handle, threadIndex);
} }
return handle; return handle;
@@ -208,16 +275,22 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool HasWork() internal bool HasWork()
{ {
if (!_jobQueue.IsEmpty) for (var i = 0; i < _jobQueues.Length; i++)
{ {
return true; if (!_jobQueues[i].IsEmpty)
{
return true;
}
} }
for (var i = 0; i < _workerThreads.Length; i++) for (var i = 0; i < _workerThreads.Length; i++)
{ {
if (!_workerThreads[i].LocalQueue.IsEmpty) for (var j = 0; j < _workerThreads[i].LocalQueues.Length; j++)
{ {
return true; if (!_workerThreads[i].LocalQueues[j].IsEmpty)
{
return true;
}
} }
} }
@@ -231,15 +304,15 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryStealFromMain(int threadIndex, out JobHandle outHandle) internal bool TryStealFromMain(int tier, out JobHandle outHandle)
{ {
return _jobQueue.TryDequeue(out outHandle); return _jobQueues[tier].TryDequeue(out outHandle);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryStealFromWorker(int threadIndex, out JobHandle outHandle) internal bool TryStealFromWorker(int threadIndex, int tier, out JobHandle outHandle)
{ {
return _workerThreads[threadIndex].LocalQueue.TryDequeue(out outHandle); return _workerThreads[threadIndex].LocalQueues[tier].TryDequeue(out outHandle);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -254,7 +327,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
return ref _jobInfoPool.GetElementReferenceAt(handle.ID, handle.Generation, out exist); return ref _jobInfoPool.GetElementReferenceAt(handle.ID, handle.Generation, out exist);
} }
internal void MarkJobComplete(JobHandle handle) internal void MarkJobComplete(JobHandle handle, int threadIndex)
{ {
Debug.Assert(handle.IsValid); Debug.Assert(handle.IsValid);
@@ -309,7 +382,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
ref var depJobInfo = ref _jobInfoPool.GetElementReferenceAt(depHandle.ID, depHandle.Generation, out var depExist); ref var depJobInfo = ref _jobInfoPool.GetElementReferenceAt(depHandle.ID, depHandle.Generation, out var depExist);
if (depExist && Interlocked.Decrement(ref depJobInfo.dependencyCount) == 0) if (depExist && Interlocked.Decrement(ref depJobInfo.dependencyCount) == 0)
{ {
EnqueueJobIfReady(depHandle); EnqueueJobIfReady(depHandle, threadIndex);
} }
} }
@@ -319,7 +392,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
_jobInfoPool.Remove(handle.ID, handle.Generation); _jobInfoPool.Remove(handle.ID, handle.Generation);
} }
public JobHandle Schedule<T>(ref readonly T job, int threadIndex, JobHandle dependency) public JobHandle Schedule<T>(ref readonly T job, int threadIndex, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob where T : unmanaged, IJob
{ {
var pJobData = _freeList.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>()); var pJobData = _freeList.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>());
@@ -341,22 +414,22 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
jobRanges = JobRanges.Single, jobRanges = JobRanges.Single,
}; };
return CreateJobHandle(ref jobInfo, dependency); return CreateJobHandle(ref jobInfo, threadIndex, dependency);
} }
public JobHandle Schedule<T>(ref readonly T job, int threadIndex) public JobHandle Schedule<T>(ref readonly T job, int threadIndex, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob where T : unmanaged, IJob
=> Schedule(in job, threadIndex, JobHandle.Invalid); => Schedule(in job, threadIndex, JobHandle.Invalid, priority);
public JobHandle Schedule<T>(ref readonly T job, JobHandle dependency) public JobHandle Schedule<T>(ref readonly T job, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob where T : unmanaged, IJob
=> Schedule(in job, -1, dependency); => Schedule(in job, -1, dependency, priority);
public JobHandle Schedule<T>(ref readonly T job) public JobHandle Schedule<T>(ref readonly T job, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJob where T : unmanaged, IJob
=> Schedule(in job, -1, JobHandle.Invalid); => Schedule(in job, -1, JobHandle.Invalid, priority);
public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency) public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor where T : unmanaged, IJobParallelFor
{ {
var pJobData = _freeList.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>()); var pJobData = _freeList.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>());
@@ -386,22 +459,22 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
}, },
}; };
return CreateJobHandle(ref jobInfo, dependency); return CreateJobHandle(ref jobInfo, threadIndex, dependency);
} }
public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex) public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor where T : unmanaged, IJobParallelFor
=> ScheduleParallelFor(in job, totalIteration, batchSize, threadIndex, JobHandle.Invalid); => ScheduleParallelFor(in job, totalIteration, batchSize, threadIndex, JobHandle.Invalid, priority);
public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency) public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor where T : unmanaged, IJobParallelFor
=> ScheduleParallelFor(in job, totalIteration, batchSize, -1, dependency); => ScheduleParallelFor(in job, totalIteration, batchSize, -1, dependency, priority);
public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize) public JobHandle ScheduleParallelFor<T>(ref readonly T job, int totalIteration, int batchSize, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallelFor where T : unmanaged, IJobParallelFor
=> ScheduleParallelFor(in job, totalIteration, batchSize, -1, JobHandle.Invalid); => ScheduleParallelFor(in job, totalIteration, batchSize, -1, JobHandle.Invalid, priority);
public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency) public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel where T : unmanaged, IJobParallel
{ {
var pJobData = _freeList.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>()); var pJobData = _freeList.Allocate(MemoryUtility.SizeOf<T>(), MemoryUtility.AlignOf<T>());
@@ -431,20 +504,20 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
}, },
}; };
return CreateJobHandle(ref jobInfo, dependency); return CreateJobHandle(ref jobInfo, threadIndex, dependency);
} }
public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex) public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, int threadIndex, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel where T : unmanaged, IJobParallel
=> ScheduleParallel(in job, totalIteration, batchSize, threadIndex, JobHandle.Invalid); => ScheduleParallel(in job, totalIteration, batchSize, threadIndex, JobHandle.Invalid, priority);
public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency) public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, JobHandle dependency, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel where T : unmanaged, IJobParallel
=> ScheduleParallel(in job, totalIteration, batchSize, -1, dependency); => ScheduleParallel(in job, totalIteration, batchSize, -1, dependency, priority);
public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize) public JobHandle ScheduleParallel<T>(ref readonly T job, int totalIteration, int batchSize, JobPriority priority = JobPriority.Normal)
where T : unmanaged, IJobParallel where T : unmanaged, IJobParallel
=> ScheduleParallel(in job, totalIteration, batchSize, -1, JobHandle.Invalid); => ScheduleParallel(in job, totalIteration, batchSize, -1, JobHandle.Invalid, priority);
public JobHandle CombineDependencies(params ReadOnlySpan<JobHandle> dependencies) public JobHandle CombineDependencies(params ReadOnlySpan<JobHandle> dependencies)
{ {
@@ -459,7 +532,7 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
jobRanges = JobRanges.Single, jobRanges = JobRanges.Single,
}; };
return CreateJobHandle(ref jobInfo, dependencies); return CreateJobHandle(ref jobInfo, -1, dependencies);
} }
public JobState GetJobStatus(JobHandle handle) public JobState GetJobStatus(JobHandle handle)
@@ -562,6 +635,45 @@ public sealed unsafe partial class JobScheduler : IJobScheduler, IDisposable
} }
} }
public Task WaitAsync(JobHandle handle, CancellationToken cancellationToken = default)
{
if (!handle.IsValid)
{
return Task.CompletedTask;
}
var workItem = new WaitItem(this, handle, cancellationToken);
ThreadPool.UnsafeQueueUserWorkItem(workItem, preferLocal: true);
return workItem.Task;
}
public Task WaitAllAsync(Memory<JobHandle> handles, CancellationToken cancellationToken = default)
{
if (handles.Length == 0)
{
return Task.CompletedTask;
}
var workItem = new WaitAllItem(this, handles, cancellationToken);
ThreadPool.UnsafeQueueUserWorkItem(workItem, preferLocal: true);
return workItem.Task;
}
public Task<JobHandle> WaitAnyAsync(ReadOnlyMemory<JobHandle> handles, CancellationToken cancellationToken = default)
{
if (handles.Length == 0)
{
return Task.FromResult(JobHandle.Invalid);
}
var workItem = new WaitAnyItem(this, handles, cancellationToken);
ThreadPool.UnsafeQueueUserWorkItem(workItem, preferLocal: true);
return workItem.Task;
}
public void Dispose() public void Dispose()
{ {
if (_disposed) if (_disposed)

View File

@@ -6,7 +6,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>1.6.1</AssemblyVersion> <AssemblyVersion>2.0.0</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>

View File

@@ -4,23 +4,33 @@ namespace Misaki.HighPerformance.Jobs;
internal class WorkerThread : IDisposable internal class WorkerThread : IDisposable
{ {
private const int _MAX_STEAL_ATTEMPTS = 8;
private readonly int _index; private readonly int _index;
private readonly Thread _thread; private readonly Thread _thread;
private readonly ConcurrentQueue<JobHandle> _localQueue; private readonly ConcurrentQueue<JobHandle>[] _localQueues;
private readonly JobScheduler _scheduler; private readonly JobScheduler _scheduler;
private readonly Random _random; private readonly Random _stealRandom;
internal ConcurrentQueue<JobHandle> LocalQueue => _localQueue; private readonly int _maxStealAttems;
private uint _priorityTick;
internal ReadOnlySpan<ConcurrentQueue<JobHandle>> LocalQueues => _localQueues;
public WorkerThread(int index, JobScheduler scheduler, ThreadPriority priority) public WorkerThread(int index, JobScheduler scheduler, ThreadPriority priority)
{ {
_index = index; _index = index;
_localQueue = new(); _localQueues = new ConcurrentQueue<JobHandle>[3];
for (var i = 0; i < _localQueues.Length; i++)
{
_localQueues[i] = new ConcurrentQueue<JobHandle>();
}
_scheduler = scheduler; _scheduler = scheduler;
_random = new Random(index * 9973 + Environment.TickCount); _stealRandom = new Random(index * 9973 + Environment.TickCount);
_maxStealAttems = Math.Max((int)(_scheduler.WorkerCount * 0.5f), 3);
_thread = new Thread(WorkLoop) _thread = new Thread(WorkLoop)
{ {
@@ -30,27 +40,53 @@ internal class WorkerThread : IDisposable
}; };
} }
public void Start() => _thread.Start(); public void Start()
private bool TryFindJob(out JobHandle handle)
{ {
if (_localQueue.TryDequeue(out handle)) _thread.Start();
{ }
return true;
}
if (_scheduler.TryStealFromMain(-1, out handle)) private unsafe bool TryFindJob(out JobHandle handle)
{ {
return true; _priorityTick++;
}
for (var i = 0; i < _MAX_STEAL_ATTEMPTS; i++) var tick = (int)(_priorityTick & 7);
// Ratio: 4 High (50%), 3 Normal (37.5%), 1 Low (12.5%)
var cascade = stackalloc int[24] {
0, 1, 2, // Tick 0 (High)
0, 1, 2, // Tick 1 (High)
0, 1, 2, // Tick 2 (High)
0, 1, 2, // Tick 3 (High)
1, 2, 0, // Tick 4 (Normal)
1, 2, 0, // Tick 5 (Normal)
1, 2, 0, // Tick 6 (Normal)
2, 0, 1 // Tick 7 (Low)
};
var index = tick * 3;
for (var offset = 0; offset < 3; offset++)
{ {
var randomIndex = _random.Next(0, _scheduler.WorkerCount); var p = cascade[index + offset];
if (randomIndex != _index && _scheduler.TryStealFromWorker(randomIndex, out handle))
if (_localQueues[p].TryDequeue(out handle))
{ {
return true; return true;
} }
if (_scheduler.TryStealFromMain(p, out handle))
{
return true;
}
for (var i = 1; i < _scheduler.WorkerCount; i++)
{
// Calculate the target deterministically using modulo arithmetic
var targetIndex = (_index + i) % _scheduler.WorkerCount;
if (_scheduler.TryStealFromWorker(targetIndex, p, out handle))
{
return true;
}
}
} }
handle = JobHandle.Invalid; handle = JobHandle.Invalid;
@@ -121,7 +157,7 @@ internal class WorkerThread : IDisposable
} }
} }
_scheduler.MarkJobComplete(handle); _scheduler.MarkJobComplete(handle, _index);
} }
} }
} }

View File

@@ -166,7 +166,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int CeilPow2(int x) internal static int CeilPow2(int x)
{ {
x -= 1; x -= 1;
x |= x >> 1; x |= x >> 1;

View File

@@ -1,7 +1,6 @@
using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts; using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities; using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;

View File

@@ -0,0 +1,504 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Collections;
public unsafe struct UnsafeParallelHashMapData<TKey, TValue>
where TKey : unmanaged, IEquatable<TKey>
where TValue : unmanaged
{
public byte* buffer;
public TKey* keys;
public TValue* values;
public int* next;
public int* buckets;
public int count;
public int capacity;
public int bucketCapacityMask;
public int allocatedIndex;
public int firstFreeIndex;
public int alignment;
public int log2MinGrowth;
#if MHP_ENABLE_SAFETY_CHECKS
public MemoryHandle memoryHandle;
#endif
public AllocationHandle allocationHandle;
}
public unsafe struct UnsafeParallelHashMap<TKey, TValue> : IDisposable
where TKey : unmanaged, IEquatable<TKey>
where TValue : unmanaged
{
internal UnsafeParallelHashMapData<TKey, TValue>* _data;
public const int MINIMAL_CAPACITY = 64;
public readonly int Count => _data != null ? _data->count : 0;
public readonly int Capacity => _data != null ? _data->capacity : 0;
public readonly bool IsEmpty => !IsCreated || _data->count == 0;
public readonly bool IsCreated
{
get
{
#if MHP_ENABLE_SAFETY_CHECKS
if (_data != null)
{
if (_data->buffer != null)
{
return _data->memoryHandle.IsValid;
}
}
return false;
#else
return _data != null && _data->buffer != null;
#endif
}
}
public UnsafeParallelHashMap(int capacity, uint minGrowth, AllocationHandle handle, AllocationOption allocationOption)
{
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
_data = (UnsafeParallelHashMapData<TKey, TValue>*)handle.Alloc(handle.State, (uint)sizeof(UnsafeParallelHashMapData<TKey, TValue>), (nuint)AlignOf<UnsafeParallelHashMapData<TKey, TValue>>(), AllocationOption.Clear);
if (_data == null)
throw new OutOfMemoryException("Failed to allocate UnsafeParallelHashMapData.");
_data->capacity = capacity;
_data->bucketCapacityMask = capacity * 2 - 1;
var alignOfKey = (int)AlignOf<TKey>();
var alignOfTValue = (int)AlignOf<TValue>();
var alignOfInt = (int)AlignOf<int>();
var maxDataAlign = Math.Max(Math.Max(alignOfTValue, alignOfKey), alignOfInt);
_data->alignment = maxDataAlign;
_data->log2MinGrowth = BitOperations.Log2(minGrowth);
_data->allocationHandle = handle;
var totalSize = CalculateDataSize(capacity, capacity * 2, out var keyOffset, out var valueOffset, out var nextOffset, out var bucketOffset);
allocationOption &= ~AllocationOption.Clear;
AllocateBuffer(_data, totalSize, keyOffset, valueOffset, nextOffset, bucketOffset, allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS
_data->memoryHandle = MemoryHandle.Create(_data->buffer, (nuint)totalSize);
#endif
Clear();
}
public void Dispose()
{
if (!IsCreated)
return;
#if MHP_ENABLE_SAFETY_CHECKS
_data->memoryHandle.Dispose();
#endif
if (_data->buffer != null && _data->allocationHandle.Free != null)
{
_data->allocationHandle.Free(_data->allocationHandle.State, _data->buffer);
_data->buffer = null;
}
if (_data != null && _data->allocationHandle.Free != null)
{
_data->allocationHandle.Free(_data->allocationHandle.State, _data);
_data = null;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("MHP_ENABLE_SAFETY_CHECKS")]
private readonly void ThrowIfNotCreated()
{
if (!IsCreated)
{
throw new InvalidOperationException("The UnsafeParallelHashMap is not created.");
}
}
private static int CalculateDataSize(int capacity, int bucketCapacity, out int outKeyOffset, out int outValueOffset, out int outNextOffset, out int outBucketOffset)
{
var sizeOfTKey = sizeof(TKey);
var sizeOfTValue = sizeof(TValue);
var sizeOfInt = sizeof(int);
var keysSize = sizeOfTKey * capacity;
var valuesSize = sizeOfTValue * capacity;
var nextSize = sizeOfInt * capacity;
var bucketSize = sizeOfInt * bucketCapacity;
var totalSize = keysSize + valuesSize + nextSize + bucketSize;
outKeyOffset = 0;
outValueOffset = outKeyOffset + keysSize;
outNextOffset = outValueOffset + valuesSize;
outBucketOffset = outNextOffset + nextSize;
return totalSize;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static uint AlignOf<T>() where T : unmanaged
{
return (uint)Unsafe.SizeOf<T>(); // Temporary substitute for alignment util
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int CeilPow2(int x)
{
x -= 1;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int CalcCapacityCeilPow2(int capacity)
{
capacity = Math.Max(Math.Max(1, _data->count), capacity);
var newCapacity = Math.Max(capacity, 1 << _data->log2MinGrowth);
var result = CeilPow2(newCapacity);
return result;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AllocateBuffer(UnsafeParallelHashMapData<TKey, TValue>* data, int totalSize, int keyOffset, int valueOffset, int nextOffset, int bucketOffset, AllocationOption allocationOption)
{
if (data->allocationHandle.Alloc == null)
{
throw new InvalidOperationException("Target allocation handle does not support allocation.");
}
var buf = (byte*)data->allocationHandle.Alloc(data->allocationHandle.State, (uint)totalSize, (nuint)data->alignment, allocationOption);
data->buffer = buf;
data->keys = (TKey*)(buf + keyOffset);
data->values = (TValue*)(buf + valueOffset);
data->next = (int*)(buf + nextOffset);
data->buckets = (int*)(buf + bucketOffset);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetBucket(int hash)
{
return hash & _data->bucketCapacityMask;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetBucket(scoped in TKey key)
{
return GetBucket(key.GetHashCode());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly void CheckIndexOutOfBounds(int idx)
{
if ((uint)idx >= (uint)_data->capacity)
{
throw new InvalidOperationException($"Index {idx} is out of bounds for the hash map with capacity {_data->capacity}.");
}
}
public void Clear()
{
ThrowIfNotCreated();
_data->count = 0;
_data->allocatedIndex = 0;
_data->firstFreeIndex = -1;
if (_data->buffer == null)
return;
var bucketCapacity = _data->bucketCapacityMask + 1;
MemoryUtility.MemSet(_data->buckets, (byte)0xFF, (nuint)(bucketCapacity * sizeof(int)));
MemoryUtility.MemSet(_data->next, (byte)0xFF, (nuint)(_data->capacity * sizeof(int)));
}
public int Add(scoped in TKey key, scoped in TValue value)
{
ThrowIfNotCreated();
if (Find(in key) != -1)
return -1; // Or throw depending on semantics you want
return AllocateEntry(key, value);
}
public bool TryGetValue(scoped in TKey key, out TValue item)
{
var idx = Find(key);
if (idx != -1)
{
item = _data->values[idx];
return true;
}
item = default;
return false;
}
public int Find(scoped in TKey key)
{
ThrowIfNotCreated();
if (_data->allocatedIndex <= 0)
{
return -1;
}
var bucket = GetBucket(key);
var entryIdx = _data->buckets[bucket];
if ((uint)entryIdx < (uint)_data->capacity)
{
var nextPtrs = _data->next;
while (!UnsafeUtility.ReadArrayElement<TKey>(_data->keys, entryIdx).Equals(key))
{
entryIdx = nextPtrs[entryIdx];
if ((uint)entryIdx >= (uint)_data->capacity)
{
return -1;
}
}
return entryIdx;
}
return -1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int AllocateEntry(scoped in TKey key, scoped in TValue value)
{
int idx;
if (_data->allocatedIndex >= _data->capacity && _data->firstFreeIndex < 0)
{
var newCap = CalcCapacityCeilPow2(_data->capacity + (1 << _data->log2MinGrowth));
Resize(newCap);
}
idx = _data->firstFreeIndex;
if (idx >= 0)
{
_data->firstFreeIndex = _data->next[idx];
}
else
{
idx = _data->allocatedIndex++;
}
CheckIndexOutOfBounds(idx);
UnsafeUtility.WriteArrayElement(_data->keys, idx, key);
UnsafeUtility.WriteArrayElement(_data->values, idx, value);
var bucket = GetBucket(key);
_data->next[idx] = _data->buckets[bucket];
_data->buckets[bucket] = idx;
_data->count++;
return idx;
}
public bool Remove(scoped in TKey key)
{
ThrowIfNotCreated();
if (_data->capacity == 0)
{
return false;
}
var removed = false;
var bucket = GetBucket(key);
var prevEntry = -1;
var entryIdx = _data->buckets[bucket];
while (entryIdx >= 0 && entryIdx < _data->capacity)
{
if (UnsafeUtility.ReadArrayElement<TKey>(_data->keys, entryIdx).Equals(key))
{
removed = true;
if (prevEntry < 0)
{
_data->buckets[bucket] = _data->next[entryIdx];
}
else
{
_data->next[prevEntry] = _data->next[entryIdx];
}
var nextIdx = _data->next[entryIdx];
_data->next[entryIdx] = _data->firstFreeIndex;
_data->firstFreeIndex = entryIdx;
entryIdx = nextIdx;
break;
}
else
{
prevEntry = entryIdx;
entryIdx = _data->next[entryIdx];
}
}
if (removed)
_data->count--;
return removed;
}
private void ResizeExact(int newCapacity, int newBucketCapacity)
{
var totalSize = CalculateDataSize(newCapacity, newBucketCapacity, out var keyOffset, out var valueOffset, out var nextOffset, out var bucketOffset);
var oldBuffer = _data->buffer;
var oldKeys = _data->keys;
var oldValues = _data->values;
var oldNext = _data->next;
var oldBuckets = _data->buckets;
var oldBucketCapacity = _data->bucketCapacityMask + 1;
AllocateBuffer(_data, totalSize, keyOffset, valueOffset, nextOffset, bucketOffset, AllocationOption.None);
_data->capacity = newCapacity;
_data->bucketCapacityMask = newBucketCapacity - 1;
Clear();
for (int i = 0, num = oldBucketCapacity; i < num; ++i)
{
for (var idx = oldBuckets[i]; idx != -1; idx = oldNext[idx])
{
Add(oldKeys[idx], oldValues[idx]);
}
}
if (_data->allocationHandle.Free != null && oldBuffer != null)
{
_data->allocationHandle.Free(_data->allocationHandle.State, oldBuffer);
}
#if MHP_ENABLE_SAFETY_CHECKS
_data->memoryHandle.Update(_data->buffer, (nuint)totalSize);
#endif
}
public void Resize(int newCapacity)
{
ThrowIfNotCreated();
newCapacity = Math.Max(newCapacity, _data->count);
var newBucketCapacity = CeilPow2(newCapacity * 2);
if (_data->capacity == newCapacity && (_data->bucketCapacityMask + 1) == newBucketCapacity)
{
return;
}
ResizeExact(newCapacity, newBucketCapacity);
}
public ParallelWriter AsParallelWriter()
{
ThrowIfNotCreated();
return new ParallelWriter(_data);
}
public unsafe struct ParallelWriter
{
internal UnsafeParallelHashMapData<TKey, TValue>* _data;
internal ParallelWriter(UnsafeParallelHashMapData<TKey, TValue>* data)
{
_data = data;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAdd(TKey key, TValue item)
{
if (_data == null || _data->buffer == null)
{
throw new InvalidOperationException("The UnsafeParallelHashMap is not created.");
}
var hash = key.GetHashCode();
var bucket = hash & _data->bucketCapacityMask;
ref var bucketValue = ref _data->buckets[bucket];
var entryIdx = bucketValue;
var nextPtrs = _data->next;
// Optional Fast path Check if item exists. (Does not lock)
if ((uint)entryIdx < (uint)_data->capacity)
{
while (!UnsafeUtility.ReadArrayElement<TKey>(_data->keys, entryIdx).Equals(key))
{
entryIdx = nextPtrs[entryIdx];
if ((uint)entryIdx >= (uint)_data->capacity)
{
break;
}
}
if ((uint)entryIdx < (uint)_data->capacity)
{
// Item already exists
return false;
}
}
// Allocate a new slot from the contiguous array atomically
var idx = Interlocked.Increment(ref _data->allocatedIndex) - 1;
if (idx >= _data->capacity)
{
// UnsafeParallelHashMap does not resize concurrently. Must pre-allocate enough memory.
Interlocked.Decrement(ref _data->allocatedIndex);
throw new InvalidOperationException($"Hash map capacity ({_data->capacity}) exceeded during parallel writing.");
}
// Write our data
UnsafeUtility.WriteArrayElement(_data->keys, idx, key);
UnsafeUtility.WriteArrayElement(_data->values, idx, item);
ref var b = ref _data->buckets[bucket];
// Atomically link into bucket linked list
while (true)
{
var bucketHead = Volatile.Read(ref b);
UnsafeUtility.WriteArrayElement(_data->next, idx, bucketHead);
if (Interlocked.CompareExchange(ref b, idx, bucketHead) == bucketHead)
{
Interlocked.Increment(ref _data->count);
return true;
}
}
}
}
}

View File

@@ -8,7 +8,7 @@ namespace Misaki.HighPerformance.LowLevel.Collections;
/// A dynamically resizing, parallel, lock-free queue using unmanaged chunks. /// A dynamically resizing, parallel, lock-free queue using unmanaged chunks.
/// Uses a very brief spin lock only during chunk allocation, alongside a lock-free segment cache. /// Uses a very brief spin lock only during chunk allocation, alongside a lock-free segment cache.
/// </summary> /// </summary>
public unsafe struct UnsafeChunkedQueue<T> : IDisposable public unsafe struct UnsafeParallelQueue<T> : IDisposable
where T : unmanaged where T : unmanaged
{ {
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
@@ -39,28 +39,34 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
public readonly unsafe struct ParallelProducer public readonly unsafe struct ParallelProducer
{ {
private readonly UnsafeChunkedQueue<T>* _queue; private readonly UnsafeParallelQueue<T>* _queue;
internal ParallelProducer(UnsafeChunkedQueue<T>* queue) internal ParallelProducer(UnsafeParallelQueue<T>* queue)
{ {
_queue = queue; _queue = queue;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(T item) => _queue->Enqueue(item); public void Enqueue(T item)
{
_queue->Enqueue(item);
}
} }
public readonly unsafe struct ParallelConsumer public readonly unsafe struct ParallelConsumer
{ {
private readonly UnsafeChunkedQueue<T>* _queue; private readonly UnsafeParallelQueue<T>* _queue;
internal ParallelConsumer(UnsafeChunkedQueue<T>* queue) internal ParallelConsumer(UnsafeParallelQueue<T>* queue)
{ {
_queue = queue; _queue = queue;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryDequeue(out T item) => _queue->TryDequeue(out item); public bool TryDequeue(out T item)
{
return _queue->TryDequeue(out item);
}
} }
// Pointer representations (nint utilized for straightforward Interlocked compatibility) // Pointer representations (nint utilized for straightforward Interlocked compatibility)
@@ -79,7 +85,15 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
public readonly bool IsCreated => _head != 0; public readonly bool IsCreated => _head != 0;
public UnsafeChunkedQueue(int capacityPerChunk, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None) [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DisposablePtr<UnsafeParallelQueue<T>> Allocate(int capacityPerChunk, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
var pQueue = (UnsafeParallelQueue<T>*)handle.Alloc(handle.State, SizeOf<DisposablePtr<UnsafeParallelQueue<T>>>(), AlignOf<DisposablePtr<UnsafeParallelQueue<T>>>(), AllocationOption.None);
*pQueue = new UnsafeParallelQueue<T>(capacityPerChunk, handle, allocationOption);
return new DisposablePtr<UnsafeParallelQueue<T>>(pQueue);
}
public UnsafeParallelQueue(int capacityPerChunk, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{ {
_chunkCapacity = Math.Max(32, capacityPerChunk); _chunkCapacity = Math.Max(32, capacityPerChunk);
_allocHandle = handle; _allocHandle = handle;
@@ -98,7 +112,7 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
} }
[Obsolete("Use AllocationHandle instead.")] [Obsolete("Use AllocationHandle instead.")]
public UnsafeChunkedQueue(int capacityPerChunk, Allocator allocator, AllocationOption allocationOption = AllocationOption.None) public UnsafeParallelQueue(int capacityPerChunk, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacityPerChunk, AllocationManager.GetAllocationHandle(allocator), allocationOption) : this(capacityPerChunk, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{ {
} }
@@ -312,7 +326,7 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ParallelProducer AsParallelProducer() public ParallelProducer AsParallelProducer()
{ {
return new ParallelProducer((UnsafeChunkedQueue<T>*)Unsafe.AsPointer(ref this)); return new ParallelProducer((UnsafeParallelQueue<T>*)Unsafe.AsPointer(ref this));
} }
/// <summary> /// <summary>
@@ -323,7 +337,7 @@ public unsafe struct UnsafeChunkedQueue<T> : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ParallelConsumer AsParallelConsumer() public ParallelConsumer AsParallelConsumer()
{ {
return new ParallelConsumer((UnsafeChunkedQueue<T>*)Unsafe.AsPointer(ref this)); return new ParallelConsumer((UnsafeParallelQueue<T>*)Unsafe.AsPointer(ref this));
} }
public void Dispose() public void Dispose()

View File

@@ -1,7 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts; using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Diagnostics; using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@@ -43,19 +41,38 @@ internal class UnsafeSlotMapDebugView<T>
public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T> public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
where T : unmanaged where T : unmanaged
{ {
private struct SlotEntry
{
public T value;
public int generation;
}
private const int _CHUNK_SHIFT = 8;
private const int _CHUNK_SIZE = 1 << _CHUNK_SHIFT;
private const int _CHUNK_MASK = _CHUNK_SIZE - 1;
public ref struct Enumerator public ref struct Enumerator
{ {
private ref UnsafeSlotMap<T> _collection; private ref UnsafeSlotMap<T> _collection;
private int _currentIndex; private int _currentIndex;
public readonly ref T Current => ref _collection._data[_currentIndex];
public Enumerator(ref UnsafeSlotMap<T> collection) public Enumerator(ref UnsafeSlotMap<T> collection)
{ {
_collection = ref collection; _collection = ref collection;
_currentIndex = -1; _currentIndex = -1;
} }
public readonly ref T Current
{
get
{
var chunks = _collection._chunks;
var chunkIdx = _currentIndex >> _CHUNK_SHIFT;
var localIdx = _currentIndex & _CHUNK_MASK;
return ref chunks[chunkIdx][localIdx].value;
}
}
public bool MoveNext() public bool MoveNext()
{ {
_currentIndex = _collection._validBits.NextSetBit(_currentIndex + 1); _currentIndex = _collection._validBits.NextSetBit(_currentIndex + 1);
@@ -68,18 +85,20 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
} }
} }
private UnsafeArray<T> _data; private UnsafeArray<UnsafeArray<SlotEntry>> _chunks;
private UnsafeArray<int> _generations;
private UnsafeQueue<int> _freeSlots; private UnsafeQueue<int> _freeSlots;
private UnsafeBitSet _validBits; private UnsafeBitSet _validBits;
private AllocationHandle _handle;
private AllocationOption _allocationOption;
private int _count; private int _count;
private int _capacity; private int _capacity;
private int _nextSlotIndex;
public readonly int Count => _count; public readonly int Count => _count;
public readonly int Capacity => _capacity; public readonly int Capacity => _capacity;
public readonly bool IsCreated => _data.IsCreated && _generations.IsCreated && _freeSlots.IsCreated && _validBits.IsCreated; public readonly bool IsCreated => _chunks.IsCreated && _freeSlots.IsCreated && _validBits.IsCreated;
/// <summary> /// <summary>
/// Initializes a new instance of UnsafeSlotMap with a default size of 1 and a persistent allocation handle. /// Initializes a new instance of UnsafeSlotMap with a default size of 1 and a persistent allocation handle.
@@ -104,19 +123,34 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero."); throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
} }
_data = new UnsafeArray<T>(capacity, handle, allocationOption); _handle = handle;
_generations = new UnsafeArray<int>(capacity, handle, allocationOption); _allocationOption = allocationOption;
var initialChunks = (capacity + _CHUNK_MASK) / _CHUNK_SIZE;
if (initialChunks == 0)
initialChunks = 1;
_capacity = initialChunks * _CHUNK_SIZE;
_chunks = new UnsafeArray<UnsafeArray<SlotEntry>>(initialChunks, handle, allocationOption);
for (var i = 0; i < initialChunks; i++)
{
_chunks[i] = new UnsafeArray<SlotEntry>(_CHUNK_SIZE, handle, allocationOption);
if (!allocationOption.HasFlag(AllocationOption.Clear))
{
_chunks[i].AsSpan().Clear();
}
}
_freeSlots = new UnsafeQueue<int>(capacity, handle, allocationOption); _freeSlots = new UnsafeQueue<int>(capacity, handle, allocationOption);
_validBits = new UnsafeBitSet(capacity, handle, allocationOption); _validBits = new UnsafeBitSet(_capacity, handle, allocationOption);
if (!allocationOption.HasFlag(AllocationOption.Clear)) if (!allocationOption.HasFlag(AllocationOption.Clear))
{ {
_generations.AsSpan().Clear();
_validBits.ClearAll(); _validBits.ClearAll();
} }
_count = 0; _count = 0;
_capacity = capacity; _nextSlotIndex = 0;
} }
/// <summary> /// <summary>
@@ -139,6 +173,33 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
return new Enumerator(ref this); return new Enumerator(ref this);
} }
[MethodImpl(MethodImplOptions.NoInlining)]
private void EnsureChunkExists(int requiredChunkIndex)
{
if (requiredChunkIndex < _chunks.Length)
return;
var newChunkCount = _chunks.Length;
while (newChunkCount <= requiredChunkIndex)
{
newChunkCount *= 2;
}
_chunks.Resize(newChunkCount, _allocationOption);
for (var i = _capacity / _CHUNK_SIZE; i < newChunkCount; i++)
{
_chunks[i] = new UnsafeArray<SlotEntry>(_CHUNK_SIZE, _handle, _allocationOption);
if (!_allocationOption.HasFlag(AllocationOption.Clear))
{
_chunks[i].AsSpan().Clear();
}
}
_capacity = newChunkCount * _CHUNK_SIZE;
_validBits.Resize(_capacity, _allocationOption);
}
/// <summary> /// <summary>
/// Adds the specified item to the collection and returns the index of the slot where it was stored. /// Adds the specified item to the collection and returns the index of the slot where it was stored.
/// </summary> /// </summary>
@@ -147,28 +208,40 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <returns>The index of the slot in which the item was stored.</returns> /// <returns>The index of the slot in which the item was stored.</returns>
public int Add(T item, out int generation) public int Add(T item, out int generation)
{ {
if (_count >= _capacity) if (_freeSlots.Count > 0)
{ {
Resize(Math.Max(1, _capacity * 2)); var slotIndex = _freeSlots.Dequeue();
var chunkIdx = slotIndex >> _CHUNK_SHIFT;
var localIdx = slotIndex & _CHUNK_MASK;
ref var slot = ref _chunks[chunkIdx][localIdx];
generation = slot.generation;
slot.value = item;
_validBits.SetBit(slotIndex);
_count++;
return slotIndex;
} }
int index; var newSlotIndex = _nextSlotIndex++;
if (_freeSlots.Count == 0) var newChunkIdx = newSlotIndex >> _CHUNK_SHIFT;
var newLocalIdx = newSlotIndex & _CHUNK_MASK;
if (newChunkIdx >= _chunks.Length)
{ {
index = _count; EnsureChunkExists(newChunkIdx);
}
else
{
index = _freeSlots.Dequeue();
} }
_data[index] = item; ref var newSlot = ref _chunks[newChunkIdx][newLocalIdx];
_validBits.SetBit(index); newSlot.value = item;
newSlot.generation = 0;
_validBits.SetBit(newSlotIndex);
generation = 0;
_count++; _count++;
return newSlotIndex;
generation = _generations[index];
return index;
} }
/// <summary> /// <summary>
@@ -181,23 +254,32 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
public bool Remove(int slotIndex, int generation, out T item) public bool Remove(int slotIndex, int generation, out T item)
{ {
item = default; item = default;
if (slotIndex < 0 || slotIndex >= _capacity) if (slotIndex < 0)
{ {
return false; return false;
} }
ref var gen = ref _generations[slotIndex]; var chunkIdx = slotIndex >> _CHUNK_SHIFT;
if (gen != generation) var localIdx = slotIndex & _CHUNK_MASK;
if (chunkIdx >= _chunks.Length)
{ {
return false; return false;
} }
item = _data[slotIndex]; ref var slot = ref _chunks[chunkIdx][localIdx];
gen++; if (!_validBits.IsSet(slotIndex) || slot.generation != generation)
{
return false;
}
slot.generation++;
_validBits.ClearBit(slotIndex); _validBits.ClearBit(slotIndex);
_freeSlots.Enqueue(slotIndex); item = slot.value;
slot.value = default;
_freeSlots.Enqueue(slotIndex);
_count--; _count--;
return true; return true;
@@ -223,17 +305,8 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <returns>true if the slot at the specified index is valid and its Generation matches the specified value; otherwise, false.</returns> /// <returns>true if the slot at the specified index is valid and its Generation matches the specified value; otherwise, false.</returns>
public readonly bool Contains(int slotIndex, int generation) public readonly bool Contains(int slotIndex, int generation)
{ {
if (slotIndex < 0 || slotIndex >= _capacity) GetElementReferenceAt(slotIndex, generation, out var exist);
{ return exist;
return false;
}
if (_validBits.IsSet(slotIndex) && _generations[slotIndex] == generation)
{
return true;
}
return false;
} }
/// <summary> /// <summary>
@@ -247,14 +320,17 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <returns>true if the element at the specified slot index and Generation is found; otherwise, false.</returns> /// <returns>true if the element at the specified slot index and Generation is found; otherwise, false.</returns>
public readonly bool TryGetElementAt(int slotIndex, int generation, out T value) public readonly bool TryGetElementAt(int slotIndex, int generation, out T value)
{ {
if (!Contains(slotIndex, generation)) ref var val = ref GetElementReferenceAt(slotIndex, generation, out var exist);
if (exist)
{
value = val;
return true;
}
else
{ {
value = default; value = default;
return false; return false;
} }
value = _data[slotIndex];
return true;
} }
/// <summary> /// <summary>
@@ -267,12 +343,12 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <exception cref="InvalidOperationException">Thrown when the specified slot is not occupied or the Generation does not match.</exception> /// <exception cref="InvalidOperationException">Thrown when the specified slot is not occupied or the Generation does not match.</exception>
public readonly T GetElementAt(int slotIndex, int generation) public readonly T GetElementAt(int slotIndex, int generation)
{ {
if (!Contains(slotIndex, generation)) if (!TryGetElementAt(slotIndex, generation, out var value))
{ {
throw new InvalidOperationException("The specified slot is not occupied or the generation does not match."); throw new InvalidOperationException("The specified slot is not occupied or the generation does not match.");
} }
return _data[slotIndex]; return value;
} }
/// <summary> /// <summary>
@@ -283,50 +359,83 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <param name="generation">The expected Generation value for the slot. Used to verify that the slot has not been recycled or replaced.</param> /// <param name="generation">The expected Generation value for the slot. Used to verify that the slot has not been recycled or replaced.</param>
/// <param name="exist">When this method returns, contains <see langword="true"/> if a valid element exists at the specified slot and Generation; otherwise, <see langword="false"/>.</param> /// <param name="exist">When this method returns, contains <see langword="true"/> if a valid element exists at the specified slot and Generation; otherwise, <see langword="false"/>.</param>
/// <returns>A reference to the element of type <typeparamref name="T"/> at the specified slot and Generation if it exists; otherwise, a null reference.</returns> /// <returns>A reference to the element of type <typeparamref name="T"/> at the specified slot and Generation if it exists; otherwise, a null reference.</returns>
public ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist) public readonly ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist)
{ {
if (!Contains(slotIndex, generation)) if (slotIndex < 0)
{ {
exist = false; exist = false;
return ref Unsafe.NullRef<T>(); return ref Unsafe.NullRef<T>();
} }
exist = true; var chunkIdx = slotIndex >> _CHUNK_SHIFT;
return ref _data[slotIndex]; var localIdx = slotIndex & _CHUNK_MASK;
if (chunkIdx >= _chunks.Length)
{
exist = false;
return ref Unsafe.NullRef<T>();
}
ref var slot = ref _chunks[chunkIdx][localIdx];
if (_validBits.IsSet(slotIndex) && slot.generation == generation)
{
exist = true;
return ref slot.value;
}
exist = false;
return ref Unsafe.NullRef<T>();
} }
public void Resize(int newSize, AllocationOption option = AllocationOption.None) public void Resize(int newSize, AllocationOption option = AllocationOption.None)
{ {
_data.Resize(newSize, option); var requiredChunkIndex = (newSize + _CHUNK_MASK) / _CHUNK_SIZE - 1;
_generations.Resize(newSize, option | AllocationOption.Clear); EnsureChunkExists(requiredChunkIndex);
_freeSlots.Resize(newSize, option); _freeSlots.Resize(newSize, option);
_validBits.Resize(newSize, option); _validBits.Resize(newSize, option);
_capacity = newSize;
} }
public void Clear() public void Clear()
{ {
_generations.Clear(); for (var i = 0; i < _chunks.Length; i++)
{
if (_chunks[i].IsCreated)
{
var chunk = _chunks[i];
for (var slot = 0; slot < _CHUNK_SIZE; slot++)
{
chunk[slot].generation = 0;
chunk[slot].value = default;
}
}
}
_freeSlots.Clear(); _freeSlots.Clear();
_validBits.ClearAll(); _validBits.ClearAll();
_count = 0; _count = 0;
_nextSlotIndex = 0;
} }
public readonly void* GetUnsafePtr() public readonly void* GetUnsafePtr()
{ {
return (T*)_data.GetUnsafePtr(); return null;
} }
public void Dispose() public void Dispose()
{ {
_data.Dispose(); for (var i = 0; i < _chunks.Length; i++)
_generations.Dispose(); {
if (_chunks[i].IsCreated)
{
_chunks[i].Dispose();
}
}
_chunks.Dispose();
_freeSlots.Dispose(); _freeSlots.Dispose();
_validBits.Dispose(); _validBits.Dispose();
_count = 0; _count = 0;
_capacity = 0; _capacity = 0;
_nextSlotIndex = 0;
} }
} }

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.6.13</AssemblyVersion> <AssemblyVersion>1.6.14</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl> <RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>

View File

@@ -392,5 +392,16 @@ public static unsafe partial class MemoryUtility
{ {
return Marshal.SizeOf<AlignOfHelper<T>>() - Marshal.SizeOf<T>(); return Marshal.SizeOf<AlignOfHelper<T>>() - Marshal.SizeOf<T>();
} }
public static nuint AlignUp(nuint value, nuint alignment)
{
if (alignment == 0)
{
throw new ArgumentException("Alignment must be greater than zero.", nameof(alignment));
}
var mask = alignment - 1;
return (value + mask) & ~mask;
}
} }

View File

@@ -0,0 +1,95 @@
using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Collections;
using Misaki.HighPerformance.Mathematics;
namespace Misaki.HighPerformance.Test.Benchmark;
public struct BigStruct
{
public int a, b, c, d, e, f, g, h, i, j;
public long k, l, m, n, o, p, q, r, s, t;
}
public class Integer
{
public BigStruct value;
}
public class ConcurrentSlotMapBenchmark
{
private readonly ConcurrentSlotMap<BigStruct> _concurrentMap = new ConcurrentSlotMap<BigStruct>(1000);
private readonly SlotMap<BigStruct> _slotMap = new SlotMap<BigStruct>(1000);
private readonly BigStruct[] _slots = new BigStruct[1000];
private readonly Integer[] _objects = new Integer[1000];
private readonly int2[] _randomIndices1 = new int2[1000];
private readonly int2[] _randomIndices2 = new int2[1000];
private readonly int[] _randomSlots = new int[1000];
[GlobalSetup]
public void Setup()
{
for (var i = 0; i < 1000; i++)
{
var element = new BigStruct
{
a = i
};
var id = _concurrentMap.Add(element, out var generation);
_randomIndices1[i] = new int2(id, generation);
id = _slotMap.Add(element, out generation);
_randomIndices2[i] = new int2(id, generation);
_slots[i] = element;
_objects[i] = new Integer { value = element };
_randomSlots[i] = i;
}
Random.Shared.Shuffle(_randomIndices1);
Random.Shared.Shuffle(_randomIndices2);
Random.Shared.Shuffle(_randomSlots);
}
[Benchmark]
public void ConcurrentSlotMap()
{
for (var i = 0; i < 1000; i++)
{
var v = _randomIndices1[i];
ref var element = ref _concurrentMap.GetElementReferenceAt(v.x, v.y, out var _);
element.a += 1;
}
}
[Benchmark]
public void SlotMap()
{
for (var i = 0; i < 1000; i++)
{
var v = _randomIndices2[i];
ref var element = ref _slotMap.GetElementReferenceAt(v.x, v.y, out var _);
element.a += 1;
}
}
[Benchmark]
public void Array()
{
for (var i = 0; i < 1000; i++)
{
_slots[_randomSlots[i]].a += 1;
}
}
[Benchmark]
public void ObjectArray()
{
for (var i = 0; i < 1000; i++)
{
_objects[_randomSlots[i]].value.a += 1;
}
}
}

View File

@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Misaki.HighPerformance.Test.Benchmark; namespace Misaki.HighPerformance.Test.Benchmark;
public class ReadWriteLockBenchmark public class ReadWriteLockBenchmark

View File

@@ -1,17 +1,20 @@
using BenchmarkDotNet.Running;
using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections; using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Utilities;
using Misaki.HighPerformance.Test.Benchmark;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
//BenchmarkRunner.Run<SPMDBenchmark>(); BenchmarkRunner.Run<ConcurrentSlotMapBenchmark>();
AllocationManager.Initialize(AllocationManagerInitOpts.Default); //AllocationManager.Initialize(AllocationManagerInitOpts.Default);
var set = new UnsafeBitSet(100, AllocationHandle.Persistent, AllocationOption.Clear); //var set = new UnsafeBitSet(100, AllocationHandle.Persistent, AllocationOption.Clear);
set.SetBit(0); //set.SetBit(0);
Console.WriteLine(set.NextSetBit(0)); //Console.WriteLine(set.NextSetBit(0));
set.Dispose(); //set.Dispose();
AllocationManager.Dispose(); //AllocationManager.Dispose();
//unsafe //unsafe
//{ //{

View File

@@ -15,7 +15,7 @@ public class TestUnsafeChunkedQueue
[TestMethod] [TestMethod]
public void BasicEnqueueDequeueTest() public void BasicEnqueueDequeueTest()
{ {
using var queue = new UnsafeChunkedQueue<int>(32, AllocationHandle.Persistent); using var queue = new UnsafeParallelQueue<int>(32, AllocationHandle.Persistent);
Assert.IsTrue(queue.IsCreated); Assert.IsTrue(queue.IsCreated);
@@ -35,7 +35,7 @@ public class TestUnsafeChunkedQueue
public void ChunkExpansionTest() public void ChunkExpansionTest()
{ {
// Force chunk expansions by enqueuing more than the chunk capacity // Force chunk expansions by enqueuing more than the chunk capacity
using var queue = new UnsafeChunkedQueue<int>(16, AllocationHandle.Persistent); using var queue = new UnsafeParallelQueue<int>(16, AllocationHandle.Persistent);
var totalItems = 100; var totalItems = 100;
@@ -57,7 +57,7 @@ public class TestUnsafeChunkedQueue
public void ConcurrentEnqueueDequeueTest() public void ConcurrentEnqueueDequeueTest()
{ {
// Multi-threaded stress test to verify lock-free safety and chunk caching // Multi-threaded stress test to verify lock-free safety and chunk caching
using var queue = new UnsafeChunkedQueue<int>(64, AllocationHandle.Persistent); using var queue = new UnsafeParallelQueue<int>(64, AllocationHandle.Persistent);
var totalElements = 100_000; var totalElements = 100_000;
var enqueueTask = Task.Run(() => var enqueueTask = Task.Run(() =>

View File

@@ -0,0 +1,154 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
namespace Misaki.HighPerformance.Test.UnitTest.Collections;
[TestClass]
public class TestUnsafeParallelHashMap
{
[TestMethod]
public void TestParallelWrite()
{
using var map = new UnsafeParallelHashMap<int, int>(10000, 1, AllocationHandle.Temp, AllocationOption.None);
var writer = map.AsParallelWriter();
Parallel.For(0, 10000, i =>
{
writer.TryAdd(i, i * 2);
});
Assert.AreEqual(10000, map.Count);
for (var i = 0; i < 10000; i++)
{
Assert.IsTrue(map.TryGetValue(i, out var val));
Assert.AreEqual(i * 2, val);
}
}
[TestMethod]
public void TestBasicOperations()
{
using var map = new UnsafeParallelHashMap<int, int>(16, 1, AllocationHandle.Temp, AllocationOption.None);
Assert.IsTrue(map.IsEmpty);
Assert.AreEqual(0, map.Count);
// Add
map.Add(1, 10);
map.Add(2, 20);
map.Add(3, 30);
Assert.IsFalse(map.IsEmpty);
Assert.AreEqual(3, map.Count);
// TryGetValue existing
Assert.IsTrue(map.TryGetValue(2, out var val));
Assert.AreEqual(20, val);
// TryGetValue non-existing
Assert.IsFalse(map.TryGetValue(4, out _));
// Remove existing
Assert.IsTrue(map.Remove(2));
Assert.AreEqual(2, map.Count);
Assert.IsFalse(map.TryGetValue(2, out _));
// Remove non-existing
Assert.IsFalse(map.Remove(4));
// Clear
map.Clear();
Assert.AreEqual(0, map.Count);
Assert.IsTrue(map.IsEmpty);
}
[TestMethod]
public void TestResize()
{
using var map = new UnsafeParallelHashMap<int, int>(16, 1, AllocationHandle.Temp, AllocationOption.None);
// Single thread adds causing resize
for (var i = 0; i < 1000; i++)
{
map.Add(i, i * 10);
}
Assert.AreEqual(1000, map.Count);
Assert.IsTrue(map.Capacity >= 1000);
for (var i = 0; i < 1000; i++)
{
Assert.IsTrue(map.TryGetValue(i, out var val));
Assert.AreEqual(i * 10, val);
}
}
private struct BadKey : IEquatable<BadKey>
{
public int Id;
public BadKey(int id) => Id = id;
public bool Equals(BadKey other) => Id == other.Id;
public override int GetHashCode() => 1; // Force collision
}
[TestMethod]
public void TestHashCollisions()
{
using var map = new UnsafeParallelHashMap<BadKey, int>(16, 1, AllocationHandle.Temp, AllocationOption.None);
for (var i = 0; i < 10; i++)
{
map.Add(new BadKey(i), i * 5);
}
Assert.AreEqual(10, map.Count);
// Verify we can retrieve them all out of the same bucket
for (var i = 0; i < 10; i++)
{
Assert.IsTrue(map.TryGetValue(new BadKey(i), out var val));
Assert.AreEqual(i * 5, val);
}
// Remove from the middle of the linked list
Assert.IsTrue(map.Remove(new BadKey(5)));
Assert.IsFalse(map.TryGetValue(new BadKey(5), out _));
Assert.AreEqual(9, map.Count);
// Make sure everything else is intact
Assert.IsTrue(map.TryGetValue(new BadKey(6), out var val6));
Assert.AreEqual(6 * 5, val6);
}
[TestMethod]
public void TestAddDuplicate()
{
using var map = new UnsafeParallelHashMap<int, int>(16, 1, AllocationHandle.Temp, AllocationOption.None);
Assert.AreEqual(0, map.Add(1, 100));
// Adding again should return -1
Assert.AreEqual(-1, map.Add(1, 200));
Assert.AreEqual(1, map.Count);
var writer = map.AsParallelWriter();
Assert.IsFalse(writer.TryAdd(1, 300));
}
[TestMethod]
public void TestParallelWriteExceedsCapacity()
{
using var map = new UnsafeParallelHashMap<int, int>(50, 1, AllocationHandle.Temp, AllocationOption.None);
var writer = map.AsParallelWriter();
// The exact exception will be wrapped in AggregateException by Parallel.For
Assert.ThrowsExactly<AggregateException>(() =>
{
Parallel.For(0, 100, i =>
{
writer.TryAdd(i, i);
});
});
}
}

View File

@@ -71,6 +71,23 @@ public class TestUnsafeSlotMap
} }
} }
[TestMethod]
public void ReferenceValidAfterResize()
{
var id = _slotMap.Add(10, out var gen);
ref var value = ref _slotMap.GetElementReferenceAt(id, gen, out _);
Assert.AreEqual(10, value);
// Force resize
for (var i = 0; i < 20; i++)
{
_slotMap.Add(i, out _);
}
Assert.AreEqual(10, value);
}
[TestMethod] [TestMethod]
public void Clear() public void Clear()
{ {

View File

@@ -9,7 +9,7 @@ namespace Misaki.HighPerformance.Test.UnitTest.Jobs;
[TestClass] [TestClass]
[DoNotParallelize] [DoNotParallelize]
public unsafe class TestJobSystem public class TestJobSystem
{ {
private static JobScheduler s_jobScheduler = null!; private static JobScheduler s_jobScheduler = null!;
@@ -32,7 +32,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void SingleJob() public unsafe void SingleJob()
{ {
var result = stackalloc float[1]; var result = stackalloc float[1];
var job = new TwoSumJob var job = new TwoSumJob
@@ -49,7 +49,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void JobDependency() public unsafe void JobDependency()
{ {
var result = stackalloc float[1]; var result = stackalloc float[1];
var job1 = new TwoSumJob var job1 = new TwoSumJob
@@ -74,7 +74,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void CompletedDependency() public unsafe void CompletedDependency()
{ {
var result = stackalloc float[1]; var result = stackalloc float[1];
var job1 = new TwoSumJob var job1 = new TwoSumJob
@@ -100,7 +100,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void CombineDependencies() public unsafe void CombineDependencies()
{ {
var result = stackalloc float[1]; var result = stackalloc float[1];
var job1 = new TwoSumJob var job1 = new TwoSumJob
@@ -135,7 +135,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void SingleParallelJob() public unsafe void SingleParallelJob()
{ {
const int size = 1000; const int size = 1000;
var result = stackalloc float[size]; var result = stackalloc float[size];
@@ -167,7 +167,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void ChainJob() public unsafe void ChainJob()
{ {
const int arraySize = 10000; const int arraySize = 10000;
@@ -209,7 +209,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void WaitAll() public unsafe void WaitAll()
{ {
var result1 = stackalloc float[1]; var result1 = stackalloc float[1];
var result2 = stackalloc float[1]; var result2 = stackalloc float[1];
@@ -235,8 +235,42 @@ public unsafe class TestJobSystem
Assert.AreEqual(JobState.Completed, s_jobScheduler.GetJobStatus(handle2)); Assert.AreEqual(JobState.Completed, s_jobScheduler.GetJobStatus(handle2));
} }
[TestMethod] [TestMethod]
public void WaitAny() public async Task WaitAllAsync()
{
AddJob job1;
AddJob job2;
unsafe
{
var result1 = stackalloc float[1];
var result2 = stackalloc float[1];
job1 = new AddJob
{
value = 1.0f,
result = result1
};
job2 = new AddJob
{
value = 1.0f,
result = result2
};
}
var handle1 = s_jobScheduler.Schedule(ref job1);
var handle2 = s_jobScheduler.Schedule(ref job2);
await s_jobScheduler.WaitAllAsync(new Memory<JobHandle>(new[] { handle1, handle2 }));
Assert.AreEqual(JobState.Completed, s_jobScheduler.GetJobStatus(handle1));
Assert.AreEqual(JobState.Completed, s_jobScheduler.GetJobStatus(handle2));
}
[TestMethod]
public unsafe void WaitAny()
{ {
var result1 = stackalloc float[1]; var result1 = stackalloc float[1];
var result2 = stackalloc float[1]; var result2 = stackalloc float[1];
@@ -262,7 +296,7 @@ public unsafe class TestJobSystem
} }
[TestMethod] [TestMethod]
public void SPMDCorrectness() public unsafe void SPMDCorrectness()
{ {
const int size = 8; const int size = 8;

View File

@@ -14,9 +14,9 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
public int isValid; public int isValid;
} }
private const int CHUNK_SHIFT = 8; private const int _CHUNK_SHIFT = 8;
private const int CHUNK_SIZE = 1 << CHUNK_SHIFT; private const int _CHUNK_SIZE = 1 << _CHUNK_SHIFT;
private const int CHUNK_MASK = CHUNK_SIZE - 1; private const int _CHUNK_MASK = _CHUNK_SIZE - 1;
public struct Enumerator : IEnumerator<T> public struct Enumerator : IEnumerator<T>
{ {
@@ -34,8 +34,8 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
get get
{ {
var chunks = _slotMap._chunks; var chunks = _slotMap._chunks;
int chunkIdx = _currentIndex >> CHUNK_SHIFT; int chunkIdx = _currentIndex >> _CHUNK_SHIFT;
int localIdx = _currentIndex & CHUNK_MASK; int localIdx = _currentIndex & _CHUNK_MASK;
return chunks[chunkIdx][localIdx].value!; return chunks[chunkIdx][localIdx].value!;
} }
} }
@@ -49,8 +49,8 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
while (++_currentIndex < maxIndex) while (++_currentIndex < maxIndex)
{ {
int chunkIdx = _currentIndex >> CHUNK_SHIFT; int chunkIdx = _currentIndex >> _CHUNK_SHIFT;
int localIdx = _currentIndex & CHUNK_MASK; int localIdx = _currentIndex & _CHUNK_MASK;
if (chunkIdx < chunks.Length && Volatile.Read(ref chunks[chunkIdx][localIdx].isValid) == 1) if (chunkIdx < chunks.Length && Volatile.Read(ref chunks[chunkIdx][localIdx].isValid) == 1)
{ {
@@ -94,14 +94,14 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
_nextSlotIndex = 0; _nextSlotIndex = 0;
_isResizing = 0; _isResizing = 0;
int initialChunks = (initialCapacity + CHUNK_MASK) / CHUNK_SIZE; int initialChunks = (initialCapacity + _CHUNK_MASK) / _CHUNK_SIZE;
if (initialChunks == 0) initialChunks = 1; if (initialChunks == 0) initialChunks = 1;
_capacity = initialChunks * CHUNK_SIZE; _capacity = initialChunks * _CHUNK_SIZE;
_chunks = new SlotEntry[initialChunks][]; _chunks = new SlotEntry[initialChunks][];
for (int i = 0; i < initialChunks; i++) for (int i = 0; i < initialChunks; i++)
{ {
_chunks[i] = new SlotEntry[CHUNK_SIZE]; _chunks[i] = new SlotEntry[_CHUNK_SIZE];
} }
_freeSlots = new(); _freeSlots = new();
@@ -144,12 +144,12 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
// Initialize new chunks // Initialize new chunks
for (var i = oldChunks.Length; i < newChunkCount; i++) for (var i = oldChunks.Length; i < newChunkCount; i++)
{ {
newChunks[i] = new SlotEntry[CHUNK_SIZE]; newChunks[i] = new SlotEntry[_CHUNK_SIZE];
} }
// Atomically update the array reference and capacity // Atomically update the array reference and capacity
_chunks = newChunks; _chunks = newChunks;
Volatile.Write(ref _capacity, newChunkCount * CHUNK_SIZE); Volatile.Write(ref _capacity, newChunkCount * _CHUNK_SIZE);
} }
finally finally
{ {
@@ -166,8 +166,8 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
if (_freeSlots.TryDequeue(out var slotIndex)) if (_freeSlots.TryDequeue(out var slotIndex))
{ {
var chunks = _chunks; var chunks = _chunks;
int chunkIdx = slotIndex >> CHUNK_SHIFT; int chunkIdx = slotIndex >> _CHUNK_SHIFT;
int localIdx = slotIndex & CHUNK_MASK; int localIdx = slotIndex & _CHUNK_MASK;
if (chunkIdx < chunks.Length) if (chunkIdx < chunks.Length)
{ {
@@ -199,8 +199,8 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
// Need a new slot // Need a new slot
int newSlotIndex = Interlocked.Increment(ref _nextSlotIndex) - 1; int newSlotIndex = Interlocked.Increment(ref _nextSlotIndex) - 1;
int newChunkIdx = newSlotIndex >> CHUNK_SHIFT; int newChunkIdx = newSlotIndex >> _CHUNK_SHIFT;
int newLocalIdx = newSlotIndex & CHUNK_MASK; int newLocalIdx = newSlotIndex & _CHUNK_MASK;
var currentChunks = _chunks; var currentChunks = _chunks;
if (newChunkIdx >= currentChunks.Length) if (newChunkIdx >= currentChunks.Length)
@@ -221,6 +221,7 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(int slotIndex, int generation) public bool Remove(int slotIndex, int generation)
{ {
return Remove(slotIndex, generation, out _); return Remove(slotIndex, generation, out _);
@@ -235,8 +236,8 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
} }
var chunks = _chunks; var chunks = _chunks;
int chunkIdx = slotIndex >> CHUNK_SHIFT; int chunkIdx = slotIndex >> _CHUNK_SHIFT;
int localIdx = slotIndex & CHUNK_MASK; int localIdx = slotIndex & _CHUNK_MASK;
if (chunkIdx >= chunks.Length) if (chunkIdx >= chunks.Length)
{ {
@@ -269,20 +270,48 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
return false; // Another thread already removed it return false; // Another thread already removed it
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(int slotIndex, int generation) public bool Contains(int slotIndex, int generation)
{
GetElementReferenceAt(slotIndex, generation, out var exist);
return exist;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetElement(int slotIndex, int generation, [MaybeNullWhen(false)] out T value)
{
value = GetElementReferenceAt(slotIndex, generation, out var exist);
return exist;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetElementAt(int slotIndex, int generation)
{
if (!TryGetElement(slotIndex, generation, out var value))
{
throw new InvalidOperationException($"Slot {slotIndex} is not occupied or generation mismatch.");
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist)
{ {
if (slotIndex < 0) if (slotIndex < 0)
{ {
return false; exist = false;
return ref Unsafe.NullRef<T>();
} }
var chunks = _chunks; var chunks = _chunks;
int chunkIdx = slotIndex >> CHUNK_SHIFT; int chunkIdx = slotIndex >> _CHUNK_SHIFT;
int localIdx = slotIndex & CHUNK_MASK; int localIdx = slotIndex & _CHUNK_MASK;
if (chunkIdx >= chunks.Length) if (chunkIdx >= chunks.Length)
{ {
return false; exist = false;
return ref Unsafe.NullRef<T>();
} }
ref var slot = ref chunks[chunkIdx][localIdx]; ref var slot = ref chunks[chunkIdx][localIdx];
@@ -294,70 +323,29 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
{ {
if (Volatile.Read(ref slot.isValid) == 1 && Volatile.Read(ref slot.generation) == generation) if (Volatile.Read(ref slot.isValid) == 1 && Volatile.Read(ref slot.generation) == generation)
{ {
return true; exist = true;
return ref chunks[chunkIdx][localIdx].value!;
} }
} }
return false; exist = false;
} return ref Unsafe.NullRef<T>();
public bool TryGetElement(int slotIndex, int generation, [MaybeNullWhen(false)] out T value)
{
if (!Contains(slotIndex, generation))
{
value = default;
return false;
}
var chunks = _chunks;
int chunkIdx = slotIndex >> CHUNK_SHIFT;
int localIdx = slotIndex & CHUNK_MASK;
value = chunks[chunkIdx][localIdx].value!;
return true;
}
public T GetElementAt(int slotIndex, int generation)
{
if (!TryGetElement(slotIndex, generation, out var value))
{
throw new InvalidOperationException($"Slot {slotIndex} is not occupied or generation mismatch.");
}
return value;
}
public ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist)
{
if (!Contains(slotIndex, generation))
{
exist = false;
return ref Unsafe.NullRef<T>();
}
var chunks = _chunks;
int chunkIdx = slotIndex >> CHUNK_SHIFT;
int localIdx = slotIndex & CHUNK_MASK;
exist = true;
return ref chunks[chunkIdx][localIdx].value!;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool UpdateElement(int slotIndex, int generation, T newValue) public bool UpdateElement(int slotIndex, int generation, T newValue)
{ {
if (!Contains(slotIndex, generation)) ref var slotRef = ref GetElementReferenceAt(slotIndex, generation, out var exist);
if (!exist)
{ {
return false; return false;
} }
var chunks = _chunks; slotRef = newValue;
int chunkIdx = slotIndex >> CHUNK_SHIFT;
int localIdx = slotIndex & CHUNK_MASK;
chunks[chunkIdx][localIdx].value = newValue;
return true; return true;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear() public void Clear()
{ {
// Reset counters // Reset counters
@@ -369,7 +357,7 @@ public class ConcurrentSlotMap<T> : IEnumerable<T>
for (var c = 0; c < chunks.Length; c++) for (var c = 0; c < chunks.Length; c++)
{ {
var chunk = chunks[c]; var chunk = chunks[c];
for (var i = 0; i < CHUNK_SIZE; i++) for (var i = 0; i < _CHUNK_SIZE; i++)
{ {
ref var slot = ref chunk[i]; ref var slot = ref chunk[i];
Volatile.Write(ref slot.isValid, 0); Volatile.Write(ref slot.isValid, 0);

View File

@@ -65,6 +65,7 @@ public class SlotMap<T> : IEnumerable<T>
_freeSlots = new(initialCapacity); _freeSlots = new(initialCapacity);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Resize() private void Resize()
{ {
var newCapacity = _capacity * 2; var newCapacity = _capacity * 2;
@@ -78,6 +79,7 @@ public class SlotMap<T> : IEnumerable<T>
_capacity = newCapacity; _capacity = newCapacity;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Add(T item, out int generation) public int Add(T item, out int generation)
{ {
if (_count >= _capacity) if (_count >= _capacity)
@@ -104,6 +106,7 @@ public class SlotMap<T> : IEnumerable<T>
return slotIndex; return slotIndex;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(int slotIndex, int generation) public bool Contains(int slotIndex, int generation)
{ {
if (slotIndex < 0 || slotIndex >= Volatile.Read(ref _capacity)) if (slotIndex < 0 || slotIndex >= Volatile.Read(ref _capacity))
@@ -119,6 +122,7 @@ public class SlotMap<T> : IEnumerable<T>
return false; return false;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(int slotIndex, int generation) public bool Remove(int slotIndex, int generation)
{ {
if (!Contains(slotIndex, generation)) if (!Contains(slotIndex, generation))
@@ -135,6 +139,7 @@ public class SlotMap<T> : IEnumerable<T>
return true; return true;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetElement(int slotIndex, int generation, [MaybeNullWhen(false)] out T value) public bool TryGetElement(int slotIndex, int generation, [MaybeNullWhen(false)] out T value)
{ {
if (!Contains(slotIndex, generation)) if (!Contains(slotIndex, generation))
@@ -147,6 +152,7 @@ public class SlotMap<T> : IEnumerable<T>
return true; return true;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetElementAt(int slotIndex, int generation) public T GetElementAt(int slotIndex, int generation)
{ {
if (!Contains(slotIndex, generation)) if (!Contains(slotIndex, generation))
@@ -157,6 +163,7 @@ public class SlotMap<T> : IEnumerable<T>
return _data[slotIndex]; return _data[slotIndex];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist) public ref T GetElementReferenceAt(int slotIndex, int generation, out bool exist)
{ {
if (!Contains(slotIndex, generation)) if (!Contains(slotIndex, generation))
@@ -169,6 +176,7 @@ public class SlotMap<T> : IEnumerable<T>
return ref _data[slotIndex]; return ref _data[slotIndex];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool UpdateElement(int slotIndex, int generation, T newValue) public bool UpdateElement(int slotIndex, int generation, T newValue)
{ {
if (!Contains(slotIndex, generation)) if (!Contains(slotIndex, generation))
@@ -180,6 +188,7 @@ public class SlotMap<T> : IEnumerable<T>
return true; return true;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear() public void Clear()
{ {
_count = 0; _count = 0;
@@ -190,9 +199,9 @@ public class SlotMap<T> : IEnumerable<T>
Add(default!, out _); Add(default!, out _);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<T> AsSpan() public Span<T> AsSpan()
{ {
// Skip the first element at index 0 return _data.AsSpan(0, _count);
return _data.AsSpan(1, _count);
} }
} }

View File

@@ -7,7 +7,7 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>Misaki</Authors> <Authors>Misaki</Authors>
<AssemblyVersion>1.0.7</AssemblyVersion> <AssemblyVersion>1.0.8</AssemblyVersion>
<Version>$(AssemblyVersion)</Version> <Version>$(AssemblyVersion)</Version>
<PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl> <PackageProjectUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</PackageProjectUrl>
<RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl> <RepositoryUrl>https://git.personalnas.com/Misaki/Misaki.HighPerformance.git</RepositoryUrl>