FluentStorage icon indicating copy to clipboard operation
FluentStorage copied to clipboard

[Question] Any plan on adding `Span<byte>` / `Memory<byte>` support

Open candoumbe opened this issue 2 years ago • 1 comments

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 IBlobStorage implementations 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.WriteAsync extension method that would take a Memory<byte> instead of byte[]

Is that something that make sense to you ?

candoumbe avatar Apr 26 '23 09:04 candoumbe

Any new methods or API are welcome. You are free to work on it and submit a PR, I accept almost all PRs.

robinrodricks avatar Apr 27 '23 12:04 robinrodricks