Read/Write bytes directly from data array #41

Open
Rob-Hague wants to merge 1 commits from Rob-Hague/MatFileHandler:span into master
First-time contributor

This uses span methods (namely MemoryMarshal.AsBytes) to avoid the creation of an additional byte array for the data when reading/writing. It therefore cuts memory in half (more or less) when reading, and given a premade IMatFile, eliminates (more or less) the memory allocations that result from writing the data arrays.

This change adds a .NET 8 target to get native support for Span<T> and relevant APIs. Since older targets do not natively support Span<T>, it adds a nuget reference to Microsoft.Bcl.Memory which allows them to be used. Even though relevant span-taking APIs are not available (such as Stream.Read(Span<byte>)), we can still achieve memory improvements on older targets using extension methods that rent an array from a pool rather than create one.

The nuget reference is only available for .NET Framework 4.6.2, since 4.6.1 is EOL (since 2022), so this change also involves bumping that target. Hopefully that is OK. There are other options if net461 should stay

This uses span methods (namely [MemoryMarshal.AsBytes]([url](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.memorymarshal.asbytes))) to avoid the creation of an additional byte array for the data when reading/writing. It therefore cuts memory in half (more or less) when reading, and given a premade IMatFile, eliminates (more or less) the memory allocations that result from writing the data arrays. This change adds a .NET 8 target to get native support for `Span<T>` and relevant APIs. Since older targets do not natively support `Span<T>`, it adds a nuget reference to [Microsoft.Bcl.Memory]([url](https://www.nuget.org/packages/Microsoft.Bcl.Memory)) which allows them to be used. Even though relevant span-taking APIs are not available (such as `Stream.Read(Span<byte>)`), we can still achieve memory improvements on older targets using extension methods that rent an array from a pool rather than create one. The nuget reference is only available for .NET Framework 4.6.2, since 4.6.1 is EOL (since 2022), so this change also involves bumping that target. Hopefully that is OK. There are other options if net461 should stay
Rob-Hague added 1 commit 2025-07-25 11:57:56 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u span:Rob-Hague-span
git checkout Rob-Hague-span
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mahalex/MatFileHandler#41
No description provided.