[API Proposal]: Custom dictionary support for Brotli
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- backend-api-design, performance
Research direction
Start with draft implementation PR #118783, related issue #112656, and Brotli's shared_dictionary.h API linked in the proposal. Clarify the factory naming, attachment semantics, and SafeHandle lifetime/refcounting design; done means an agreed API and implementation that preserves dictionary lifetime for encoder, decoder, and stream use.
Written by the indexing model from the issue text.
Description
Background and motivation
Related to #112656
Draft implementation PR #118783
Brotli is a flexible compression algorithm which allows specifying custom dictionary to achieve better compression performance on particular types of data. One of the possible uses for this is https://datatracker.ietf.org/doc/draft-ietf-httpbis-compression-dictionary/.
Technical background
When attaching a dictionary to encoder/decoder, brotli library does not copy the provided dictionary data, so our implementation will have to ensure that
- the dictionary data is kept alive and (preferably) unmodified
- the dictionary data is pinned and is not moved by GC
For the above reason, this proposal creates a new class BrotliDictionary which user can construct, and then reuse across multiple encoder/decoder/streams.
When constructing a dictionary, the native API accepts an enum specifying the dictionary type
In theory, more than one dictionary formats may be supported in the future, so creating BrotliDictionary instances via factory method is preferred.
The way how a dictionary is attached to an encoder or decoder is assymetrical:
Encoder side accepts a separately constructed BrotliEncoderPreparedDictionary object.
BROTLI_ENC_API BrotliEncoderPreparedDictionary*
BrotliEncoderPrepareDictionary(BrotliSharedDictionaryType type,
size_t data_size, const uint8_t data[BROTLI_ARRAY_PARAM(data_size)],
int quality,
brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
BROTLI_ENC_API BROTLI_BOOL BrotliEncoderAttachPreparedDictionary(
BrotliEncoderState* state,
const BrotliEncoderPreparedDictionary* dictionary);
Decoder side accepts a byte array.
BROTLI_DEC_API BROTLI_BOOL BrotliDecoderAttachDictionary(
BrotliDecoderState* state, BrotliSharedDictionaryType type,
size_t data_size, const uint8_t data[BROTLI_ARRAY_PARAM(data_size)]);
Both sides require the data array to be kept alive.
For simplicity, this proposal seeks to add the same signature for either side.
API Proposal
+ public sealed class BrotliDictionary : System.IDisposable
+ {
+ internal BrotliDictionary() { }
// Alternative name: CreateFromRawBytes or just CreateFromBytes
// ZstandardDictionary uses Create
+ public static System.IO.Compression.BrotliDictionary Create(System.ReadOnlySpan<byte> buffer, int quality) { throw null; }
// Uses default quality
+ public static System.IO.Compression.BrotliDictionary Create(System.ReadOnlySpan<byte> buffer) { throw null; }
+ public void Dispose() { }
+ }
public struct BrotliDecoder : System.IDisposable
{
+ public void AttachDictionary(System.IO.Compression.BrotliDictionary dictionary) { }
}
public struct BrotliEncoder : System.IDisposable
{
+ public void AttachDictionary(System.IO.Compression.BrotliDictionary dictionary) { }
}
public class BrotliStream
{
+ public void AttachDictionary(System.IO.Compression.BrotliDictionary dictionary) { }
}
API Usage
BrotliDictionary dictionary = BrotliDictionary.CreateFromBuffer(RawDictionaryData);
BrotliStream stream = new BrotliStream(....);
stream.AttachDictionary(dictionary);
// use stream as usual
Alternative Designs
ctor parameter instead of AttachDictionary method
Accepting dictionaries in Encoder/Decoder ctor would also be possible, but then we need to introduce a few more constructor overloads. Also, keep in mind that it is possible to attach multiple dictionaries in Brotli (the effect seems to be similar to concatenating the two dictionaries), so additional overloads accepting a dictionary collection might be needed.
+ public sealed class BrotliDictionary : System.IDisposable
+ {
// proposal left unchanged
+ }
public sealed class BrotliCompressionOptions
{
// If set, supersedes Quality
+ public BrotliDictionary? Dictionary { get; set; }
}
public struct BrotliDecoder : System.IDisposable
{
+ public BrotliDecoder(System.IO.Compression.BrotliDictionary dictionary) { }
}
public struct BrotliEncoder : System.IDisposable
{
+ public BrotliEncoder(System.IO.Compression.BrotliDictionary dictionary) { }
}
public class BrotliStream
{
+ public BrotliStream(System.IO.Stream stream, System.IO.Compression.CompressionMode mode, BrotliDictionary dictionary, bool leaveOpen = false) { }
}
An approach requiring fewer allocations is possible for decoders, as the native API only requires a reference to an array (that must be kept alive/pinned for the lifetime of the Decoder). But the design is more complicated and error prone, and if we assume that dictionaries are going to be long-lived and reused, does not offer enough savings.
Risks
Lifetime management of the BrotliDictionary (more specificly, the native memory held by the safe handle within) can be tricky. since brotli does not do any internal refcounting to ensure the dictionary will be alive long enough, we need to perform the refcounting in Managed code on the relevant SafeHandle types.
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·