Skip to content

Commit

Permalink
Use UnsafeAccessor to call internal Rid constructor without reflection
Browse files Browse the repository at this point in the history
pkdawson committed Sep 9, 2024
1 parent 14e05b6 commit ff08a76
Showing 2 changed files with 13 additions and 8 deletions.
11 changes: 3 additions & 8 deletions addons/imgui-godot/ImGuiGodot/Internal/Util.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using Godot;
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace ImGuiGodot.Internal;

internal static class Util
{
public static Rid ConstructRid(ulong id)
{
ReadOnlySpan<ulong> uspan = new(in id);
ReadOnlySpan<byte> bytes = MemoryMarshal.Cast<ulong, byte>(uspan);
return MemoryMarshal.Read<Rid>(bytes);
}
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
public static extern Rid ConstructRid(ulong id);
}
10 changes: 10 additions & 0 deletions doc/test/csbench/BenchRid.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
using System.Reflection.Emit;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace csbench;

@@ -58,6 +59,15 @@ public Rid ConstructRid_MemoryMarshal_SpanCast()
return MemoryMarshal.Read<Rid>(bytes);
}

[Benchmark]
public Rid ConstructRid_UnsafeAccessor()
{
return RidConstructor(_id);
}

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
private static extern Rid RidConstructor(ulong id);

[Benchmark(Baseline = true)]
public unsafe Rid ConstructRid_Unsafe_DirectCopy()
{

0 comments on commit ff08a76

Please sign in to comment.