System.IO.Compression contains the DeflateStream class, that can decompress data from a stream. In case we have compressed data already loaded in a ReadOnlySpan<byte>, using DeflateStream might be little inefficient - we have to create a MemoryStream and this operation allocates a new array.
c#
function Decompress(ReadOnlySpan<byte> block) {
using(var stream = new MemoryStream(block.ToArray())) {
using(var deflateStream = new DeflateStream(stream, CompressionMode.Decompress)) {
Data = ... read from deflateStream
}
}
}
It would be great, if the Deflate algorithm is exposed in the way that supports processing data from ReadOnlySpan<byte>
cc @stephentoub @KrzysztofCwalina
Yes, we have such APIs in Brotli (see BrotliEncoder & BrotliDecoder in https://github.com/dotnet/corefx/issues/25785). We should add similar APIs to Deflate/GZip.
cc: @ianhays, @joshfree
In case we have compressed data already loaded in a ReadOnlySpan
, using DeflateStream might be little inefficient - we have to create a MemoryStream and this operation allocates a new array.
That isn't the only option; you can create an UnmanagedMemoryStream from it (pinning the span with fixed) and then there's no extra array needed.
And if you have the data as a Memory instead of a Span, you can either do that or create a MemoryStream that wraps the original array if TryGetArray returns one.
Duplicate of dotnet/runtime#16923
Most helpful comment
That isn't the only option; you can create an UnmanagedMemoryStream from it (pinning the span with fixed) and then there's no extra array needed.
And if you have the data as a Memory instead of a Span, you can either do that or create a MemoryStream that wraps the original array if TryGetArray returns one.