-
Notifications
You must be signed in to change notification settings - Fork 571
Replace AssemblyStore compression algorithm LZ4 with Zstd #11730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
9
commits into
main
Choose a base branch
from
copilot/replace-lz4-with-zstd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dbf0f8f
Initial plan
Copilot 6850ac9
Replace AssemblyStore LZ4 compression with Zstd
Copilot ed3ba4c
Update CoreCLR APK size descriptors
simonrozsival d05c82a
Use LibraryImport in Zstd tools
simonrozsival 15c5588
Use BCL ZstandardDecoder in tools
simonrozsival 7b59da5
Inline ZstandardDecoder call
simonrozsival 78d6a04
Address review feedback for Zstd compression changes
Copilot 46e5591
Update CoreCLR apkdesc baselines from CI build 1482888
simonrozsival d9a8b0c
Fix Zstd.Compress doc to match actual compression level
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace Xamarin.Android.Tasks | ||
| { | ||
| /// <summary> | ||
| /// Minimal managed wrapper around the Zstandard compression functions exported by | ||
| /// <c>libSystem.IO.Compression.Native</c>, which ships in the .NET runtime pack. | ||
| /// We use it to compress assemblies that are placed in the assembly store; the native | ||
| /// runtime decompresses them at load time using the same library. | ||
| /// </summary> | ||
| static class Zstd | ||
| { | ||
| // libSystem.IO.Compression.Native exports the raw zstd entry points (no prefix). | ||
| const string ZstdLibrary = "System.IO.Compression.Native"; | ||
|
simonrozsival marked this conversation as resolved.
|
||
|
|
||
| // ZSTD_cParameter value for ZSTD_c_compressionLevel (see zstd.h). | ||
| const int ZSTD_c_compressionLevel = 100; | ||
|
simonrozsival marked this conversation as resolved.
|
||
| const int ZstdCompressionLevel = 3; | ||
|
|
||
| [DllImport (ZstdLibrary, CallingConvention = CallingConvention.Cdecl)] | ||
| static extern UIntPtr ZSTD_compressBound (UIntPtr srcSize); | ||
|
|
||
| [DllImport (ZstdLibrary, CallingConvention = CallingConvention.Cdecl)] | ||
| static extern IntPtr ZSTD_createCCtx (); | ||
|
|
||
| [DllImport (ZstdLibrary, CallingConvention = CallingConvention.Cdecl)] | ||
| static extern UIntPtr ZSTD_freeCCtx (IntPtr cctx); | ||
|
|
||
| [DllImport (ZstdLibrary, CallingConvention = CallingConvention.Cdecl)] | ||
| static extern UIntPtr ZSTD_CCtx_setParameter (IntPtr cctx, int param, int value); | ||
|
|
||
| [DllImport (ZstdLibrary, CallingConvention = CallingConvention.Cdecl)] | ||
| static extern UIntPtr ZSTD_compress2 (IntPtr cctx, byte[] dst, UIntPtr dstCapacity, byte[] src, UIntPtr srcSize); | ||
|
|
||
| [DllImport (ZstdLibrary, CallingConvention = CallingConvention.Cdecl)] | ||
| static extern uint ZSTD_isError (UIntPtr code); | ||
|
|
||
| /// <summary> | ||
| /// Returns the maximum size that compressed data of <paramref name="inputSize"/> bytes can occupy. | ||
| /// </summary> | ||
| public static int MaximumOutputSize (int inputSize) | ||
| { | ||
| if (inputSize < 0) | ||
| return -1; | ||
|
|
||
| try { | ||
| UIntPtr result = ZSTD_compressBound ((UIntPtr) (uint) inputSize); | ||
| if (ZSTD_isError (result) != 0) | ||
| return -1; | ||
|
|
||
| ulong maxOutputSize = (ulong) result; | ||
| if (maxOutputSize > int.MaxValue) | ||
| return -1; | ||
|
|
||
| return (int) maxOutputSize; | ||
| } catch (DllNotFoundException) { | ||
| return -1; | ||
| } catch (EntryPointNotFoundException) { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compresses <paramref name="inputLength"/> bytes from <paramref name="input"/> into | ||
| /// <paramref name="output"/> using zstd's default compression level. Returns the number of | ||
| /// bytes written to <paramref name="output"/>, or <c>-1</c> if compression failed. | ||
| /// </summary> | ||
| public static int Compress (byte[] input, int inputLength, byte[] output) | ||
| { | ||
| if (input == null) | ||
| throw new ArgumentNullException (nameof (input)); | ||
| if (output == null) | ||
| throw new ArgumentNullException (nameof (output)); | ||
| if (inputLength < 0 || inputLength > input.Length) | ||
| throw new ArgumentOutOfRangeException (nameof (inputLength)); | ||
|
|
||
| IntPtr cctx; | ||
| try { | ||
| cctx = ZSTD_createCCtx (); | ||
| } catch (DllNotFoundException) { | ||
| return -1; | ||
| } catch (EntryPointNotFoundException) { | ||
| return -1; | ||
| } | ||
|
|
||
| if (cctx == IntPtr.Zero) | ||
| return -1; | ||
|
|
||
| try { | ||
| UIntPtr setParameterResult = ZSTD_CCtx_setParameter (cctx, ZSTD_c_compressionLevel, ZstdCompressionLevel); | ||
| if (ZSTD_isError (setParameterResult) != 0) | ||
| return -1; | ||
|
|
||
| UIntPtr result = ZSTD_compress2 (cctx, output, (UIntPtr) (uint) output.Length, input, (UIntPtr) (uint) inputLength); | ||
| if (ZSTD_isError (result) != 0) | ||
| return -1; | ||
|
|
||
| ulong encodedLength = (ulong) result; | ||
| if (encodedLength > int.MaxValue) | ||
| return -1; | ||
|
|
||
| return (int) encodedLength; | ||
| } catch (DllNotFoundException) { | ||
| return -1; | ||
| } catch (EntryPointNotFoundException) { | ||
| return -1; | ||
| } finally { | ||
| try { | ||
| ZSTD_freeCCtx (cctx); | ||
| } catch (DllNotFoundException) { | ||
| } catch (EntryPointNotFoundException) { | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.