From 981ba286e4793de95bf65e6588313411344c4d53 Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 4 Jul 2024 16:04:03 -0400 Subject: refactor: Refactor extensions with perf updates --- lib/Utils/src/IO/VnMemoryStream.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'lib/Utils/src/IO') diff --git a/lib/Utils/src/IO/VnMemoryStream.cs b/lib/Utils/src/IO/VnMemoryStream.cs index 4d51a08..3f14061 100644 --- a/lib/Utils/src/IO/VnMemoryStream.cs +++ b/lib/Utils/src/IO/VnMemoryStream.cs @@ -84,7 +84,7 @@ namespace VNLib.Utils.IO ArgumentNullException.ThrowIfNull(handle); return handle.CanRealloc || readOnly - ? new VnMemoryStream(handle, length, readOnly, ownsHandle) + ? new VnMemoryStream(handle, existingManager: null, length, readOnly, ownsHandle) : throw new ArgumentException("The supplied memory handle must be resizable on a writable stream", nameof(handle)); } @@ -179,7 +179,14 @@ namespace VNLib.Utils.IO /// The length property of the stream /// Is the stream readonly (should mostly be true!) /// Does the new stream own the memory -> - private VnMemoryStream(IResizeableMemoryHandle buffer, nint length, bool readOnly, bool ownsHandle) + /// A reference to an existing memory manager class + private VnMemoryStream( + IResizeableMemoryHandle buffer, + MemoryManager? existingManager, + nint length, + bool readOnly, + bool ownsHandle + ) { Debug.Assert(length >= 0, "Length must be positive"); Debug.Assert(buffer.CanRealloc || readOnly, "The supplied buffer is not resizable on a writable stream"); @@ -188,6 +195,7 @@ namespace VNLib.Utils.IO _buffer = buffer; //Consume the handle _length = length; //Store length of the buffer _isReadonly = readOnly; + _memoryWrapper = existingManager; } /// @@ -205,7 +213,7 @@ namespace VNLib.Utils.IO //Create a new readonly copy (stream does not own the handle) return !_isReadonly ? throw new NotSupportedException("This stream is not readonly. Cannot create shallow copy on a mutable stream") - : new VnMemoryStream(_buffer, _length, true, false); + : new VnMemoryStream(_buffer, _memoryWrapper, _length, readOnly: true, ownsHandle: false); } /// -- cgit