Upgrade to .net9

This commit is contained in:
dennisarfan 2025-07-31 06:19:32 +07:00
parent eb97cfb57c
commit a1cb6592eb
15 changed files with 395 additions and 54 deletions

View file

@ -0,0 +1,37 @@
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StitchATon2.Infra.Buffers;
internal sealed unsafe class ImmovableMemory<T> : MemoryManager<T> where T : unmanaged
{
private readonly T* _pointer;
private readonly int _length;
private bool _disposed;
public ImmovableMemory(int count)
{
_pointer = (T*)NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
_length = count;
}
protected override void Dispose(bool disposing)
{
if (!_disposed)
{
NativeMemory.Free(_pointer);
_disposed = true;
}
}
public override Span<T> GetSpan()
=> new(_pointer, _length);
public override MemoryHandle Pin(int elementIndex = 0)
=> new(_pointer + elementIndex);
public override void Unpin()
{
}
}

View file

@ -12,4 +12,7 @@ public static class MemoryAllocator
public static ArrayOwner<T> AllocateArray<T>(int count) where T : unmanaged
=> new(ArrayPool<T>.Shared, count);
public static MemoryManager<T> AllocateImmovable<T>(int count) where T : unmanaged
=> new ImmovableMemory<T>(count);
}

View file

@ -3,18 +3,23 @@ using System.Runtime.InteropServices;
namespace StitchATon2.Infra.Buffers;
/// <summary>
/// Provide non-thread safe anti GC contiguous memory.
/// </summary>
/// <typeparam name="T"></typeparam>
internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
{
private readonly void* _pointer;
private readonly T* _pointer;
private readonly int _count;
private bool _disposed;
public ref T this[int index] => ref Unsafe.AsRef<T>((T*)_pointer + index); // *((T*)_pointer + index);
public ref T this[int index] => ref Unsafe.AsRef<T>(_pointer + index);
public Span<T> Span => new(_pointer, _count);
public UnmanagedMemory(int count)
{
_pointer = NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
_pointer = (T*)NativeMemory.Alloc((nuint)count, (nuint)Unsafe.SizeOf<T>());
_count = count;
}
@ -22,7 +27,11 @@ internal sealed unsafe class UnmanagedMemory<T> : IBuffer<T> where T : unmanaged
public void Dispose()
{
NativeMemory.Free(_pointer);
GC.SuppressFinalize(this);
if (!_disposed)
{
NativeMemory.Free(_pointer);
GC.SuppressFinalize(this);
_disposed = true;
}
}
}

View file

@ -0,0 +1,135 @@
using System.Buffers;
using System.Buffers.Binary;
using System.IO.Compression;
using System.IO.Pipelines;
using StitchATon2.Infra.Buffers;
namespace StitchATon2.Infra.Encoders;
public class PngPipeEncoder : IDisposable
{
private const int BufferSize = 8 * 1024;
private const int FlushThreshold = 1024;
private readonly PipeWriter _outputPipe;
private readonly MemoryStream _memoryStream;
private readonly int _width;
private readonly int _height;
private readonly ZLibStream _zlibStream;
private bool _disposed;
private bool _shouldFlush;
public PngPipeEncoder(PipeWriter outputPipe, int width, int height)
{
_outputPipe = outputPipe;
_width = width;
_height = height;
_memoryStream = new MemoryStream(BufferSize * 2);
_zlibStream = new ZLibStream(_memoryStream, CompressionLevel.Optimal, leaveOpen: true);
_memoryStream.SetLength(8);
_memoryStream.Position = 8;
}
~PngPipeEncoder() => Dispose();
public void WriteHeader()
{
Span<byte> headerBytes = [
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG Signature
0x00, 0x00, 0x00, 0x0D, // Length
// IHDR chunk
0x49, 0x48, 0x44, 0x52, // IHDR
0x00, 0x00, 0x00, 0x00, // Reserve to write Width
0x00, 0x00, 0x00, 0x00, // Reserve to write Height
0x08, // Bit depth
0x02, // Color type
0x00, // Compression method
0x00, // Filter method
0x00, // Interlace method
0x00, 0x00, 0x00, 0x00, // Reserve to write CRC-32
];
BinaryPrimitives.WriteInt32BigEndian(headerBytes[16..], _width);
BinaryPrimitives.WriteInt32BigEndian(headerBytes[20..], _height);
var crc = Crc32.Compute(headerBytes.Slice(12, 17));
BinaryPrimitives.WriteUInt32BigEndian(headerBytes[29..], crc);
_outputPipe.Write(headerBytes);
}
public void WriteData(IBuffer<byte> buffer, bool disposeBuffer = true, CancellationToken cancellationToken = default)
{
_zlibStream.Write([0]);
var dataSlice = buffer.Span;
while (dataSlice.Length > FlushThreshold)
{
_zlibStream.Write(dataSlice[..FlushThreshold]);
_zlibStream.Flush();
dataSlice = dataSlice[FlushThreshold..];
if(_memoryStream.Length >= BufferSize)
Flush(cancellationToken);
}
if (dataSlice.Length > 0)
{
_zlibStream.Write(dataSlice);
_zlibStream.Flush();
_shouldFlush = true;
}
if(disposeBuffer) buffer.Dispose();
}
private void Flush(CancellationToken cancellationToken)
{
_zlibStream.Flush();
var dataSize = (int)(_memoryStream.Length - 8);
_memoryStream.Write("\0\0\0\0"u8);
_memoryStream.Position = 4;
_memoryStream.Write("IDAT"u8);
var buffer = _memoryStream.GetBuffer();
BinaryPrimitives.WriteInt32BigEndian(buffer, dataSize);
// write Crc
var crc = Crc32.Compute(buffer.AsSpan(4, dataSize + 4));
BinaryPrimitives.WriteUInt32BigEndian(buffer.AsSpan(dataSize + 8), crc);
_outputPipe.Write(buffer.AsSpan(0, dataSize + 12));
_memoryStream.SetLength(8);
_memoryStream.Position = 8;
_shouldFlush = false;
}
public void WriteEndOfFile(CancellationToken cancellationToken = default)
{
if(_shouldFlush)
Flush(cancellationToken);
Span<byte> endChunk = [
0x00, 0x00, 0x00, 0x00, // Length
0x49, 0x45, 0x4E, 0x44, // IEND
0xAE, 0x42, 0x60, 0x82, // Crc
];
_outputPipe.Write(endChunk);
Dispose();
}
public void Dispose()
{
if (!_disposed)
{
_zlibStream.Dispose();
_memoryStream.Dispose();
_disposed = true;
GC.SuppressFinalize(this);
}
}
}

View file

@ -20,7 +20,7 @@ public class ImageIntegral : IDisposable
private IMemoryOwner<ManualResetEventSlim>? _rowLocks;
private MemoryMappedFile? _memoryMappedFile;
private readonly object _lock = new();
private readonly Lock _lock = new();
private readonly ManualResetEventSlim _queueLock = new(true);
private readonly ManualResetEventSlim _initializationLock = new(false);
@ -249,7 +249,7 @@ public class ImageIntegral : IDisposable
view.DangerousReadSpan(0, buffer, 0, _width);
}
private void ReadRow(int row, IBuffer<Int32Pixel> buffer)
private void ReadRow(int row, ArrayOwner<Int32Pixel> buffer)
{
using var view = AcquireView(row, MemoryMappedFileAccess.Read);
view.DangerousReadSpan(0, buffer.Span, 0, _width);

View file

@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.11" />
</ItemGroup>
</Project>

View file

@ -2,11 +2,7 @@ namespace StitchATon2.Infra.Synchronization;
public static class TaskHelper
{
public static TaskFactory CreateTaskFactory()
{
return new TaskFactory(
TaskCreationOptions.AttachedToParent,
TaskContinuationOptions.ExecuteSynchronously
);
}
public static readonly TaskFactory SynchronizedTaskFactory = new(
TaskCreationOptions.LongRunning,
TaskContinuationOptions.ExecuteSynchronously);
}

View file

@ -7,19 +7,13 @@ namespace StitchATon2.Infra;
public static class Utils
{
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_buffer")]
private static extern ref SafeBuffer GetSafeBuffer(this UnmanagedMemoryAccessor view);
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_offset")]
private static extern ref long GetOffset(this UnmanagedMemoryAccessor view);
private static unsafe uint AlignedSizeOf<T>() where T : unmanaged
{
uint size = (uint)sizeof(T);
return size is 1 or 2 ? size : (uint)((size + 3) & (~3));
}
public static void DangerousReadSpan<T>(this UnmanagedMemoryAccessor view, long position, Span<T> span, int offset, int count)
internal static void DangerousReadSpan<T>(this MemoryMappedViewAccessor view, long position, Span<T> span, int offset, int count)
where T : unmanaged
{
uint sizeOfT = AlignedSizeOf<T>();
@ -38,8 +32,8 @@ public static class Utils
}
}
var byteOffset = (ulong)(view.GetOffset() + position);
view.GetSafeBuffer().ReadSpan(byteOffset, span.Slice(offset, n));
var byteOffset = (ulong)(view.PointerOffset + position);
view.SafeMemoryMappedViewHandle.ReadSpan(byteOffset, span.Slice(offset, n));
}
public static ArrayOwner<T> Clone<T>(this ArrayOwner<T> arrayOwner, int length) where T : unmanaged
@ -48,9 +42,4 @@ public static class Utils
Array.Copy(arrayOwner.Array, 0, newArrayOwner.Array, 0, length);
return newArrayOwner;
}
public static void CopyTo<T>(this ArrayOwner<T> arrayOwner, ArrayOwner<T> target, int length) where T : unmanaged
{
Array.Copy(arrayOwner.Array, 0, target.Array, 0, length);
}
}