在将枚举值反序列化为设置了DataContract和EnumMember属性的.Net枚举时,是否有一种方法可以优化ServiceStack.Text(版本5.4.0)性能?我想使用EnumMember属性定义序列化的名称,但是不幸的是,与基于普通枚举成员名称的反序列化相比,ServiceStack.Text似乎需要大约三倍的时间才能使用此方法进行反序列化。
例如,考虑以下简单的C#测试程序:
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;
namespace ServiceStackPerformance
{
public class Program
{
[DataContract]
enum DataContractEnum
{
[EnumMember(Value = "first")]
First = 0,
[EnumMember(Value = "second")]
Second = 1,
}
enum PlainEnum
{
first = 0,
second = 1,
}
[DataContract]
class DataContractEnumList
{
[DataMember(Name = "values")]
public List<DataContractEnum> Values { get; set; }
}
[DataContract]
class PlainEnumList
{
[DataMember(Name = "values")]
public List<PlainEnum> Values { get; set; }
}
static void Main(string[] args)
{
int size = 100000;
string test = GenerateTestString(size);
var stopwatch = new Stopwatch();
stopwatch.Start();
var dataContractEnumList = test.FromJson<DataContractEnumList>();
var dataContractMs = stopwatch.ElapsedMilliseconds;
stopwatch.Restart();
var plainType = test.FromJson<PlainEnumList>();
var plainMs = stopwatch.ElapsedMilliseconds;
Console.WriteLine($"Deserializing input of size {2*size+1} to data contract enum took {dataContractMs} ms.");
Console.WriteLine($"Deserializing input of size {2*size+1} to simple enum took {plainMs} ms.");
}
private static string GenerateTestString(int size)
{
var builder = new StringBuilder(10*size);
builder.Append("{\"values\":[");
for ( int i = 0; i < size; i++)
{
builder.Append("\"first\",\"second\",");
}
builder.Append("\"first\"]}");
return builder.ToString();
}
}
}
输出如下:
Deserializing input of size 200001 to data contract enum took 3520 ms.
Deserializing input of size 200001 to simple enum took 1131 ms.
我可以启用任何缓存或其他性能优化来避免速度减慢吗?
答案 0 :(得分:2)
使用this commit可以改善使用[EnumMember]
的枚举的性能,并可以利用缓存的枚举信息对this commit中的枚举进行反序列化。
此更改可从v5.4.1(现在为available on MyGet)中获得。