From 07ddf6738d32127926d07b1366e56d2a2308b53b Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 16 Jun 2024 01:12:07 -0400 Subject: perf: Absolutely yuge perf boosts --- lib/Utils/src/Extensions/MemoryExtensions.cs | 102 ++------ lib/Utils/src/IO/VnMemoryStream.cs | 4 +- lib/Utils/src/Memory/MemoryUtilAlloc.cs | 378 ++++++++++++++++++--------- lib/Utils/src/Memory/UnmanagedHeapBase.cs | 16 +- 4 files changed, 282 insertions(+), 218 deletions(-) (limited to 'lib/Utils/src') diff --git a/lib/Utils/src/Extensions/MemoryExtensions.cs b/lib/Utils/src/Extensions/MemoryExtensions.cs index c433527..99d4cf1 100644 --- a/lib/Utils/src/Extensions/MemoryExtensions.cs +++ b/lib/Utils/src/Extensions/MemoryExtensions.cs @@ -48,19 +48,8 @@ namespace VNLib.Utils.Extensions /// The minimum size array to allocate /// Should elements from 0 to size be set to default(T) /// A new encapsulating the rented array - public static UnsafeMemoryHandle UnsafeAlloc(this ArrayPool pool, int size, bool zero = false) where T : unmanaged - { - ArgumentNullException.ThrowIfNull(pool); - - T[] array = pool.Rent(size); - - if (zero) - { - MemoryUtil.InitializeBlock(array, (uint)size); - } - - return new(pool, array, size); - } + public static UnsafeMemoryHandle UnsafeAlloc(this ArrayPool pool, int size, bool zero = false) where T : unmanaged + => MemoryUtil.UnsafeAlloc(pool, size, zero); /// /// Rents a new array and stores it as a resource within an to return the @@ -72,19 +61,7 @@ namespace VNLib.Utils.Extensions /// Should elements from 0 to size be set to default(T) /// A new encapsulating the rented array public static IMemoryHandle SafeAlloc(this ArrayPool pool, int size, bool zero = false) where T : struct - { - ArgumentNullException.ThrowIfNull(pool); - - T[] array = pool.Rent(size); - - if (zero) - { - MemoryUtil.InitializeBlock(array, (uint)size); - } - - //Use the array pool buffer wrapper to return the array to the pool when the handle is disposed - return new ArrayPoolBuffer(pool, array, size); - } + => MemoryUtil.SafeAlloc(pool, size, zero); /// /// Retreives a buffer that is at least the reqested length, and clears the array from 0-size. @@ -110,13 +87,6 @@ namespace VNLib.Utils.Extensions return arr; } - /// - /// Copies the characters within the memory handle to a - /// - /// The string representation of the buffer - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string ToString(this T charBuffer) where T : IMemoryHandle => charBuffer.Span.ToString(); - /// /// Wraps the instance in System.Buffers.MemoryManager /// wrapper to provide buffers from umanaged handles. @@ -131,7 +101,8 @@ namespace VNLib.Utils.Extensions /// NOTE: This wrapper now manages the lifetime of the current handle /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static MemoryManager ToMemoryManager(this IMemoryHandle handle, bool ownsHandle) => new SysBufferMemoryManager(handle, ownsHandle); + public static MemoryManager ToMemoryManager(this IMemoryHandle handle, bool ownsHandle) + => new SysBufferMemoryManager(handle, ownsHandle); /// /// Allows direct allocation of a fixed size from a instance @@ -168,7 +139,8 @@ namespace VNLib.Utils.Extensions /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetIntLength(this IMemoryHandle handle) => Convert.ToInt32(handle.Length); + public static int GetIntLength(this IMemoryHandle handle) + => Convert.ToInt32(handle.Length); /// /// Gets the integer length (number of elements) of the @@ -181,7 +153,8 @@ namespace VNLib.Utils.Extensions /// //Method only exists for consistancy since unsafe handles are always 32bit [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetIntLength(this in UnsafeMemoryHandle handle) where T : unmanaged => handle.IntLength; + public static int GetIntLength(this in UnsafeMemoryHandle handle) where T : unmanaged + => handle.IntLength; /// /// Gets an offset pointer from the base postion to the number of bytes specified. Performs bounds checks @@ -440,16 +413,8 @@ namespace VNLib.Utils.Extensions /// /// /// - public static unsafe MemoryHandle Alloc(this IUnmangedHeap heap, nuint elements, bool zero = false) where T : unmanaged - { - ArgumentNullException.ThrowIfNull(heap); - //Minimum of one element - elements = Math.Max(elements, 1); - //If zero flag is set then specify zeroing memory - IntPtr block = heap.Alloc(elements, (nuint)sizeof(T), zero); - //Return handle wrapper - return new MemoryHandle(heap, block, elements, zero); - } + public static unsafe MemoryHandle Alloc(this IUnmangedHeap heap, nuint elements, bool zero = false) where T : unmanaged + => MemoryUtil.SafeAlloc(heap, elements, zero); /// /// Allocates a block of unmanaged memory of the number of elements to store of an unmanged type @@ -464,10 +429,7 @@ namespace VNLib.Utils.Extensions /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static MemoryHandle Alloc(this IUnmangedHeap heap, nint elements, bool zero = false) where T : unmanaged - { - ArgumentOutOfRangeException.ThrowIfNegative(elements); - return Alloc(heap, (nuint)elements, zero); - } + => MemoryUtil.SafeAlloc(heap, elements, zero); /// /// Allocates a buffer from the current heap and initialzies it by copying the initial data buffer @@ -481,10 +443,8 @@ namespace VNLib.Utils.Extensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static MemoryHandle AllocAndCopy(this IUnmangedHeap heap, ReadOnlySpan initialData) where T : unmanaged { - //Aloc block - MemoryHandle handle = heap.Alloc(initialData.Length); - - //Copy initial data + MemoryHandle handle = Alloc(heap, initialData.Length); + MemoryUtil.Copy(initialData, 0, handle, 0, initialData.Length); return handle; @@ -502,10 +462,8 @@ namespace VNLib.Utils.Extensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static MemoryHandle AllocAndCopy(this IUnmangedHeap heap, ReadOnlyMemory initialData) where T : unmanaged { - //Aloc block - MemoryHandle handle = heap.Alloc(initialData.Length); - - //Copy initial data + MemoryHandle handle = Alloc(heap, initialData.Length); + MemoryUtil.Copy(initialData, 0, handle, 0, initialData.Length); return handle; @@ -542,25 +500,8 @@ namespace VNLib.Utils.Extensions /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static UnsafeMemoryHandle UnsafeAlloc(this IUnmangedHeap heap, int elements, bool zero = false) where T : unmanaged - { - ArgumentNullException.ThrowIfNull(heap); - - if (elements < 1) - { - //Return an empty handle - return new UnsafeMemoryHandle(); - } - - //Get element size - nuint elementSize = (nuint)Unsafe.SizeOf(); - - //If zero flag is set then specify zeroing memory (safe case because of the above check) - IntPtr block = heap.Alloc((nuint)elements, elementSize, zero); - - //handle wrapper - return new (heap, block, elements); - } + public unsafe static UnsafeMemoryHandle UnsafeAlloc(this IUnmangedHeap heap, int elements, bool zero = false) where T : unmanaged + => MemoryUtil.UnsafeAlloc(heap, elements, zero); #region VnBufferWriter @@ -774,13 +715,6 @@ namespace VNLib.Utils.Extensions return charCount; } - /// - /// Converts the buffer data to a - /// - /// A instance that owns the underlying string memory - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static PrivateString ToPrivate(this ref ForwardOnlyWriter buffer) => new(buffer.ToString(), true); - /// /// Gets a over the modified section of the internal buffer /// diff --git a/lib/Utils/src/IO/VnMemoryStream.cs b/lib/Utils/src/IO/VnMemoryStream.cs index 2e5ec40..23df869 100644 --- a/lib/Utils/src/IO/VnMemoryStream.cs +++ b/lib/Utils/src/IO/VnMemoryStream.cs @@ -41,6 +41,8 @@ namespace VNLib.Utils.IO /// public sealed class VnMemoryStream : Stream, ICloneable { + public const int DefaultBufferSize = 4096; + private nint _position; private nint _length; private bool _isReadonly; @@ -108,7 +110,7 @@ namespace VNLib.Utils.IO /// to allocate memory from /// /// - public VnMemoryStream(IUnmangedHeap heap) : this(heap, 0, false) { } + public VnMemoryStream(IUnmangedHeap heap) : this(heap, DefaultBufferSize, false) { } /// /// Creates a new memory stream and pre-allocates the internal diff --git a/lib/Utils/src/Memory/MemoryUtilAlloc.cs b/lib/Utils/src/Memory/MemoryUtilAlloc.cs index e2e7434..6e4f9b0 100644 --- a/lib/Utils/src/Memory/MemoryUtilAlloc.cs +++ b/lib/Utils/src/Memory/MemoryUtilAlloc.cs @@ -27,14 +27,83 @@ using System.Buffers; using System.Diagnostics; using System.Runtime.CompilerServices; -using VNLib.Utils.Extensions; - namespace VNLib.Utils.Memory { public static unsafe partial class MemoryUtil { #region alloc + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static bool UseUnmanagedHeap(IUnmangedHeap heap, nuint elements) + { + /* + * We may allocate from the share heap only if the heap is not using locks + * or if the element size could cause performance issues because its too large + * to use a managed array. + * + * We want to avoid allocations, that may end up in the LOH if we can + */ + + return (heap.CreationFlags & HeapCreation.UseSynchronization) == 0 + || ByteCount((uint)elements) > MAX_UNSAFE_POOL_SIZE; + } + + /// + /// Allocates a block of unmanaged memory of the number of elements to store of an unmanged type + /// + /// Unmanaged data type to create a block of + /// + /// The size of the block (number of elements) + /// A flag that zeros the allocated block before returned + /// The unmanaged + /// + /// + /// + public static UnsafeMemoryHandle UnsafeAlloc(IUnmangedHeap heap, int elements, bool zero = false) where T : unmanaged + { + ArgumentNullException.ThrowIfNull(heap); + ArgumentOutOfRangeException.ThrowIfNegative(elements); + + if (elements == 0) + { + //Return an empty handle + return default; + } + + //If zero flag is set then specify zeroing memory (safe case because of the above check) + IntPtr block = heap.Alloc((nuint)elements, (nuint)sizeof(T), zero); + + return new(heap, block, elements); + } + + /// + /// Rents a new array and stores it as a resource within an to return the + /// array when work is completed + /// + /// + /// + /// The minimum size array to allocate + /// Should elements from 0 to size be set to default(T) + /// A new encapsulating the rented array + public static UnsafeMemoryHandle UnsafeAlloc(ArrayPool pool, int size, bool zero = false) where T : unmanaged + { + ArgumentNullException.ThrowIfNull(pool); + + if (size <= 0) + { + return default; + } + + T[] array = pool.Rent(size); + + if (zero) + { + InitializeBlock(array, (uint)size); + } + + return new(pool, array, size); + } + /// /// Allocates a block of unmanaged, or pooled manaaged memory depending on /// compilation flags and runtime unamanged allocators. @@ -43,40 +112,20 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// public static UnsafeMemoryHandle UnsafeAlloc(int elements, bool zero = false) where T : unmanaged { - if (elements < 0) - { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); - } + ArgumentOutOfRangeException.ThrowIfNegative(elements); if (elements == 0) { return default; } - /* - * We may allocate from the share heap only if the heap is not using locks - * or if the element size could cause performance issues because its too large - * to use a managed array. - * - * We want to avoid allocations, that may end up in the LOH if we can - */ - - if ((Shared.CreationFlags & HeapCreation.UseSynchronization) == 0 || ByteCount((uint)elements) > MAX_UNSAFE_POOL_SIZE) - { - // Alloc from heap - IntPtr block = Shared.Alloc((uint)elements, (uint)sizeof(T), zero); - //Init new handle - return new(Shared, block, elements); - } - else - { - //Rent the array from the pool - return ArrayPool.Shared.UnsafeAlloc(elements, zero); - } + return UseUnmanagedHeap(Shared, (uint)elements) + ? UnsafeAlloc(Shared, elements, zero) + : UnsafeAlloc(ArrayPool.Shared, elements, zero); } /// @@ -88,18 +137,81 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static UnsafeMemoryHandle UnsafeAllocNearestPage(int elements, bool zero = false) where T : unmanaged { - if (elements < 0) + ArgumentOutOfRangeException.ThrowIfNegative(elements); + return UnsafeAlloc(elements: (int)NearestPage(elements), zero); + } + + /// + /// Allocates a block of unmanaged memory of the number of elements to store of an unmanged type + /// + /// Unmanaged data type to create a block of + /// + /// The size of the block (number of elements) + /// A flag that zeros the allocated block before returned + /// The unmanaged + /// + /// + /// + public static MemoryHandle SafeAlloc(IUnmangedHeap heap, nuint elements, bool zero = false) where T : unmanaged + { + ArgumentNullException.ThrowIfNull(heap); + + //Return empty handle if no elements were specified + if (elements == 0) { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); + return new MemoryHandle(); } + + IntPtr block = heap.Alloc(elements, (nuint)sizeof(T), zero); - //Round to nearest page (in bytes) - nint np = NearestPage(elements); - return UnsafeAlloc((int)np, zero); + return new MemoryHandle(heap, block, elements, zero); + } + + /// + /// Allocates a block of unmanaged memory of the number of elements to store of an unmanged type + /// + /// Unmanaged data type to create a block of + /// + /// The size of the block (number of elements) + /// A flag that zeros the allocated block before returned + /// The unmanaged + /// + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static MemoryHandle SafeAlloc(IUnmangedHeap heap, nint elements, bool zero = false) where T : unmanaged + { + ArgumentOutOfRangeException.ThrowIfNegative(elements); + return SafeAlloc(heap, (nuint)elements, zero); + } + + /// + /// Rents a new array and stores it as a resource within an to return the + /// array when work is completed + /// + /// + /// + /// The minimum size array to allocate + /// Should elements from 0 to size be set to default(T) + /// A new encapsulating the rented array + public static ArrayPoolBuffer SafeAlloc(ArrayPool pool, int size, bool zero = false) where T : struct + { + ArgumentNullException.ThrowIfNull(pool); + + T[] array = pool.Rent(size); + + if (zero) + { + InitializeBlock(array, (uint)size); + } + + //Use the array pool buffer wrapper to return the array to the pool when the handle is disposed + return new ArrayPoolBuffer(pool, array, size); } /// @@ -110,33 +222,58 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// - public static IMemoryHandle SafeAlloc(int elements, bool zero = false) where T : unmanaged + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IMemoryHandle SafeAlloc(nuint elements, bool zero = false) where T : unmanaged { - if (elements < 0) - { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); - } - - /* - * We may allocate from the share heap only if the heap is not using locks - * or if the element size could cause performance issues because its too large - * to use a managed array. - * - * We want to avoid allocations, that may end up in the LOH if we can - */ + ArgumentOutOfRangeException.ThrowIfNegative(elements); - if ((Shared.CreationFlags & HeapCreation.UseSynchronization) == 0 || ByteCount((uint)elements) > MAX_UNSAFE_POOL_SIZE) + if (UseUnmanagedHeap(Shared, elements)) { - return Shared.Alloc(elements, zero); + return SafeAlloc(Shared, elements, zero); } else { - return new ArrayPoolBuffer(ArrayPool.Shared, elements, zero); + //Should never happen because max pool size guards against this + Debug.Assert(elements <= int.MaxValue); + + return SafeAlloc(ArrayPool.Shared, (int)elements, zero); } } + /// + /// Allocates a block of unmanaged, or pooled manaaged memory depending on + /// compilation flags and runtime unamanged allocators. + /// + /// The unamanged type to allocate + /// The number of elements of the type within the block + /// Flag to zero elements during allocation before the method returns + /// A handle to the block of memory + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IMemoryHandle SafeAlloc(int elements, bool zero = false) where T : unmanaged + { + ArgumentOutOfRangeException.ThrowIfNegative(elements); + return SafeAlloc((nuint)elements, zero); + } + + /// + /// Allocates a block of unmanaged, or pooled manaaged memory depending on + /// compilation flags and runtime unamanged allocators, rounded up to the + /// neareset memory page. + /// + /// The unamanged type to allocate + /// The number of elements of the type within the block + /// Flag to zero elements during allocation before the method returns + /// A handle to the block of memory + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static IMemoryHandle SafeAllocNearestPage(nuint elements, bool zero = false) where T : unmanaged + => SafeAlloc(elements: NearestPage(elements), zero); + /// /// Allocates a block of unmanaged, or pooled manaaged memory depending on /// compilation flags and runtime unamanged allocators, rounded up to the @@ -146,20 +283,51 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IMemoryHandle SafeAllocNearestPage(int elements, bool zero = false) where T : unmanaged { - if (elements < 0) - { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); - } + ArgumentOutOfRangeException.ThrowIfNegative(elements); + return SafeAllocNearestPage((nuint)elements, zero); + } - //Round to nearest page (in bytes) - nint np = NearestPage(elements); - return SafeAlloc((int)np, zero); + /// + /// Allocates a block of unmanaged, or pooled manaaged memory depending on + /// compilation flags and runtime unamanged allocators, rounded up to the + /// neareset memory page. + /// + /// The unamanged type to allocate + /// The number of elements of the type within the block + /// Flag to zero elements during allocation before the method returns + /// The heap to allocate the block of memory from + /// A handle to the block of memory + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static MemoryHandle SafeAllocNearestPage(IUnmangedHeap heap, int elements, bool zero = false) where T : unmanaged + { + ArgumentOutOfRangeException.ThrowIfNegative(elements); + + return SafeAllocNearestPage(heap, (nuint)elements, zero); } + /// + /// Allocates a block of unmanaged, or pooled manaaged memory depending on + /// compilation flags and runtime unamanged allocators, rounded up to the + /// neareset memory page. + /// + /// The unamanged type to allocate + /// The number of elements of the type within the block + /// Flag to zero elements during allocation before the method returns + /// The heap to allocate the block of memory from + /// A handle to the block of memory + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static MemoryHandle SafeAllocNearestPage(IUnmangedHeap heap, nuint elements, bool zero = false) where T : unmanaged + => SafeAlloc(heap, elements: NearestPage(elements), zero); + /// /// Allocates a structure of the specified type on the specified /// unmanged heap and optionally zero's it's memory @@ -191,13 +359,8 @@ namespace VNLib.Utils.Memory /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref T StructAllocRef(IUnmangedHeap heap, bool zero) where T : unmanaged - { - //Alloc structure - T* ptr = StructAlloc(heap, zero); - //Get a reference and assign it - return ref Unsafe.AsRef(ptr); - } + public static ref T StructAllocRef(IUnmangedHeap heap, bool zero) where T : unmanaged + => ref Unsafe.AsRef(StructAlloc(heap, zero)); /// /// Frees a structure allocated with @@ -207,7 +370,8 @@ namespace VNLib.Utils.Memory /// A pointer to the unmanaged structure to free /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void StructFree(IUnmangedHeap heap, T* structPtr) where T : unmanaged => StructFree(heap, (void*)structPtr); + public static void StructFree(IUnmangedHeap heap, T* structPtr) where T : unmanaged + => StructFree(heap, (void*)structPtr); /// /// Frees a structure allocated with @@ -218,7 +382,8 @@ namespace VNLib.Utils.Memory /// A reference to the unmanaged structure to free /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void StructFreeRef(IUnmangedHeap heap, ref T structRef) where T : unmanaged => StructFree(heap, Unsafe.AsPointer(ref structRef)); + public static void StructFreeRef(IUnmangedHeap heap, ref T structRef) where T : unmanaged + => StructFree(heap, Unsafe.AsPointer(ref structRef)); /// /// Frees a structure allocated with @@ -233,7 +398,7 @@ namespace VNLib.Utils.Memory //Get intpointer IntPtr ptr = (IntPtr)structPtr; - //Free + bool isFree = heap.Free(ref ptr); Debug.Assert(isFree, $"Structure free failed for heap {heap.GetHashCode()}, struct address {ptr:x}"); } @@ -249,39 +414,20 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// public static UnsafeMemoryHandle UnsafeAlloc(int elements, bool zero = false) { - if (elements < 0) - { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); - } + ArgumentOutOfRangeException.ThrowIfNegative(elements); if(elements == 0) { return default; } - /* - * We may allocate from the share heap only if the heap is not using locks - * or if the element size could cause performance issues because its too large - * to use a managed array. - * - * We want to avoid allocations, that may end up in the LOH if we can - */ - - if ((Shared.CreationFlags & HeapCreation.UseSynchronization) == 0 || elements > MAX_UNSAFE_POOL_SIZE) - { - // Alloc from heap - IntPtr block = Shared.Alloc((uint)elements, 1, zero); - //Init new handle - return new(Shared, block, elements); - } - else - { - return ArrayPool.Shared.UnsafeAlloc(elements, zero); - } + return UseUnmanagedHeap(Shared, (uint)elements) + ? UnsafeAlloc(Shared, elements, zero) + : UnsafeAlloc(ArrayPool.Shared, elements, zero); } /// @@ -292,19 +438,15 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static UnsafeMemoryHandle UnsafeAllocNearestPage(int elements, bool zero = false) { - if (elements < 0) - { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); - } + ArgumentOutOfRangeException.ThrowIfNegative(elements); //Round to nearest page (in bytes) - nint np = NearestPage(elements); - return UnsafeAlloc((int)np, zero); + return UnsafeAlloc(elements: (int)NearestPage(elements), zero); } /// @@ -314,31 +456,15 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// public static IMemoryHandle SafeAlloc(int elements, bool zero = false) { - if (elements < 0) - { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); - } - - /* - * We may allocate from the share heap only if the heap is not using locks - * or if the element size could cause performance issues because its too large - * to use a managed array. - * - * We want to avoid allocations, that may end up in the LOH if we can - */ + ArgumentOutOfRangeException.ThrowIfNegative(elements); - if ((Shared.CreationFlags & HeapCreation.UseSynchronization) == 0 || elements > MAX_UNSAFE_POOL_SIZE) - { - return Shared.Alloc(elements, zero); - } - else - { - return new ArrayPoolBuffer(ArrayPool.Shared, elements, zero); - } + return UseUnmanagedHeap(Shared, (uint)elements) + ? SafeAlloc(Shared, (nuint)elements, zero) + : SafeAlloc(ArrayPool.Shared, elements, zero); } /// @@ -349,18 +475,12 @@ namespace VNLib.Utils.Memory /// The number of elements of the type within the block /// Flag to zero elements during allocation before the method returns /// A handle to the block of memory - /// + /// /// public static IMemoryHandle SafeAllocNearestPage(int elements, bool zero = false) { - if (elements < 0) - { - throw new ArgumentException("Number of elements must be a positive integer", nameof(elements)); - } - - //Round to nearest page (in bytes) - nint np = NearestPage(elements); - return SafeAlloc((int)np, zero); + ArgumentOutOfRangeException.ThrowIfNegative(elements); + return SafeAlloc(elements: (int)NearestPage(elements), zero); } #endregion diff --git a/lib/Utils/src/Memory/UnmanagedHeapBase.cs b/lib/Utils/src/Memory/UnmanagedHeapBase.cs index 7f42761..a9730b7 100644 --- a/lib/Utils/src/Memory/UnmanagedHeapBase.cs +++ b/lib/Utils/src/Memory/UnmanagedHeapBase.cs @@ -23,6 +23,7 @@ */ using System; +using System.Diagnostics; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; @@ -111,25 +112,32 @@ namespace VNLib.Utils.Memory bool result; //If disposed, set the block handle to zero and exit to avoid raising exceptions during finalization - if (IsClosed || IsInvalid) + if (IsClosed) { block = LPVOID.Zero; return true; } + /* + * Checking for invalid is not really necesasry because + * the only way the handle can be invalidated is + * if some derrived class mutates the handle value + * and doesn't close the handle + */ + Debug.Assert(IsInvalid == false); + if ((flags & HeapCreation.UseSynchronization) > 0) { //wait for lock lock (HeapLock) - { - //Free block + { result = FreeBlock(block); //Release lock before releasing handle } } else { - //No lock + //No lock needed result = FreeBlock(block); } -- cgit