From 9afed1427472da1ea13079f98dbe27339e55ee7d Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 31 Mar 2024 16:43:15 -0400 Subject: perf: Deprecate unsafememoryhandle span extensions --- lib/Utils/src/Extensions/MemoryExtensions.cs | 4 +++- lib/Utils/src/Memory/IMemoryHandle.cs | 4 ++-- lib/Utils/src/Memory/MemoryHandle.cs | 15 ++++++++++++--- lib/Utils/src/Memory/UnsafeMemoryHandle.cs | 26 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/Utils/src/Extensions/MemoryExtensions.cs b/lib/Utils/src/Extensions/MemoryExtensions.cs index e81580b..65d90a0 100644 --- a/lib/Utils/src/Extensions/MemoryExtensions.cs +++ b/lib/Utils/src/Extensions/MemoryExtensions.cs @@ -270,7 +270,7 @@ namespace VNLib.Utils.Extensions /// /// Gets a reference to the element at the specified offset from the base - /// address of the + /// address of the /// /// /// The element offset from the base address to add to the returned reference @@ -833,6 +833,7 @@ namespace VNLib.Utils.Extensions /// The sub-sequence of the current handle /// [MethodImpl(MethodImplOptions.AggressiveInlining)] + [Obsolete("Functions are included directly on the type now")] public static Span AsSpan(this in UnsafeMemoryHandle handle, int start) where T: unmanaged => handle.Span[start..]; /// @@ -845,6 +846,7 @@ namespace VNLib.Utils.Extensions /// The sub-sequence of the current handle /// [MethodImpl(MethodImplOptions.AggressiveInlining)] + [Obsolete("Functions are included directly on the type now")] public static Span AsSpan(this in UnsafeMemoryHandle handle, int start, int count) where T : unmanaged => handle.Span.Slice(start, count); /// diff --git a/lib/Utils/src/Memory/IMemoryHandle.cs b/lib/Utils/src/Memory/IMemoryHandle.cs index f4e1a36..b538f87 100644 --- a/lib/Utils/src/Memory/IMemoryHandle.cs +++ b/lib/Utils/src/Memory/IMemoryHandle.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -46,7 +46,7 @@ namespace VNLib.Utils.Memory /// /// Gets a reference to the first element in the block /// - /// The reference + /// The reference to the first element in the block ref T GetReference(); } diff --git a/lib/Utils/src/Memory/MemoryHandle.cs b/lib/Utils/src/Memory/MemoryHandle.cs index c5cc295..16fc555 100644 --- a/lib/Utils/src/Memory/MemoryHandle.cs +++ b/lib/Utils/src/Memory/MemoryHandle.cs @@ -188,8 +188,7 @@ namespace VNLib.Utils.Memory this.ThrowIfClosed(); //Get ptr and offset it - T* bs = ((T*)handle) + elements; - return bs; + return ((T*)handle) + elements; } /// @@ -199,6 +198,16 @@ namespace VNLib.Utils.Memory return ref MemoryUtil.GetRef(handle); } + /// + /// Gets a reference to the element at the specified offset from the base + /// address of the + /// + /// The element offset from the base address to add to the returned reference + /// The reference to the item at the desired offset + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public unsafe ref T GetOffsetRef(nuint offset) => ref Unsafe.AsRef(GetOffset(offset)); + /// /// /// @@ -248,6 +257,6 @@ namespace VNLib.Utils.Memory public override bool Equals(object? obj) => obj is MemoryHandle oHandle && Equals(oHandle); /// - public override int GetHashCode() => base.GetHashCode(); + public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), handle.GetHashCode(), _length); } } \ No newline at end of file diff --git a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs index 1ed995f..d93739d 100644 --- a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs +++ b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs @@ -202,6 +202,32 @@ namespace VNLib.Utils.Memory } } + /// + /// Returns a that represents the memory block pointed to by this handle + /// + /// The memory block that is held by the internl handle + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Span AsSpan() => Span; + + /// + /// Returns a that represents the memory block pointed to by this handle + /// + /// A starting element offset to return the span at + /// The desired memory block at the desired element offset + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Span AsSpan(int start) => Span[start..]; + + /// + /// Returns a that represents the memory block pointed to by this handle + /// + /// The starting element offset + /// The number of elements included in the returned span + /// The desired memory block at the desired element offset and length + /// " + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Span AsSpan(int start, int length) => Span.Slice(start, length); + /// public readonly override int GetHashCode() { -- cgit