Runtime: Add support for decompresing data in ReadOnlySpan<byte> with Deflate algorithm

Created on 9 Jan 2018  路  4Comments  路  Source: dotnet/runtime

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>

api-suggestion area-System.IO.Compression

Most helpful comment

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.

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matty-hall picture matty-hall  路  3Comments

jkotas picture jkotas  路  3Comments

noahfalk picture noahfalk  路  3Comments

aggieben picture aggieben  路  3Comments

nalywa picture nalywa  路  3Comments