Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
candoumbe opened this issue Apr 26, 2023 · 1 comment
Open

Comments

@candoumbe
Copy link
Contributor

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 or Memory 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 ?

@robinrodricks
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants