I'm trying out CsWin32 with the "allowMarshaling": false mode enabled, which generates blittable struct types for all COM types, instead of interfaces. That is great, but I noticed the generated code is leaving a bit of performance on the table. Specifically:
This is an example, but the same applies to all instance methods across all generated types. Pinning this is actually not needed, because the actual value will always be on native memory (since users would always just receive a pointer to a native COM object).
We can improve this by instead generating:
public uint AddRef()
{
return ((delegate *unmanaged [Stdcall]<IUnknown*,uint>)lpVtbl[1])((IUnknown*)Unsafe.AsPointer(ref this));
}
We might want this gated behind a separate JSON flag maybe? But I'd argue this should just always be the case. If anyone ever copied the actual value pointed to by a pointer to a COM object, that would most definitely just be a bug.
We might want to only do this if Unsafe.AsPointer is available on the target project (which can be checked easily enough).
Context
CsWin32 version: 0.3.2-beta
Target Framework: tested on .NET 8 Preview 5 and .NET Standard 2.0
Overview
I'm trying out CsWin32 with the
"allowMarshaling": false
mode enabled, which generates blittable struct types for all COM types, instead of interfaces. That is great, but I noticed the generated code is leaving a bit of performance on the table. Specifically:This is an example, but the same applies to all instance methods across all generated types. Pinning
this
is actually not needed, because the actual value will always be on native memory (since users would always just receive a pointer to a native COM object).We can improve this by instead generating:
Open questions
Unsafe.AsPointer
is available on the target project (which can be checked easily enough).Context
0.3.2-beta
LangVersion
(if explicitly set by project): 11