Skip to content
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

ReflectionGraphicsStateOperationFactory and NativeAOT builds #790

Open
Numpsy opened this issue Mar 9, 2024 · 9 comments
Open

ReflectionGraphicsStateOperationFactory and NativeAOT builds #790

Numpsy opened this issue Mar 9, 2024 · 9 comments

Comments

@Numpsy
Copy link
Contributor

Numpsy commented Mar 9, 2024

A follow up to #664 and a question / enquiry:

As mentioned in 664, ReflectionGraphicsStateOperationFactory is doing some things with reflection that can cause issues when using linker trimming or doing NativeAOT builds.

As I've been wanting to learn a bit about how SourceGenerators work, I wondered if if would be possible to generate the list of graphics operations with one, rather than having to enumerate all types in the library looking for classes that implement IGraphicsStateOperation at runtime.

So, I had a play and put together this: master...Numpsy:PdfPig:source_gen

It's rather rough and glued together from generator samples, but the idea is that you can generate something like this at build time, and then just use the fixed collection of types with less runtime work:

image

Does anyone have any thoughts on if the idea might be useful to take beyond a learning exercise?

@Numpsy
Copy link
Contributor Author

Numpsy commented Mar 15, 2024

This has become a bit easier now, as it didn't work with .NET 4.5, but that's been removed by #795

@jsiedentop
Copy link

I've tried using PdfPig with AOT and had trim warnings and an exception during runtime. Once I built my application with your fix, everything went fine. Is there a plan, to bring your code generation approach to main?

@Numpsy
Copy link
Contributor Author

Numpsy commented Apr 22, 2024

It needs a bit more work to be production ready, but I can have another look if there's interest.

I've updated my branch with all the latest changes for now anyway.

@jsiedentop
Copy link

I am definitely interested. Did a lot research with libraries in different languages and PdfPig would be my preferred choice. Unfortunately I am depending on AOT. I didn't realize code generation in C# was a thing now too, but it's nice that it's leading to more compatibility here. Currently I know little about c# code generation myself, so not sure, if I would be much help, but let me know if i can assist anywhere else.

@Numpsy
Copy link
Contributor Author

Numpsy commented Apr 25, 2024

Now that everything has been updated to .NET 8 etc I'll have a go at doing a version using IIncrementalGenerator instead of ISourceGenerator as I gather that should be faster

@BobLd
Copy link
Collaborator

BobLd commented Apr 28, 2024

@Numpsy I'm not very familiar with source generators but we could also take the approach to hard code stuf. This is the approach I took, see here https://github.com/BobLd/PdfPig/blob/caly-20240412/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs

it works very well with aot

@Numpsy
Copy link
Contributor Author

Numpsy commented May 5, 2024

That's all the generator actually creates, so if the list of items doesn't change much then just hardcoding it is simpler.

@BobLd
Copy link
Collaborator

BobLd commented May 5, 2024

Feel free to create a PR when you have time @Numpsy. More than happy to review it and merge it

@Numpsy
Copy link
Contributor Author

Numpsy commented May 9, 2024

@jsiedentop We've merged some changes should fix some of the problems here - would you be able to test your case when we have a nightly build containing #832 and see if that works now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants