FluentStorage
FluentStorage copied to clipboard
[Question] Any plan on adding `Span<byte>` / `Memory<byte>` support
Hi !
Is there any plan on adding support of Span<byte> / Memory<byte> where byte[] is currently required ?
(given that the introduction of these types help enhance memory usage and are spreading across the SDK)
Here a example where I think having overloads that accepts Span<T> or Memory<T> depending on the context could help
Memory<byte> buffer = GetBuffer();
Guid id = Guid.NewGuid();
string fileName = section.FileName;
string fullPath = $"upload/{id}";
Stream stream = section.Section.Body;
int bytesRead = 0;
// Streaming of the file
do
{
bytesRead = await stream.ReadAsync(buffer.Memory, ct);
await _storage.WriteAsync(fullPath, buffer.Memory.ToArray(), true, cancellationToken: ct);
}
while (bytesRead > 0);
which could be rewritten
Memory<byte> buffer = GetBuffer();
Guid id = Guid.NewGuid();
string fileName = section.FileName;
string fullPath = $"upload/{id}";
Stream stream = section.Section.Body;
int bytesRead = 0;
// Streaming of the file
do
{
bytesRead = await stream.ReadAsync(buffer.Memory, ct);
await _storage.WriteAsync(fullPath, buffer.Memory, true, cancellationToken: ct);
}
while (bytesRead > 0);
From what i've seen in the codebase, it would require to either :
- go through all existing
IBlobStorageimplementations to add support for this but with a default interface implementation, the change could be as minimal as adding default method
Task WriteAsync(string fullPath, Memory<byte> data, bool append = false, CancellationToken cancellationToken = default) => WriteAsync(fullPath, new MemoryStream(data.ToArray()), append, cancellationToken);
This has the advantage of allowing implementors to provide a more memory efficient approach without breaking them (net6.0 upwards)
- add a new
IBlobStorage.WriteAsyncextension method that would take aMemory<byte>instead ofbyte[]
Is that something that make sense to you ?
Any new methods or API are welcome. You are free to work on it and submit a PR, I accept almost all PRs.