注入通用getter和setter以获得比反射更好的性能

时间:2013-11-09 20:27:27

标签: c# .net reflection delegates dynamictype

我试图编写一个高级网络库(主要是为了好玩),用户可以通过派生类轻松定义其数据包。这样解析消息很简单。用户数据包应仅包含基本值types

为了做到这一点,我需要访问每个用户定义的数据包的每个字段。这个问题可以通过反射很容易地完成,但由于反射很慢,我无法使用它。为了快速做到这一点,我创建了一个类,在运行时为每个用户定义的数据包字段注入getter和setter(在StackOverflow上找到它)。获取者为Func<UserDefinedPacket, fieldType>,设置者为Action<UserDefinedPAcket, setValue>

以下是问题:因为用户数据包是在另一个程序集中定义的,所以在编译时我不可能知道getter或setter的类型。我可以用于getter和setter的派生类最多的是Delegate。这意味着我只能使用DynamicInvoke,所以我再次进行反射...但是因为我知道这些方法是实际的Funcs和Actions但实际上无法投射它们我可以使用动态类型并调用Invoke。动态类型将性能提高了大约5倍,但与正常的字段访问(大约快100倍)相比,设置和获取值仍然很慢,这使得该库无法使用。

最终我希望能够将用户定义的类转换为字节数组。此外,我不想序列化类,因为序列化包括一些我真的不想通过网络发送的垃圾数据,序列化似乎很慢。

以下是问题:我可以更快地制作GetVaue和SetValue方法吗?我尝试将代理转换为所需的函数/操作(移动库中的自定义数据包类,因此这不好),并且setter大约需要50ms而不是300ms。我希望能够获得通用数据包的性能。

namespace MirrorNet
{
    // Base class for packets
    // This will be derived by users and should only contain basic type fields
    public class UserPacket
    {
    }

public class MirrorNetManager
{
    private static MirrorNetManager instance = new MirrorNetManager();

    public static MirrorNetManager Instance
    {
        get { return instance; }
    }

    // Dictionary: packetType -> field -> getter | setter
    private class PropertyDictionary : Dictionary<Type, Dictionary<FieldInfo, Delegate>>
    {
    }

    private Dictionary<int, PacketConstructor> m_packetConstructors = new Dictionary<int, PacketConstructor>();
    private PropertyDictionary m_packetFieldGetters = new PropertyDictionary();
    private PropertyDictionary m_packetFieldSetters = new PropertyDictionary();

    public void SetValue(UserPacket packet, FieldInfo field, object value)
    {
        var setDelegate = m_packetFieldSetters[packet.GetType()][field];

        dynamic setAction = setDelegate; //Convert.ChangeType(setDelegate, setDelegate.GetType());
        dynamic setObject = packet;      //Convert.ChangeType(packet, packet.GetType());
        dynamic setValue  = value;       //Convert.ChangeType(value, value.GetType());

        setAction.Invoke(setObject, setValue);

        //setDelegate.DynamicInvoke(packet, value);
    }

    public object GetValue(UserPacket packet, FieldInfo field)
    {
        var getDelegate = m_packetFieldGetters[packet.GetType()][field];

        dynamic getFunction = getDelegate; //Convert.ChangeType(getDelegate, getDelegate.GetType());
        dynamic getObject   = packet;      //Convert.ChangeType(packet, packet.GetType());

        return getFunction.Invoke(getObject);

        //return getDelegate.DynamicInvoke(packet);
    }

    public void InitializePackets(Assembly packetsAssembly)
    {
        var typesArray = packetsAssembly.GetTypes();

        foreach (Type type in typesArray)
        {
            if (type.BaseType == typeof(UserPacket))
            {
                InsertPacketConstructor(type);
                InsertSettersAndGetters(type);
            }
        }
    }

    private void InsertPacketConstructor(Type packetType)
    {
        foreach (var member in packetType.GetFields())
        {
           Console.WriteLine(member);
           // TODO: Implement
        }
    }

    private void InsertSettersAndGetters(Type type)
    {
        Dictionary<FieldInfo, Delegate> getters = new Dictionary<FieldInfo, Delegate>();
        Dictionary<FieldInfo, Delegate> setters = new Dictionary<FieldInfo, Delegate>();

        foreach (FieldInfo field in type.GetFields())
        {
            Delegate getDelegate = CreateGetter(type, field.FieldType, field);
            Delegate setDelegate = CreateSetter(type, field.FieldType, field);

            getters.Add(field, getDelegate);
            setters.Add(field, setDelegate);
        }

        m_packetFieldGetters.Add(type, getters);
        m_packetFieldSetters.Add(type, setters);
    }

    private Delegate CreateGetter(Type classType, Type getReturnType, FieldInfo field)
    {
        string methodName = field.ReflectedType.FullName + ".get_" + field.Name;

        Type[] parameterTypes      = new Type[1] { classType };
        DynamicMethod getterMethod = new DynamicMethod(methodName, getReturnType, parameterTypes, true);
        ILGenerator gen = getterMethod.GetILGenerator();

        if (field.IsStatic)
        {
            gen.Emit(OpCodes.Ldsfld, field);
        }
        else
        {
            gen.Emit(OpCodes.Ldarg_0);
            gen.Emit(OpCodes.Ldfld, field);
        }
        gen.Emit(OpCodes.Ret);

        // Create the specific Func<,> instance
        Type[] typeArgs    = new Type[] { classType, getReturnType };
        Type   generic     = typeof(Func<,>);
        Type   genInstance = generic.MakeGenericType(typeArgs);

        Delegate getterDelegate = getterMethod.CreateDelegate(genInstance);

        return getterDelegate;
    }
    private Delegate CreateSetter(Type classType, Type setValueType,  FieldInfo field)
    {
        string methodName = field.ReflectedType.FullName + ".set_" + field.Name;
        Type[] parameters = new Type[2]
        {
            classType,
            setValueType
        };
        DynamicMethod setterMethod = new DynamicMethod(methodName, null, parameters);
        ILGenerator gen = setterMethod.GetILGenerator();

        if (field.IsStatic)
        {
            gen.Emit(OpCodes.Ldarg_1);
            gen.Emit(OpCodes.Stsfld, field);
        }
        else
        {
            gen.Emit(OpCodes.Ldarg_0);
            gen.Emit(OpCodes.Ldarg_1);
            gen.Emit(OpCodes.Stfld, field);
        }
        gen.Emit(OpCodes.Ret);

        // Create the specific Action<,> instance
        Type[] typeArgs    = new Type[] { classType, setValueType };
        Type   generic     = typeof(Action<,>);
        Type   genInstance = generic.MakeGenericType(typeArgs);

        Delegate ret = setterMethod.CreateDelegate(genInstance);
        return ret;
        }


    }
}

// THIS IS IN A DIFERENT ASSEMBLY
namespace MirrorNetTesting

{
// This is just an example packet
public class StudentPacket : UserPacket
{
    public int    age;
    public int    height;
    public double grades;
    public string firstName;
    public string lastName;
}
class Program
{
    static void Main(string[] args)
    {
        Assembly asm = Assembly.GetAssembly(typeof(StudentPacket));
        MirrorNetManager.Instance.InitializePackets(asm);

        PerformanceTesting();

        Console.ReadLine();
    }

    public static void PerformanceTesting()
    {
        int studentsCount = 1000 * 100;
        StudentPacket[] studentsArray = new StudentPacket[studentsCount];

        //////////////////////////////////////////////////////////////////////////

        Random rnd = new Random();

        for (int i = 0; i < studentsArray.Length; i++)
        {
            StudentPacket student = new StudentPacket();

            student.age    = rnd.Next();
            student.height = rnd.Next();
            student.grades = rnd.NextDouble();
            student.firstName = "First " + rnd.Next().ToString();
            student.lastName  = "Last "  + rnd.Next().ToString();

            studentsArray[i] = student;
        }

        var fieldsArray = typeof(StudentPacket).GetFields().ToArray();

        //////////////////////////////////////////////////////////////////////////

        // Begin normal getter test
        Console.WriteLine("Testing normal getters");
        Stopwatch normalGetterSw = new Stopwatch();

        normalGetterSw.Start();

        foreach (var student in studentsArray)
        {
            //object getValue;

            var getAge       = student.age;
            var getHeight    = student.height;
            var getGrades    = student.grades;
            var getFirstName = student.firstName;
            var getLastName  = student.lastName;
        }

        normalGetterSw.Stop();

        //////////////////////////////////////////////////////////////////////////

        // Begin reflection getter test
        Console.WriteLine("Testing reflection getters");
        Stopwatch reflectionGetterSw = new Stopwatch();

        reflectionGetterSw.Start();

        foreach (var student in studentsArray)
        {
            object getValue;

            for (int i = 0; i < fieldsArray.Length; i++ )
            {
                FieldInfo field = fieldsArray[i];
                getValue = MirrorNetManager.Instance.GetValue(student, field);
            }
        }

        reflectionGetterSw.Stop();

        //////////////////////////////////////////////////////////////////////////

        // Begin normal setter test
        Console.WriteLine("Testing normal setters");
        Stopwatch normalSetterSw = new Stopwatch();

        int    age       = 10;
        int    height    = 12;
        double grades    = 1432.523d;
        string firstName = "first name";
        string lastName  = "last name";

        normalSetterSw.Start();

        foreach (var student in studentsArray)
        {
            student.age       = age;
            student.height    = height;
            student.grades    = grades;
            student.firstName = firstName;
            student.lastName  = lastName;
        }

        normalSetterSw.Stop();

        //////////////////////////////////////////////////////////////////////////

        // Begin reflection setter test
        Console.WriteLine("Testing reflection setters ");
        Stopwatch reflectionSetterSw = new Stopwatch();

        object[] setValues = new object[]
        {
            age,
            height,
            grades,
            firstName,
            lastName
        };

        reflectionSetterSw.Start();

        foreach (var student in studentsArray)
        {
            for (int i = 0; i < fieldsArray.Length; i++ )
            {
                FieldInfo field = fieldsArray[i];
                MirrorNetManager.Instance.SetValue(student, field, setValues[i]);
            }
        }

        reflectionSetterSw.Stop();

        //////////////////////////////////////////////////////////////////////////

        Console.WriteLine("Normal getter:     \t {0}",     normalGetterSw.ElapsedMilliseconds);
        Console.WriteLine("Normal setter:     \t {0}",     normalSetterSw.ElapsedMilliseconds);
        Console.WriteLine("Reflection getter: \t {0}", reflectionGetterSw.ElapsedMilliseconds);
        Console.WriteLine("Reflection setter: \t {0}", reflectionSetterSw.ElapsedMilliseconds);

        //////////////////////////////////////////////////////////////////////////
    }
}

}

输出(仅限相关内容):

Normal getter:           3
Normal setter:           4
Reflection getter:       261
Reflection setter:       183

反射getter和setter实际上是动态调用,它们通常最终需要300ms。

此外,由于代码很长,我也发布了here

2 个答案:

答案 0 :(得分:2)

您是否考虑过略有不同的方法?在这种情况下,getter和setter的主要目的是序列化和反序列化,也许您应该专注于动态生成序列化方法,这将消除使用动态方法逐个访问这些字段的开销。

例如,假设你想使用BinaryFormatter进行序列化(虽然你可能会选择更好的东西),目标是动态生成如下方法:

    static private byte[] SerializeStudentPacket(StudentPacket packet)
    {
        var bf = new BinaryFormatter();
        var ms = new MemoryStream();
        bf.Serialize(ms, packet.age);
        bf.Serialize(ms, packet.firstName);
        bf.Serialize(ms, packet.grades);
        bf.Serialize(ms, packet.height);
        bf.Serialize(ms, packet.lastName);
        return ms.ToArray();
    }

使用Linq表达式可以使ILGenerator更简单:

    ParameterExpression @object = Expression.Parameter(typeof(StudentPacket), "@object");
    MethodInfo serializeMethodInfo = typeof(BinaryFormatter).GetMethod("Serialize", new Type[] { typeof(Stream), typeof(object) });
    MethodInfo toArrayMethodInfo = typeof(MemoryStream).GetMethod("ToArray");
    var bf = Expression.Variable(typeof(BinaryFormatter), "bf");
    var ms = Expression.Variable(typeof(System.IO.MemoryStream), "ms");
    List<Expression> expressions = new List<Expression>();
    expressions.Add(
        Expression.Assign(bf, Expression.New(typeof(BinaryFormatter))));
    expressions.Add(
        Expression.Assign(ms, Expression.New(typeof(MemoryStream))));
    foreach (FieldInfo field in typeof(StudentPacket).GetFields())
    {
        expressions.Add(
           Expression.Call(bf, serializeMethodInfo, ms, 
                           Expression.Convert(Expression.Field(@object, field.Name),
                                              typeof(object))));
    }
    expressions.Add(Expression.Call(ms, toArrayMethodInfo));
    var lambda = Expression.Lambda(
        Expression.Block(
            new[] { bf, ms },
            expressions
        ),
        @object);

然后,您当然可以将lambda.Compile()的结果存储为序列化StudentPacket。同样的方法也可以用于反序列化。

答案 1 :(得分:2)

为了完整起见,我设法降低了getter和setter的访问时间(kkokosa确实更好地解决了我的问题,但问题是我可以为getter和setter获得更好的性能)。

我做了这样的getter和setter:

get_firstName(UserPacket packet)
{
    var pk = (StudentPacket)packet;
    return pk.firstName;
}

void set_firstName(UserPacket packet, object value)
{
    var pk = (StudentPacket)packet;
    pk.firstName = (string)value;
}

使用表达式树而不是ILGenerator注入它们。现在我知道运行时委托的类型,我只能通过装箱和拆箱开销来逃避。 这个说法让我让吸气剂和制定者以大约50-70毫秒的速度运行(对于5场* 100.000个对象,并获得)。