Skip to content

Commit

Permalink
Eliminate an ObjectReference object in a couple custom types (#1090)
Browse files Browse the repository at this point in the history
* Removing unnecessary objref while creating Uri from ABI.

* Replace improvement with 2 other types

* Make IReferenceArray use function pointers similar to previous PR which did it for other types.

* Update testwinrt
  • Loading branch information
manodasanW authored Jan 25, 2022
1 parent a75005d commit 1e4cd5e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
18 changes: 18 additions & 0 deletions src/Benchmarks/ReflectionPerf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,24 @@ public string CreateAndIterateList()
sentence += list[i];
}
return sentence;
}

[Benchmark]
public object GetUri()
{
return instance.NewUri;
}

[Benchmark]
public void SetUri()
{
instance.NewUri = new Uri("https://github.com");
}

[Benchmark]
public object GetExistingUri()
{
return instance.ExistingUri;
}
}
}
3 changes: 1 addition & 2 deletions src/WinRT.Runtime/Projections/DataErrorsChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,10 @@ public static IObjectReference CreateMarshaler(global::System.ComponentModel.Dat
return null;
}

using var args = ObjectReference<ABI.Microsoft.UI.Xaml.Data.IDataErrorsChangedEventArgsVftbl>.FromAbi(ptr);
IntPtr propertyName = IntPtr.Zero;
try
{
ExceptionHelpers.ThrowExceptionForHR(args.Vftbl.get_PropertyName_0(args.ThisPtr, &propertyName));
ExceptionHelpers.ThrowExceptionForHR((**(ABI.Microsoft.UI.Xaml.Data.IDataErrorsChangedEventArgsVftbl**)ptr).get_PropertyName_0(ptr, &propertyName));
return new global::System.ComponentModel.DataErrorsChangedEventArgs(MarshalString.FromAbi(propertyName));
}
finally
Expand Down
22 changes: 13 additions & 9 deletions src/WinRT.Runtime/Projections/IReferenceArray.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ namespace ABI.Windows.Foundation
internal static class BoxedArrayIReferenceArrayImpl<T>
{
private static readonly IReferenceArray<T>.Vftbl AbiToProjectionVftable;
public static readonly IntPtr AbiToProjectionVftablePtr;
public static readonly IntPtr AbiToProjectionVftablePtr;
private static readonly Delegate DelegateCache;

static unsafe BoxedArrayIReferenceArrayImpl()
{
AbiToProjectionVftable = new IReferenceArray<T>.Vftbl
{
IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable,
get_Value_0 = Do_Abi_get_Value_0
_get_Value_0 = (void*)Marshal.GetFunctionPointerForDelegate(DelegateCache = new IReferenceArray_Delegates.get_Value_0(Do_Abi_get_Value_0))
};
var nativeVftbl = (IntPtr*)ComWrappersSupport.AllocateVtableMemory(typeof(BoxedArrayIReferenceArrayImpl<T>), Marshal.SizeOf<global::WinRT.IInspectable.Vftbl>() + sizeof(IntPtr) * 1);
Marshal.StructureToPtr(AbiToProjectionVftable.IInspectableVftbl, (IntPtr)nativeVftbl, false);
nativeVftbl[6] = Marshal.GetFunctionPointerForDelegate(AbiToProjectionVftable.get_Value_0);
nativeVftbl[6] = (IntPtr)AbiToProjectionVftable.GetValue_0;

AbiToProjectionVftablePtr = (IntPtr)nativeVftbl;
}
Expand Down Expand Up @@ -102,18 +104,20 @@ public static IntPtr FromManaged(object value)
public static string GetGuidSignature() => GuidGenerator.GetSignature(typeof(IReferenceArray<T>));

[Guid("61C17707-2D65-11E0-9AE8-D48564015472")]
public struct Vftbl
public unsafe struct Vftbl
{
internal IInspectable.Vftbl IInspectableVftbl;
public IReferenceArray_Delegates.get_Value_0 get_Value_0;
internal IInspectable.Vftbl IInspectableVftbl;
internal void* _get_Value_0;
internal delegate* unmanaged[Stdcall]<IntPtr, out int, out IntPtr, int> GetValue_0 { get => (delegate* unmanaged[Stdcall]<IntPtr, out int, out IntPtr, int>)_get_Value_0; set => _get_Value_0 = (void*)value; }

public static Guid PIID = GuidGenerator.CreateIID(typeof(IReferenceArray<T>));

internal unsafe Vftbl(IntPtr thisPtr)
internal unsafe Vftbl(IntPtr thisPtr) : this()
{
var vftblPtr = Marshal.PtrToStructure<VftblPtr>(thisPtr);
var vftbl = (IntPtr*)vftblPtr.Vftbl;
IInspectableVftbl = Marshal.PtrToStructure<IInspectable.Vftbl>(vftblPtr.Vftbl);
get_Value_0 = Marshal.GetDelegateForFunctionPointer<IReferenceArray_Delegates.get_Value_0>(vftbl[6]);
GetValue_0 = (delegate* unmanaged[Stdcall]<IntPtr, out int, out IntPtr, int>)vftbl[6];
}
}

Expand All @@ -140,7 +144,7 @@ public unsafe T[] Value
IntPtr __retval_data = default;
try
{
global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.get_Value_0(ThisPtr, out __retval_length, out __retval_data));
global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetValue_0(ThisPtr, out __retval_length, out __retval_data));
return Marshaler<T>.FromAbiArray((__retval_length, __retval_data));
}
finally
Expand Down
3 changes: 1 addition & 2 deletions src/WinRT.Runtime/Projections/PropertyChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ public static IObjectReference CreateMarshaler(global::System.ComponentModel.Pro
return null;
}

using var args = ObjectReference<ABI.Microsoft.UI.Xaml.Data.IPropertyChangedEventArgsVftbl>.FromAbi(ptr);
IntPtr propertyName = IntPtr.Zero;
try
{
ExceptionHelpers.ThrowExceptionForHR(args.Vftbl.get_PropertyName_0(args.ThisPtr, &propertyName));
ExceptionHelpers.ThrowExceptionForHR((**(ABI.Microsoft.UI.Xaml.Data.IPropertyChangedEventArgsVftbl**)ptr).get_PropertyName_0(ptr, &propertyName));
return new global::System.ComponentModel.PropertyChangedEventArgs(MarshalString.FromAbi(propertyName));
}
finally
Expand Down
3 changes: 1 addition & 2 deletions src/WinRT.Runtime/Projections/Uri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,10 @@ public static IObjectReference CreateMarshaler(global::System.Uri value)
return null;
}

using var uri = ObjectReference<ABI.Windows.Foundation.IUriRuntimeClassVftbl>.FromAbi(ptr);
IntPtr rawUri = IntPtr.Zero;
try
{
ExceptionHelpers.ThrowExceptionForHR(uri.Vftbl.get_RawUri_10(uri.ThisPtr, &rawUri));
ExceptionHelpers.ThrowExceptionForHR((**(ABI.Windows.Foundation.IUriRuntimeClassVftbl**)ptr).get_RawUri_10(ptr, &rawUri));
return new global::System.Uri(MarshalString.FromAbi(rawUri));
}
finally
Expand Down
2 changes: 1 addition & 1 deletion src/get_testwinrt.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ git checkout -f master
if ErrorLevel 1 popd & exit /b !ErrorLevel!
git fetch -f
if ErrorLevel 1 popd & exit /b !ErrorLevel!
git reset -q --hard c08bbd40da7f93e29ca2ed683e32121a4692535d
git reset -q --hard 1071d8ee25b8dcb0a1874f6f2b6de2e97fb4c74c
if ErrorLevel 1 popd & exit /b !ErrorLevel!
echo Restoring Nuget
%this_dir%.nuget\nuget.exe restore
Expand Down

0 comments on commit 1e4cd5e

Please sign in to comment.