Make PdfSharpCore trimmable #293
Merged
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.
Trimming enables the compiler to strip out parts of the library and the framework that are unused. It does this by analyzing the code at compile time. Some patterns (esp. heavy reflection usage) can make it hard for the compiler to automatically analyze the code. By using DynamicallyAccessedMembers attributes we can help the compiler.
I fixed all the trim-warnings the compiler produced for me. I was quite surprised how little was needed to make this work.
A SampleApp that just puts text in a PDF (probably the ideal case for trimming) was 70.5MB (self-contained) before and with trimming is only 21.5MB (-70%).
This is also a mayor focus area of .NET 7.
The most difficult part was the constructor of DictionaryMeta
DictionaryMeta(Type type)
, here the older .net framework code works better with trimming on .NET 5 and 6.