Skip to content

Commit

Permalink
Optimize the RecyclableMemoryStream usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamradocz committed Nov 13, 2024
1 parent f331657 commit 0701840
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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());

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

0 comments on commit 0701840

Please sign in to comment.