Initial upload;

This commit is contained in:
2025-03-25 00:55:48 +09:00
commit aa1e9e6b1d
23 changed files with 1621 additions and 0 deletions

363
.gitignore vendored Normal file
View File

@@ -0,0 +1,363 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>True</PublishAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Misaki.HighPerformance.Unsafe\Misaki.HighPerformance.Unsafe.csproj" />
<ProjectReference Include="..\Misaki.HighPerformance\Misaki.HighPerformance.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,94 @@
using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.Unsafe.Collections;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Test;
[MemoryDiagnoser]
public class ParallelNoiseBenchmark
{
private struct NoiseJob : IJobParallelFor
{
public UnsafeArray<float> buffers;
public int width;
public int height;
public void Execute(int index)
{
var x = index % width;
var y = index / height;
var uv = new Vector2(x, y);
buffers[index] = GradientNoise(uv);
}
}
private const int _WIDTH = 512;
private const int _HEIGHT = 512;
private const int _LENGTH = _WIDTH * _HEIGHT;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Frac(float x)
{
return x - MathF.Truncate(x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Lerp(float a, float b, float t)
{
return a + t * (b - a);
}
private static Vector2 GradientNoiseDirect(Vector2 uv)
{
uv.X %= 289;
uv.Y %= 289;
var x = (34 * uv.X + 1) * uv.X % 289 + uv.Y;
x = (34 * x + 1) * x % 289;
x = Frac(x / 41) * 2 - 1;
return Vector2.Normalize(new Vector2(x - MathF.Floor(x + 0.5f), MathF.Abs(x) - 0.5f));
}
private static float GradientNoise(Vector2 uv)
{
var ip = new Vector2(MathF.Floor(uv.X), MathF.Floor(uv.Y));
var fp = new Vector2(Frac(uv.X), Frac(uv.Y));
var d00 = Vector2.Dot(GradientNoiseDirect(ip), fp);
var d01 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(0, 1)), fp - new Vector2(0, 1));
var d10 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 0)), fp - new Vector2(1, 0));
var d11 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 1)), fp - new Vector2(1, 1));
fp = fp * fp * fp * (fp * (fp * new Vector2(6.0f) - new Vector2(15.0f)) + new Vector2(10.0f));
return Lerp(Lerp(d00, d10, fp.Y), Lerp(d01, d11, fp.Y), fp.X);
}
[Benchmark]
public void JobSystem()
{
using var buffers = new UnsafeArray<float>(_WIDTH * _HEIGHT, AllocationType.UnInitialized);
var job = new NoiseJob()
{
buffers = buffers,
width = _WIDTH,
height = _HEIGHT
};
using var handle = job.Schedule(_LENGTH, 64);
handle.WaitComplete();
}
[Benchmark]
public void ParallelFor()
{
using var buffers = new UnsafeArray<float>(_WIDTH * _HEIGHT, AllocationType.UnInitialized);
Parallel.For(0, _LENGTH, i =>
{
var x = i % _WIDTH;
var y = i / _HEIGHT;
var uv = new Vector2(x, y);
buffers[i] = GradientNoise(uv);
});
}
}

View File

@@ -0,0 +1,4 @@
using BenchmarkDotNet.Running;
using Misaki.HighPerformance.Test;
BenchmarkRunner.Run<ParallelNoiseBenchmark>();

View File

@@ -0,0 +1,2 @@
global using static Misaki.HighPerformance.Unsafe.Helpers.MemoryUtilities;
global using SystemUnsfae = System.Runtime.CompilerServices.Unsafe;

View File

@@ -0,0 +1,75 @@
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Unsafe.Buffer;
/// <summary>
/// A memory management structure that allocates and resets memory blocks with specified alignment.
/// </summary>
public unsafe struct Arena : IDisposable
{
private void* _buffer;
private ulong _size;
private ulong _offset;
private bool _disposed;
public Arena(ulong size)
{
_buffer = Marshal.AllocHGlobal((IntPtr)size).ToPointer();
_size = size;
_offset = 0;
}
/// <summary>
/// Allocates a block of memory of a specified size with a given alignment. Returns a pointer to the allocated
/// memory or null if allocation fails.
/// </summary>
/// <param name="size">Specifies the amount of memory to allocate in bytes.</param>
/// <param name="alignSize">Defines the alignment requirement for the allocated memory.</param>
/// <returns>A pointer to the allocated memory block or null if the allocation cannot be fulfilled.</returns>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void* Allocate(ulong size, uint alignSize)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var offset = (_offset + alignSize - 1) & ~(alignSize - 1);
if (offset + size > _size)
{
return null;
}
_offset = offset + size;
var ptr = (byte*)_buffer + offset;
MemClear(ptr, (uint)size);
return ptr;
}
/// <summary>
/// Resets the arena, optionally clearing the allocated memory.
/// </summary>
/// <param name="clear">If true, the allocated memory will be cleared; otherwise, it will not be cleared.</param>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void Reset(bool clear = false)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (clear)
{
MemClear(_buffer, (uint)_size);
}
_offset = 0;
}
public void Dispose()
{
Marshal.FreeHGlobal((IntPtr)_buffer);
_buffer = null;
_size = 0;
_offset = 0;
_disposed = true;
}
}

View File

@@ -0,0 +1,124 @@
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Unsafe.Buffer;
/// <summary>
/// A dynamic memory management structure that automatically grows by creating linked arenas
/// when more space is needed.
/// </summary>
public unsafe struct DynamicArena : IDisposable
{
private struct ArenaNode
{
public Arena arena;
public ArenaNode* next;
}
private ArenaNode* _root;
private ArenaNode* _current;
private readonly ulong _initialSize;
private bool _disposed;
/// <summary>
/// Initializes a new instance of DynamicArena with the specified initial size.
/// </summary>
/// <param name="initialSize">The initial size in bytes for the first arena block.</param>
public DynamicArena(ulong initialSize)
{
_initialSize = initialSize;
_root = (ArenaNode*)Marshal.AllocHGlobal(sizeof(ArenaNode));
_root->arena = new Arena(initialSize);
_root->next = null;
_current = _root;
_disposed = false;
}
private bool CreateNewNode(ulong size)
{
try
{
var newNode = (ArenaNode*)Marshal.AllocHGlobal(sizeof(ArenaNode));
newNode->arena = new Arena(size);
newNode->next = null;
_current->next = newNode;
_current = newNode;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Allocates a block of memory with specified size and alignment. Creates a new arena if current one is full.
/// </summary>
/// <param name="size">Size of the memory block to allocate in bytes.</param>
/// <param name="alignSize">Alignment requirement for the memory block.</param>
/// <returns>Pointer to the allocated memory block.</returns>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void* Allocate(ulong size, uint alignSize)
{
ObjectDisposedException.ThrowIf(_disposed, this);
void* result = null;
var current = _current;
while (current != null)
{
result = current->arena.Allocate(size, alignSize);
if (result != null)
return result;
if (current->next == null && !CreateNewNode(Math.Max(size, _initialSize)))
return null;
current = current->next;
}
_current = current;
return result;
}
/// <summary>
/// Resets all arenas in the chain, optionally clearing their memory.
/// </summary>
/// <param name="clear">If true, memory will be cleared during reset.</param>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void Reset(bool clear = false)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var current = _root;
while (current != null)
{
current->arena.Reset(clear);
current = current->next;
}
_current = _root;
}
/// <summary>
/// Disposes all arenas and frees associated memory.
/// </summary>
public void Dispose()
{
if (_disposed)
return;
var current = _root;
while (current != null)
{
var next = current->next;
current->arena.Dispose();
Marshal.FreeHGlobal((IntPtr)current);
current = next;
}
_root = null;
_current = null;
_disposed = true;
}
}

View File

@@ -0,0 +1,7 @@
namespace Misaki.HighPerformance.Unsafe.Collections;
public enum AllocationType
{
UnInitialized,
Clear
}

View File

@@ -0,0 +1,22 @@
namespace Misaki.HighPerformance.Unsafe.Collections.Contracts;
public unsafe interface IUnsafeCollection<T> : IDisposable where T : unmanaged
{
public T* Buffer
{
get;
}
public int Size
{
get;
}
public ref T this[int index]
{
get;
}
public void Clear();
public void ReAlloc(int newSize);
}

View File

@@ -0,0 +1,113 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Unsafe.Collections;
public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where T : unmanaged
{
public struct Enumerator : IEnumerator<T>
{
private UnsafeArray<T> _collection;
private int _index;
private T _value;
public Enumerator(ref UnsafeArray<T> collection)
{
_collection = collection;
_index = -1;
_value = default;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
_index++;
if (_index < _collection.Size)
{
_value = UnsafeUtilities.ReadArrayElement<T>(_collection.Buffer, _index);
return true;
}
_value = default;
return false;
}
public void Reset()
{
_index = -1;
}
// Let NativeArray indexer check for out of range.
public T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _value;
}
}
object IEnumerator.Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return Current;
}
}
public void Dispose()
{
}
}
private T* _buffer;
private int _size;
public readonly T* Buffer => _buffer;
public readonly int Size => _size;
public readonly ref T this[int index] => ref UnsafeUtilities.AsRef<T>(_buffer + index);
public IEnumerator<T> GetEnumerator() => new Enumerator(ref this);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public UnsafeArray(int size, AllocationType allocationType)
{
_size = size;
_buffer = (T*)Marshal.AllocHGlobal(size * sizeof(T)).ToPointer();
if (allocationType == AllocationType.Clear)
{
Clear();
}
}
public void ReAlloc(int newSize)
{
if (newSize == _size)
{
return;
}
_buffer = (T*)Marshal.ReAllocHGlobal((IntPtr)_buffer, newSize * sizeof(T)).ToPointer();
_size = newSize;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Clear()
{
MemClear(_buffer, (uint)(_size * sizeof(T)));
}
public void Dispose()
{
Marshal.FreeHGlobal((IntPtr)_buffer);
_buffer = null;
_size = 0;
}
}

View File

@@ -0,0 +1,290 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Unsafe.Collections;
public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T> where T : unmanaged
{
public struct Enumerator : IEnumerator<T>
{
private UnsafeList<T> _collection;
private int _index;
private T _value;
public Enumerator(ref UnsafeList<T> collection)
{
_collection = collection;
_index = -1;
_value = default;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
_index++;
if (_index < _collection.Size)
{
_value = UnsafeUtilities.ReadArrayElement<T>(_collection.Buffer, _index);
return true;
}
_value = default;
return false;
}
public void Reset()
{
_index = -1;
}
// Let NativeArray indexer check for out of range.
public readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _value;
}
}
readonly object IEnumerator.Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return Current;
}
}
public readonly void Dispose()
{
}
}
/// <summary>
/// A parallel writer for an UnsafeList.
/// </summary>
/// <remarks>
/// Use <see cref="AsParallelWriter"/> to create a parallel writer for a list.
/// </remarks>
public unsafe struct ParallelWriter
{
/// <summary>
/// The UnsafeList to write to.
/// </summary>
public UnsafeList<T>* listData;
internal unsafe ParallelWriter(UnsafeList<T>* list)
{
listData = list;
}
/// <summary>
/// Adds a value to a collection without resizing it, ensuring capacity is checked before insertion.
/// </summary>
/// <param name="value">The value to be added to the collection.</param>
public void AddNoResize(T value)
{
var idx = Interlocked.Increment(ref listData->_size) - 1;
listData->CheckNoResizeCapacity(idx, 1);
UnsafeUtilities.WriteArrayElement(listData->_buffer, idx, value);
}
/// <summary>
/// Adds a specified number of elements from a pointer to a buffer without resizing the underlying storage.
/// </summary>
/// <param name="ptr">Points to the source data to be copied into the buffer.</param>
/// <param name="count">Indicates the number of elements to be added from the source data.</param>
public void AddRangeNoResize(T* ptr, int count)
{
var idx = Interlocked.Add(ref listData->_size, count) - count;
listData->CheckNoResizeCapacity(idx, count);
MemCpy(listData->_buffer + idx, ptr, (uint)(count * sizeof(T)));
}
}
private T* _buffer;
private int _size;
private int _capacity;
public readonly T* Buffer => _buffer;
public readonly int Size => _size;
public readonly int Capacity => _capacity;
public readonly ref T this[int index] => ref UnsafeUtilities.ReadArrayElementRef<T>(_buffer, index);
public IEnumerator<T> GetEnumerator() => new Enumerator(ref this);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public ParallelWriter AsParallelWriter() => new((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this));
public UnsafeList(int capacity, AllocationType allocationType)
{
_buffer = (T*)Marshal.AllocHGlobal(capacity * sizeof(T));
_size = 0;
_capacity = capacity;
if (allocationType == AllocationType.Clear)
{
Clear();
}
}
private readonly void CheckNoResizeCapacity(int count)
{
CheckNoResizeCapacity(count, count);
}
private readonly void CheckNoResizeCapacity(int index, int count)
{
if (index + count > _capacity)
{
throw new Exception($"AddNoResize assumes that list capacity is sufficient (Capacity {Capacity}, Size {Size}), requested count {count}!");
}
}
private readonly void CheckIndexCount(int index, int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException($"Value for count {count} must be positive.");
}
if (index < 0)
{
throw new ArgumentOutOfRangeException($"Value for index {index} must be positive.");
}
if (index > Size)
{
throw new ArgumentOutOfRangeException($"Value for index {index} is out of bounds.");
}
if (index + count > Size)
{
throw new ArgumentOutOfRangeException($"Value for count {count} is out of bounds.");
}
}
public void Add(T value)
{
if (_size >= _capacity)
{
ReAlloc(_capacity + (int)(_capacity * 0.5f));
}
UnsafeUtilities.WriteArrayElement(_buffer, _size, value);
_size++;
}
public void AddNoResize(T value)
{
CheckNoResizeCapacity(1);
UnsafeUtilities.WriteArrayElement(_buffer, _size, value);
_size++;
}
public void AddRange(Span<T> values, int count)
{
var newSize = _size + count;
if (newSize > _capacity)
{
ReAlloc(_capacity + count);
}
fixed (T* ptr = values)
{
MemCpy(_buffer + _size, ptr, (uint)(count * sizeof(T)));
}
_size += count;
}
public void AddRangeNoResize(ReadOnlySpan<T> values)
{
CheckNoResizeCapacity(values.Length);
fixed (T* ptr = values)
{
MemCpy(_buffer + _size, ptr, (uint)(values.Length * sizeof(T)));
}
_size += values.Length;
}
public void AddRangeNoResize(T* ptr, int count)
{
CheckNoResizeCapacity(count);
MemCpy(_buffer + _size, ptr, (uint)(count * sizeof(T)));
_size += count;
}
public void RemoveRange(int start, int length)
{
CheckIndexCount(start, length);
if (length <= 0)
{
return;
}
var copyFrom = Math.Min(start + length, _size);
MemCpy(_buffer + start, _buffer + copyFrom, (uint)((_size - copyFrom) * sizeof(T)));
_size -= length;
}
public void RemoveAt(int index)
{
RemoveRange(index, 1);
}
public void RemoveRangeSwapBack(int start, int length)
{
CheckIndexCount(start, length);
if (length <= 0)
{
return;
}
var copyFrom = Math.Min(_size - length, start + length);
MemCpy(_buffer + start, _buffer + copyFrom, (uint)((_size - copyFrom) * sizeof(T)));
_size -= length;
}
public void RemoveAtSwapBack(int index)
{
RemoveRangeSwapBack(index, 1);
}
public void ReAlloc(int newSize)
{
if (newSize == _size)
{
return;
}
_buffer = (T*)Marshal.ReAllocHGlobal((IntPtr)_buffer, newSize * sizeof(T)).ToPointer();
_size = newSize;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Clear()
{
MemClear(_buffer, (uint)(_size * sizeof(T)));
}
public void Dispose()
{
Marshal.FreeHGlobal((IntPtr)_buffer);
_buffer = null;
_size = 0;
}
}

View File

@@ -0,0 +1,41 @@
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Helpers;
public unsafe static class MemoryUtilities
{
/// <summary>
/// Clears a block of memory by setting it to zero. It initializes a specified number of bytes at a given memory
/// address.
/// </summary>
/// <param name="ptr">Specifies the memory address where the clearing operation will begin.</param>
/// <param name="size">Indicates the number of bytes to be cleared in the memory block.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MemClear(void* ptr, uint size)
{
SystemUnsfae.InitBlock(ptr, 0, size);
}
/// <summary>
/// Sets a block of memory to a specified byte value for a given size.
/// </summary>
/// <param name="ptr">The memory address where the byte value will be set.</param>
/// <param name="value">The byte value to which the memory block will be initialized.</param>
/// <param name="size">The number of bytes to set to the specified value.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MemSet(void* ptr, byte value, uint size)
{
SystemUnsfae.InitBlock(ptr, value, size);
}
/// <summary>
/// Copies a block of memory from a source location to a destination location.
/// </summary>
/// <param name="destination">Specifies the memory address where the copied data will be stored.</param>
/// <param name="source">Indicates the memory address from which data will be copied.</param>
/// <param name="size">Defines the number of bytes to be copied from the source to the destination.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MemCpy(void* destination, void* source, uint size)
{
SystemUnsfae.CopyBlock(destination, source, size);
}
}

View File

@@ -0,0 +1,90 @@
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
namespace Misaki.HighPerformance.Unsafe.Helpers;
public unsafe static class UnsafeCollectionExtensions
{
/// <summary>
/// Copies elements from a source UnsafeCollection to a destination Span, ensuring both have the same size.
/// </summary>
/// <typeparam name="T">Specifies the type of elements being copied, which must be unmanaged.</typeparam>
/// <param name="source">Represents the source collection from which elements are copied.</param>
/// <param name="destination">Represents the target span where elements are copied to.</param>
/// <exception cref="ArgumentException">Thrown when the sizes of the source collection and destination span do not match.</exception>
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination) where T : unmanaged
{
if (source.Size > destination.Length)
{
throw new ArgumentException("Source collection is larger than the destination span.");
}
fixed (T* ptr = destination)
{
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
}
}
/// <summary>
/// Copies elements from a source span to a destination unsafe collection, ensuring both have the same size.
/// </summary>
/// <typeparam name="T">Specifies the type of elements being copied, which must be unmanaged.</typeparam>
/// <param name="destination">Represents the unsafe collection that will receive the copied elements.</param>
/// <param name="source">Represents the span containing the elements to be copied to the unsafe collection.</param>
/// <exception cref="ArgumentException">Thrown when the source span and destination collection have different sizes.</exception>
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source) where T : unmanaged
{
if (destination.Size > source.Length)
{
throw new ArgumentException("Destination collection is larger than the source span.");
}
fixed (T* ptr = source)
{
SystemUnsfae.CopyBlock(destination.Buffer, ptr, (uint)(source.Length * sizeof(T)));
}
}
/// <summary>
/// Converts an UnsafeCollection of unmanaged types into a standard collection.
/// </summary>
/// <typeparam name="T">Represents a type that is unmanaged, allowing for direct memory manipulation.</typeparam>
/// <param name="source">The UnsafeCollection instance that contains the data to be converted.</param>
/// <returns>A new collection containing the elements from the UnsafeCollection.</returns>
public static T[] ToArray<T>(this IUnsafeCollection<T> source) where T : unmanaged
{
var array = new T[source.Size];
fixed (T* ptr = array)
{
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
}
return array;
}
/// <summary>
/// Converts an unmanaged collection into a list by copying its elements into a new list.
/// </summary>
/// <typeparam name="T">Represents a type that is unmanaged, allowing for direct memory manipulation.</typeparam>
/// <param name="source">The collection from which elements are copied to create the new list.</param>
/// <returns>A list containing the elements from the specified unmanaged collection.</returns>
public static List<T> ToList<T>(this IUnsafeCollection<T> source) where T : unmanaged
{
var list = new List<T>(source.Size);
fixed (T* ptr = list.ToArray())
{
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
}
return list;
}
/// <summary>
/// Converts an UnsafeCollection into a Span for efficient memory access.
/// </summary>
/// <typeparam name="T">Represents a type that can be stored in unmanaged memory.</typeparam>
/// <param name="source">The UnsafeCollection instance to be converted into a Span.</param>
/// <returns>A Span that provides a view over the elements of the UnsafeCollection.</returns>
public static Span<T> AsSpan<T>(this IUnsafeCollection<T> source) where T : unmanaged
{
return new(source.Buffer, source.Size);
}
}

View File

@@ -0,0 +1,82 @@
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Helpers;
public static unsafe class UnsafeUtilities
{
/// <summary>
/// Converts a pointer to a reference of a specified type.
/// </summary>
/// <typeparam name="T">Specifies the type of the reference to be created from the pointer.</typeparam>
/// <param name="ptr">Represents the memory address to be converted into a reference.</param>
/// <returns>Returns a reference of the specified type pointing to the given memory address.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T AsRef<T>(void* ptr)
{
return ref SystemUnsfae.AsRef<T>(ptr);
}
/// <summary>
/// Returns the address of a specified variable in memory.
/// </summary>
/// <typeparam name="T">Represents the type of the variable whose address is being retrieved.</typeparam>
/// <param name="value">The variable whose memory address is to be obtained.</param>
/// <returns>A pointer to the memory address of the specified variable.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void* AddressOf<T>(ref T value)
{
return SystemUnsfae.AsPointer(ref value);
}
/// <summary>
/// Reads an element from an unmanaged array at a specified index using a pointer.
/// </summary>
/// <typeparam name="T">Specifies the type of elements in the unmanaged array.</typeparam>
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
/// <returns>Returns a pointer to the element located at the specified index.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T* ReadArrayElementUnsafe<T>(void* ptr, int index) where T : unmanaged
{
return (T*)((byte*)ptr + (index * sizeof(T)));
}
/// <summary>
/// Reads an element from an unmanaged array using a pointer and index, returning a reference to the element.
/// </summary>
/// <typeparam name="T">Specifies the type of the elements in the unmanaged array.</typeparam>
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
/// <param name="index">Indicates the position of the element to be accessed in the array.</param>
/// <returns>A reference to the specified element in the unmanaged array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T ReadArrayElementRef<T>(void* ptr, int index) where T : unmanaged
{
return ref AsRef<T>(ReadArrayElementUnsafe<T>(ptr, index));
}
/// <summary>
/// Reads an element from an array at a specified index using a pointer to the array.
/// </summary>
/// <typeparam name="T">Specifies the type of the elements in the array, which must be unmanaged.</typeparam>
/// <param name="ptr">Points to the start of the array from which an element will be read.</param>
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
/// <returns>The element located at the specified index in the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T ReadArrayElement<T>(void* ptr, int index) where T : unmanaged
{
return *ReadArrayElementUnsafe<T>(ptr, index);
}
/// <summary>
/// Writes a value to a specified index of an unmanaged array using a pointer.
/// </summary>
/// <typeparam name="T">Specifies the type of the value being written to the array, which must be an unmanaged type.</typeparam>
/// <param name="ptr">Points to the beginning of the unmanaged array where the value will be written.</param>
/// <param name="index">Indicates the position in the array where the value should be stored.</param>
/// <param name="value">Represents the value to be written to the specified index of the array.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteArrayElement<T>(void* ptr, int index, T value) where T : unmanaged
{
*ReadArrayElementUnsafe<T>(ptr, index) = value;
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,37 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.35821.62
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Misaki.HighPerformance", "Misaki.HighPerformance\Misaki.HighPerformance.csproj", "{275B2E80-9B2A-4567-A157-F147A6B28A0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Misaki.HighPerformance.Unsafe", "Misaki.HighPerformance.Unsafe\Misaki.HighPerformance.Unsafe.csproj", "{0DD1B42E-BA40-4F22-9565-5A3977139B66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Misaki.HighPerformance.Test", "Misaki.HighPerformance.Test\Misaki.HighPerformance.Test.csproj", "{90EFF5B8-22CD-4B6A-83AB-48E0E97610EA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{275B2E80-9B2A-4567-A157-F147A6B28A0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{275B2E80-9B2A-4567-A157-F147A6B28A0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{275B2E80-9B2A-4567-A157-F147A6B28A0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{275B2E80-9B2A-4567-A157-F147A6B28A0F}.Release|Any CPU.Build.0 = Release|Any CPU
{0DD1B42E-BA40-4F22-9565-5A3977139B66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DD1B42E-BA40-4F22-9565-5A3977139B66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DD1B42E-BA40-4F22-9565-5A3977139B66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DD1B42E-BA40-4F22-9565-5A3977139B66}.Release|Any CPU.Build.0 = Release|Any CPU
{90EFF5B8-22CD-4B6A-83AB-48E0E97610EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90EFF5B8-22CD-4B6A-83AB-48E0E97610EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90EFF5B8-22CD-4B6A-83AB-48E0E97610EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90EFF5B8-22CD-4B6A-83AB-48E0E97610EA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51A97B1D-DB4D-45BC-8D2E-347710C1AA37}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,110 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
namespace Misaki.HighPerformance.Buffer
{
public class ObjectPool<T> : IDisposable where T : class
{
private readonly Func<T> _factory;
private readonly ConcurrentQueue<T> _objects = new();
private readonly bool _autoCleanup;
private readonly int _autoCleanupInterval;
private bool _disposed;
public uint InitialSize
{
get;
}
public uint MaxSize
{
get;
private set;
}
public ObjectPool(Func<T> factory, uint initialSize = uint.MinValue, uint maxSize = uint.MaxValue, bool autoCleanup = false, int autoCleanupInterval = 1000 * 60 * 5)
{
_factory = factory;
_autoCleanup = autoCleanup;
_autoCleanupInterval = autoCleanupInterval;
InitialSize = initialSize;
MaxSize = maxSize;
if (initialSize != uint.MinValue)
{
for (var i = 0; i < initialSize; i++)
{
_objects.Enqueue(_factory());
}
}
SetupAutoCleanup();
}
private void PoolCleanup()
{
foreach (var obj in _objects)
{
if (obj is IDisposable disposable)
{
disposable.Dispose();
}
}
_objects.Clear();
GC.Collect();
}
private void SetupAutoCleanup()
{
if (!_autoCleanup)
{
return;
}
Task.Run(async () =>
{
while (true)
{
await Task.Delay(_autoCleanupInterval);
PoolCleanup();
}
});
}
public bool TryRent([MaybeNullWhen(false)] out T obj)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (!_objects.IsEmpty)
{
return _objects.TryDequeue(out obj);
}
obj = null;
return false;
}
public void Return(T obj)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (_objects.Count < MaxSize)
{
_objects.Enqueue(obj);
}
}
public void Dispose()
{
PoolCleanup();
_disposed = true:
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Misaki.HighPerformance.Jobs;
public interface IJob
{
public void Execute();
}

View File

@@ -0,0 +1,6 @@
namespace Misaki.HighPerformance.Jobs;
public interface IJobParallelFor
{
public void Execute(int index);
}

View File

@@ -0,0 +1,49 @@
namespace Misaki.HighPerformance.Jobs;
public static class JobExtensions
{
public static JobHandle Schedule<T>(this T job, bool preferLocal = false) where T : struct, IJob
{
var handle = new JobHandle(1);
var worker = new JobWorker<T>(job, handle);
ThreadPool.UnsafeQueueUserWorkItem(worker, preferLocal);
return handle;
}
public static JobHandle Schedule<T>(this T job, ReadOnlySpan<JobHandle> dependencies, bool preferLocal = false) where T : struct, IJob
{
foreach (var dependency in dependencies)
{
dependency.WaitComplete();
}
return job.Schedule(preferLocal);
}
public static JobHandle Schedule<T>(this T job, int length, int batchCount, bool preferLocal = false) where T : struct, IJobParallelFor
{
var batchSize = (length + batchCount - 1) / batchCount;
var handle = new JobHandle(batchCount);
for (var i = 0; i < batchCount; i++)
{
var start = i * batchSize;
var end = Math.Min(start + batchSize, length);
var worker = new ParallelJobWorker<T>(job, handle, start, end);
ThreadPool.UnsafeQueueUserWorkItem(worker, preferLocal);
}
return handle;
}
public static JobHandle Schedule<T>(this T job, int length, int batchCount, ReadOnlySpan<JobHandle> dependencies, bool preferLocal = false) where T : struct, IJobParallelFor
{
foreach (var dependency in dependencies)
{
dependency.WaitComplete();
}
return job.Schedule(length, batchCount, preferLocal);
}
}

View File

@@ -0,0 +1,28 @@
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Jobs;
public readonly struct JobHandle(int jobCount) : IDisposable
{
private readonly CountdownEvent _jobCompletionEvent = new(jobCount);
public readonly bool IsCompleted => _jobCompletionEvent.IsSet;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void CompleteOne()
{
_jobCompletionEvent.Signal();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WaitComplete()
{
_jobCompletionEvent.Wait();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
_jobCompletionEvent.Dispose();
}
}

View File

@@ -0,0 +1,23 @@
namespace Misaki.HighPerformance.Jobs;
internal readonly struct JobWorker<T>(T job, JobHandle handle) : IThreadPoolWorkItem where T : struct, IJob
{
public void Execute()
{
job.Execute();
handle.CompleteOne();
}
}
internal readonly struct ParallelJobWorker<T>(T job, JobHandle handle, int start, int end) : IThreadPoolWorkItem where T : struct, IJobParallelFor
{
public void Execute()
{
for (var i = start; i < end; i++)
{
job.Execute(i);
}
handle.CompleteOne();
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
</PropertyGroup>
</Project>