Skip to content

Commit

Permalink
perf(Animations): Marshal pointers instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed Apr 5, 2023
1 parent f735d89 commit d51f099
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
10 changes: 2 additions & 8 deletions src/Uno.Foundation.Runtime.WebAssembly/Interop/JSObject.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ namespace Uno.Foundation.Interop
[Obfuscation(Feature = "renaming", Exclude = true)]
public sealed class JSObject
{
private static readonly Func<string, IntPtr> _strToIntPtr =
Marshal.SizeOf<IntPtr>() == 4
? (s => (IntPtr)int.Parse(s, CultureInfo.InvariantCulture))
: (Func<string, IntPtr>)(s => (IntPtr)long.Parse(s, CultureInfo.InvariantCulture));

/// <summary>
/// Used by javascript to dispatch a method call to the managed object at <paramref name="handlePtr"/>.
/// </summary>
[Obfuscation(Feature = "renaming", Exclude = true)]
public static void Dispatch(string handlePtr, string method, string parameters)
public static void Dispatch(IntPtr handlePtr, string method, string parameters)
{
var intPtr = _strToIntPtr(handlePtr);
var handle = GCHandle.FromIntPtr(intPtr);
var handle = GCHandle.FromIntPtr(handlePtr);

if (!handle.IsAllocated)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public long CreateNativeInstance(IntPtr managedHandle)
#if NET7_0_OR_GREATER
NativeMethods.CreateInstance(managedHandle, id);
#else
WebAssemblyRuntime.InvokeJS($"Windows.UI.Xaml.Media.Animation.RenderingLoopAnimator.createInstance(\"{managedHandle}\", \"{id}\")");
WebAssemblyRuntime.InvokeJS($"Windows.UI.Xaml.Media.Animation.RenderingLoopAnimator.createInstance({managedHandle}, {id})");
#endif

return id;
Expand All @@ -118,7 +118,7 @@ public void DestroyNativeInstance(IntPtr managedHandle, long jsHandle)
#if NET7_0_OR_GREATER
NativeMethods.DestroyInstance(jsHandle);
#else
WebAssemblyRuntime.InvokeJS($"Windows.UI.Xaml.Media.Animation.RenderingLoopAnimator.destroyInstance(\"{jsHandle}\")");
WebAssemblyRuntime.InvokeJS($"Windows.UI.Xaml.Media.Animation.RenderingLoopAnimator.destroyInstance({jsHandle})");
#endif

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/ts/Interop/ManagedObject.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace Uno.Foundation.Interop {
export class ManagedObject {
private static assembly: UI.Interop.IMonoAssemblyHandle;
private static dispatchMethod: (handle: string, method: string, parameters: string) => number;
private static dispatchMethod: (handle: number, method: string, parameters: string) => number;

private static init() {
ManagedObject.dispatchMethod = (<any>Module).mono_bind_static_method("[Uno.Foundation.Runtime.WebAssembly] Uno.Foundation.Interop.JSObject:Dispatch");
}

public static dispatch(handle: string, method: string, parameters: string): void {
public static dispatch(handle: number, method: string, parameters: string): void {
if (!ManagedObject.dispatchMethod) {
ManagedObject.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export class RenderingLoopAnimator {
private static activeInstances: { [jsHandle: number]: RenderingLoopAnimator} = {};

public static createInstance(managedHandle: string, jsHandle: number) {
public static createInstance(managedHandle: number, jsHandle: number) {
RenderingLoopAnimator.activeInstances[jsHandle] = new RenderingLoopAnimator(managedHandle);
}

Expand All @@ -20,7 +20,7 @@
delete RenderingLoopAnimator.activeInstances[jsHandle];
}

private constructor(private managedHandle: string) {
private constructor(private managedHandle: number) {
}

public SetStartFrameDelay(delay: number) {
Expand Down

0 comments on commit d51f099

Please sign in to comment.