Skip to content

[Proposal] Expose streaming API in same manner as Brotli did #63

Closed

Description

@deniszykov

Provide an API for streaming compression/decompression in the same form as is done for Brotli in .NET standard library. This will allow ZstdSharp to be used in scenarios where Stream cannot be used (like System.IO.Pipelines).

BrotliDecoder:

        /// <summary>Decompresses data that was compressed using the Brotli algorithm.</summary>
        /// <param name="source">A buffer containing the compressed data.</param>
        /// <param name="destination">When this method returns, a byte span containing the decompressed data.</param>
        /// <param name="bytesConsumed">The total number of bytes that were read from <paramref name="source" />.</param>
        /// <param name="bytesWritten">The total number of bytes that were written in the <paramref name="destination" />.</param>
        /// <returns>One of the enumeration values that indicates the status of the decompression operation.</returns>
        /// <remarks>The return value can be as follows:
        /// - <see cref="System.Buffers.OperationStatus.Done" />: <paramref name="source" /> was successfully and completely decompressed into <paramref name="destination" />.
        /// - <see cref="System.Buffers.OperationStatus.DestinationTooSmall" />: There is not enough space in <paramref name="destination" /> to decompress <paramref name="source" />.
        /// - <see cref="System.Buffers.OperationStatus.NeedMoreData" />: The decompression action is partially done at least one more byte is required to complete the decompression task. This method should be called again with more input to decompress.
        /// - <see cref="System.Buffers.OperationStatus.InvalidData" />: The data in <paramref name="source" /> is invalid and could not be decompressed.</remarks>
        public OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten);

BrotliEncoder:

        /// <summary>Compresses an empty read-only span of bytes into its destination, which ensures that output is produced for all the processed input. An actual flush is performed when the source is depleted and there is enough space in the destination for the remaining data.</summary>
        /// <param name="destination">When this method returns, a span of bytes where the compressed data will be stored.</param>
        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramref name="destination" />.</param>
        /// <returns>One of the enumeration values that describes the status with which the operation finished.</returns>
        public OperationStatus Flush(Span<byte> destination, out int bytesWritten);

        /// <summary>Compresses a read-only byte span into a destination span.</summary>
        /// <param name="source">A read-only span of bytes containing the source data to compress.</param>
        /// <param name="destination">When this method returns, a byte span where the compressed is stored.</param>
        /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramref name="source" />.</param>
        /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramref name="destination" />.</param>
        /// <param name="isFinalBlock"><see langword="true" /> to finalize the internal stream, which prevents adding more input data when this method returns; <see langword="false" /> to allow the encoder to postpone the production of output until it has processed enough input.</param>
        /// <returns>One of the enumeration values that describes the status with which the span-based operation finished.</returns>
        public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out int bytesWritten, bool isFinalBlock);

Activity

  1. oleg-st commented on Dec 25, 2025

    @oleg-st
    Owner

    I think it's a good idea.

  2. oleg-st commented on Dec 25, 2025

    @oleg-st
    Owner

    Draft implementation
    #64

  3. oleg-st commented on Jan 2, 2026

    @oleg-st
    Owner

    Added in master

  4. oleg-st commented on Jan 3, 2026

    @oleg-st
    Owner

    Released in 0.8.7

  5. deniszykov commented on Jan 4, 2026

    @deniszykov
    Author
    # AMD Ryzen 9 5900X 12-Core Processor 
    # payload: 2mb of random data
    # Brotli comp. level 6
    # Gzip/ZLib comp. level 6
    # Zstd comp. level 3
    
    # Gzip -> Managed Gzip from SharpZipLib
    
    | Method           | Mean        | Error     | StdDev    | Median      | Gen0    | Allocated |
    |----------------- |------------:|----------:|----------:|------------:|--------:|----------:|
    | ZStdEncoder      |    996.6 us |  19.77 us |  45.43 us |    972.7 us |       - |      65 B |
    | BrotliEncoder    |  9,370.7 us | 180.74 us | 200.89 us |  9,369.3 us |       - |      47 B |
    | GZipEncoder      | 46,820.3 us | 903.98 us | 967.25 us | 46,531.9 us | 90.9091 | 1678379 B |
    | ZlibEncoder      | 40,846.6 us | 189.32 us | 147.81 us | 40,795.5 us |       - |     105 B |
    
    
    | Method           | Mean       | Error    | StdDev   | Median     | Allocated |
    |----------------- |-----------:|---------:|---------:|-----------:|----------:|
    | ZStdDecoder      |   434.5 us | 10.20 us | 30.08 us |   423.0 us |      56 B |
    | BrotliDecoder    |   434.8 us | 10.15 us | 29.92 us |   422.0 us |      32 B |
    | ZlibDecoder      |   644.0 us | 10.12 us |  9.47 us |   643.6 us |      49 B |
    | GZipDecoder       | 3,604.4 us |  3.81 us |  3.18 us | 3,604.0 us |   33011 B |
    
     ZstdEncoder: 1888.00 MiB/s
     BrotliEncoder 224.75 MiB/s
     ZlibEncoder: 52.69 MiB/s
     GZipEncoder: 45.32 MiB/s
    
     ZstdDecoder: 4822.32 MiB/s
     BrotliDecoder 4491.45 MiB/s
     ZlibDecoder 5481.22 MiB/s
     GZipDecoder 586.43 MiB/s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions