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

Optimize the RecyclableMemoryStream usage. #17006

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IActionResult> ServiceEndpoint([ModelBinder(BinderType = typeo
result.Save(w);
}

var content = Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
var content = Encoding.UTF8.GetString(stream.GetReadOnlySequence());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about these, apparently there are a few in EncodingExtensions. We should check if there aren't other places where we could use these.


return Content(content, "text/xml");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ static CommonGeneratorMethods()
using var uncompressedStream = MemoryStreamFactory.GetStream((int)compressedStream.Length);
gZip.CopyTo(uncompressedStream);

return Convert.ToBase64String(uncompressedStream.GetBuffer(), 0, (int)uncompressedStream.Length);
var readOnlySequence = uncompressedStream.GetReadOnlySequence();
var base64String = readOnlySequence.IsSingleSegment ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. It would only make sense if ReadOnlySequence could be used instead.

Convert.ToBase64String(readOnlySequence.FirstSpan) :
Convert.ToBase64String(uncompressedStream.GetBuffer(), 0, (int)uncompressedStream.Length);

return base64String;
}),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ public object Evaluate(IScriptingScope scope, string script)
memoryStream.WriteTo(fileStream);
memoryStream.Seek(0, SeekOrigin.Begin);

return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
var readOnlySequence = memoryStream.GetReadOnlySequence();
var base64String = readOnlySequence.IsSingleSegment ?
Convert.ToBase64String(readOnlySequence.FirstSpan) :
Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);

return base64String;
}
else
{
Expand Down