From 12e56b8a0fe0c06661eb646fe1c29825a331460f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 15 Sep 2019 13:48:46 +0200 Subject: [PATCH] Added dynamic method builder for member values --- .../Models/ReadableMember.GetValue.cs | 67 +++++++++++++++++++ .../Translation/Models/ReadableMember.cs | 22 ++---- 2 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs new file mode 100644 index 00000000..e30bb2be --- /dev/null +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; +using System.Reflection.Emit; + +namespace ComputeSharp.Shaders.Translation.Models +{ + internal sealed partial class ReadableMember + { + /// + /// The mapping of members to getter delegates + /// + private static readonly Dictionary GettersMapping = new Dictionary(); + + /// + /// Returns the value of the wrapped member for the current instance + /// + /// The target instance to use to read the value from + [Pure] + public object GetValue(object? instance) + { + if (!GettersMapping.TryGetValue(Id, out Getter getter)) + { + getter = BuildDynamicGetter(); + GettersMapping.Add(Id, getter); + } + + return getter(instance); + } + + /// + /// A that represents a getter for a specific member + /// + /// The source object to get the member from + /// The value of the member, upcast to + private delegate object Getter(object? obj); + + /// + /// Builds a dynamic IL method to retrieve the value of the current member + /// + [Pure] + private Getter BuildDynamicGetter() + { + // Create a new dynamic method for the current member + DynamicMethod method = new DynamicMethod($"Get{Id}", typeof(object), new[] { typeof(object) }, DeclaringType); + ILGenerator il = method.GetILGenerator(); + + // Load the argument (the object instance) and cast it to the right type + il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Castclass, DeclaringType); + + // Get the member value with the appropriate method + if (Property != null) il.EmitCall(OpCodes.Callvirt, Property.GetMethod, null); + else if (Field != null) il.Emit(OpCodes.Ldfld, Field); + else throw new InvalidOperationException("Field and property can't both be null at the same time"); + + // Box the value, if needed + if (MemberType.IsValueType) il.Emit(OpCodes.Box, MemberType); + + // Return the member value from the top of the evaluation stack + il.Emit(OpCodes.Ret); + + // Build and return the delegate + return (Getter)method.CreateDelegate(typeof(Getter)); + } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs index b0ba6344..02c66f33 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.Contracts; using System.Reflection; namespace ComputeSharp.Shaders.Translation.Models @@ -7,8 +6,13 @@ namespace ComputeSharp.Shaders.Translation.Models /// /// A that wraps a readable member of another and provides access to it /// - internal sealed class ReadableMember + internal sealed partial class ReadableMember { + /// + /// The id of the current member + /// + private readonly string Id; + /// /// The object wrapped by the current instance, if present /// @@ -28,6 +32,7 @@ namespace ComputeSharp.Shaders.Translation.Models { Field = field; Property = property; + Id = $"{DeclaringType.FullName}{Type.Delimiter}{Name}".Replace(Type.Delimiter, '_'); } /// @@ -63,19 +68,6 @@ namespace ComputeSharp.Shaders.Translation.Models /// public string Name => (Field?.Name ?? Property?.Name) ?? throw new InvalidOperationException("Field and property can't both be null at the same time"); - /// - /// Returns the value of the wrapped member for the current instance - /// - /// The target instance to use to read the value from - [Pure] - public object GetValue(object? instance) - { - if (Field != null) return Field.GetValue(instance); - if (Property != null) return Property.GetValue(instance); - - throw new InvalidOperationException("Field and property can't both be null at the same time"); - } - /// /// Converts a object into a instance ///