Fix DeflateEncoder.GetMaxCompressedLength conservative bound - #132274
Open
BrzVlad wants to merge 1 commit into
Open
Fix DeflateEncoder.GetMaxCompressedLength conservative bound#132274BrzVlad wants to merge 1 commit into
BrzVlad wants to merge 1 commit into
Conversation
Previous code was using the implementation from compressBound(). This api returns a conservative bound when using the compress() APIs, which our .NET implementation doesn't use (it uses deflate directly). The compress API does a deflate with default configuration, while our API allows custom configuration (via the quality level, window size and a few others). This means that the compressBound is not valid when using the deflate API with custom parameters. zlib exposes the deflateBound method which can be used to obtain a conservative size. The problem with this API is that it requires to know the parameters of the compression, which we don't have available in our .NET API, given GetMaxCompressedLenght is static, applying universally to all compressions. While it is not yet decided if we will add a new API to support obtaining the bound from the library, this commit adds a workaround, by computing the maximum bound for every compression configuration exposed by our .NET APIs The formula is obtained from our zlib-ng sources in deflate.c. Mobile uses zlib however. Validated that this change works correctly on maccatalyst, ios simulator and that the current logic stands with the current open source code for zlib (which applies to android). The actual bound used by Apple and Android can always change, so we do have a small risk that our hardcoded limit will diverge. I believe the only safe solution for this would be to make use of the actual deflateBound method, which requires adjustement to our API.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression |
Contributor
There was a problem hiding this comment.
Pull request overview
Updates DeflateEncoder.GetMaxCompressedLength to return a more conservative, configuration-agnostic upper bound for deflate-based encoders (addressing mobile failures where GetMaxCompressedLength() under-allocated and Compress returned DestinationTooSmall), and re-enables the previously skipped window-log round-trip test.
Changes:
- Replaces the prior
compressBound()-based implementation with a managed bound derived from zlib-ng’sdeflateBound()conservative paths. - Updates
ZLibEncoder/GZipEncodercommentary to reflect the updated bound semantics and overhead assumptions. - Removes the mobile-only
[ActiveIssue]skip forRoundTrip_AllWindowLogsnow that the bound is expected to be sufficient.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateEncoder.cs | Reworks GetMaxCompressedLength to a managed conservative bound intended to cover all supported compression configurations. |
| src/libraries/System.IO.Compression/src/System/IO/Compression/ZLibEncoder.cs | Adjusts the method comment to reflect that the bound is an upper bound (not necessarily exact). |
| src/libraries/System.IO.Compression/src/System/IO/Compression/GZipEncoder.cs | Updates comment explaining gzip overhead relative to the deflate/zlib bound. |
| src/libraries/Common/tests/System/IO/Compression/EncoderDecoderTestBase.cs | Re-enables the all-window-log round-trip test by removing the mobile platform skip. |
Comment on lines
+150
to
+154
| // so z_stream's strstart and gzhead are always null. We also don't handle the s390 corner case. | ||
| ulong sourceLength = (ulong)inputLength; | ||
| ulong maxCompressedLength = sourceLength | ||
| + (sourceLength == 0 ? 1u : 0u) | ||
| + (sourceLength < 9 ? 1u : 0u) | ||
| + ((sourceLength + 7) >> 3) | ||
| + 3 // DEFLATE_BLOCK_OVERHEAD: (3 + 15 + 6) >> 3 | ||
| + 6; // ZLIB_WRAPLEN: zlib header (2 bytes) + Adler32 trailer (4 bytes) | ||
| const ulong wrapLength = 6; // GZIP_WRAPLEN is 18, GZipEncoder compensates for the rest | ||
| ulong maxCompressedLength = sourceLength + ((sourceLength + 7) >> 3) + ((sourceLength + 63) >> 6) + 5; | ||
| ulong storedBlockBound = sourceLength + (sourceLength >> 5) + (sourceLength >> 7) + (sourceLength >> 11) + 7; |
Member
Author
|
/azp run runtime-extra-platforms |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
This was referenced Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previous code was using the implementation from compressBound(). This api returns a conservative bound when using the compress() APIs, which our .NET implementation doesn't use (it uses deflate directly). The compress API does a deflate with default configuration, while our API allows custom configuration (via the quality level, window size and a few others). This means that the compressBound is not valid when using the deflate API with custom parameters.
zlib exposes the deflateBound method which can be used to obtain a conservative size. The problem with this API is that it requires to know the parameters of the compression, which we don't have available in our .NET API, given GetMaxCompressedLenght is static, applying universally to all compressions. While it is not yet decided if we will add a new API to support obtaining the bound from the library, this commit adds a workaround, by computing the maximum bound for every compression configuration exposed by our .NET APIs
The formula is obtained from our zlib-ng sources in deflate.c. Mobile uses zlib however. Validated that this change works correctly on maccatalyst, ios simulator and that the current logic stands with the current open source code for zlib (which applies to android). The actual bound used by Apple and Android can always change, so we do have a small risk that our hardcoded limit will diverge. I believe the only safe solution for this would be to make use of the actual deflateBound method, which requires adjustement to our API.
Fixes #127563