Added: Assembly Trimming Annotations for COM Related Components in SharpGen.Runtime#223
Conversation
…s a superset of Interfaces
…d objs passed to native)
I'd actually like to hear opinions about that; I've only really seen these used with COM in the context of However, it might be safer to enforce the whole types are preserved in the potential future scenario of Seems I'll also need to do something about |
What about explicit interface method implementations? They are non-public, as far as I remember. |
|
I have strong build infra concerns around |
So, basically, to everything related to SharpGen and it's users. That's excessive, but I'm not sure if there is a way to preserve semantics. The semantics are: everything that is derived from |
That's a really good point. I also agree with moving the dummy to the COM solution. I was thinking about that part actually. Though I think it might be easier to have a separate dummy just for COM as far as development goes anyway, because then dealing with potential future analyzer warnings in main Runtime Lib would be pretty annoying. As for the preserving the semantics, yeah, it'd be pretty difficult without breaking the existing public API. I'll comment more when I get working on this later. |
SharpGen.RuntimeSharpGen.Runtime
…uperset of Interfaces incl. explicit interfaces]]
.NET 5 Annotations
I made a dummy project to test this, seems like that's right, so I've updated the annotations to use Note: The test project uses 3 projects because Trimming DummyI removed Original PostUpdated to reflect all changes. The Remaining ProblemBest just quoted.
Yeah, I'm not 100% sure if it's possible either without a change in public API. This requires some further investigation. Edit: I have an idea for a clean way to handle this one actually, will need to test a thing or two after a break. |
This reverts commit 4a31d54.
|
Okay, think the best approach for the remaining problem (don't preserve all static void PreserveInterface<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interface)]T>() { }I've never worked on a source generator before but this sounds simple enough, I'm gonna give it a go for now. Unrelated ShenanigansWhile investigating possible solutions I ran into a very funny hack that lets you preserve every type that inherits from a type at the expense of 2 x64 instructions. It's too funny not to share. [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] // Only 'All' seems to preserve interfaces.
// [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All | DynamicallyAccessedMemberTypes.PublicMethods)] // Doesn't preserve interface contents :(
public class InterfaceImplementer : IGoodBye, IHello
{
private bool _false = false;
public InterfaceImplementer()
{
if (_false) // Always false
PreserveMe(GetType()); // GetType exposes class which parents InterfaceImplementer
}
public void SayHello() => Console.WriteLine("We're no strangers to love");
public void SayGoodbye() => Console.WriteLine("Never gonna");
private static void PreserveMe([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type value) { }
}
public class BaseClassImplementer : InterfaceImplementer, IGoodBye, IHello
{
public void Execute() => Console.WriteLine("execution. execution. execution. execution.");
}
public interface IGoodBye
{
void SayGoodbye();
}
public interface IHello
{
void SayHello();
}[Unfortunately Linker doesn't have a |
|
I added Analyzer Warnings for any This PR should be closer to a mergeable state now. Needs more testing for the .NET 5 case, I'll also need to revert the change that replaced I'm just keeping everyone updated; I'll explicitly mention in the post when the PR is ready for final review and merging. |
Due to complications and some weird cases where the linker may preserve the interface but link away the internal code, I'm forced to preserve all instead of just the interfaces for now [I might need to open an issue report on linker repo] and have pushed the relevant change to the fork. Trimming in general is hard to test, however I believe the code is ready for testing on a real project; so I'll try IL Linking |
|
I have marked I can at least confirm that the trimming experiment is a success; all
(CC. @amerkoleci ) Tested ApplicationsSizes are for All projects published with:
You can find my fork of Vortice here if you wish to inspect the results yourself. Test Results✔ AdvancedTextRenderingApp Size: ✔ HelloDirect3D11 Size: ✔ HelloDirect3D12 Size: ✔ HelloDirectML Size: ✔ HelloDirectInput Size: ✔ AudioPlay Size: ✔ EnumerateDevices Size: ✔ HelloXInput Size: [Doesn't actually use SharpGen, just for completeness since I did all the others] ❓ HelloDirect3D12Raytracing Size: My GPU (R9 290) does not support Raytracing. I cannot verify this sample works. ❓ HelloDirectStorage Size: Unsupported on my hardware. |
SharpGen.RuntimeSharpGen.Runtime
Thanks, currently I'm on vacation, will be back on July 4th and check your changes, thanks again! |
|
@jkoritzinsky @andrew-boyarshin PR should be ready for review. Opening post should be accurate with current PR changes. This was a lot of work for a post-weekend project 😅. |
|
When I read the announcement, this PR suddenly came back to mind; so I thought I'd spread the news if there was anyone out there who was also interested. |
|
Are there still plans to merge this? I'm trying to use Vortice in an AoT scenario, and the lack of annotations on the currently released version are causing it to completely fail. |
I am having the same issue as this one. Would be nice to get any suggestions/ideas on the workaround. |
|
I should be able to go back to my Vortice branch and submit the PR to add trimmer annotations now. Just need a package to make it nuget.org, I don't think @amerkoleci would be comfortable with having a dependency a nightly package from a MyGet feed. (At least I wouldn't). It'd cause a few issues 😅. |
|
Whenever you folks get some time, please create a new (beta) package :) |
|
Tag: v2.0.0-beta.12 has been released and new nuget is on the way. |
|
Not sure why SharpGen.Runtime.COM didn't being updated to latest tag, any thought @andrew-boyarshin @jkoritzinsky ? |
|
It just dawned on me I missed the whole NuGet package release (oops!). I'm about to finish my first ever Rust library (port of one of my C# libraries); I'll get on it right after that. |





This PR picks up the work from #222.
Changes
Marked
SharpGen.Runtime.COMas trimmable.SharpGen.Runtimeitself.Added a separate dummy trim tester to
SharpGen.Runtime.COMin order to keep build system simple.Extended
SharpGen.Generatorto automatically preserve interfaces of types inheriting fromCallbackBase.CallbackBase.)no-opfunctionTrimmingHelpers.PreserveMe<T>to the module initializer.No changes to existing public API.
Notes
Variant.ValueinSharpGen.Runtime.COMdoes make use of Reflection technically,GetTypeand check if typeIs Primitive, however this has no effect on linking since the type just needs to exist for that to be resolved in the first place; and since it's passed into the method, it exists.