ServiceStack,第一次反序列化速度慢?

ServiceStack, Slowness on first Deserialization?

我正在 C# 应用程序中测试替代反序列化方法。我现在正在试用 ServiceStack (4.0.38),我发现了一些奇怪的行为。这是我的测试代码:

    public class BasicObject
    {
        public List<string> Stuff { get; set; }

        public BasicObject()
        {
            Stuff = new List<string>();
        }
    }

    private void CustomTesting()
    {
        var largeObject = new BasicObject();
        var mediumObject = new BasicObject();
        var smallObject = new BasicObject();
        //Populate this shiz
        for (int i = 0; i < 100000; i++)
        {
            if (i < 50000)
                mediumObject.Stuff.Add("HelloWorld");

            if (i < 2000)
                smallObject.Stuff.Add("HelloWorld");

            largeObject.Stuff.Add("HelloWorld");
        }

        //Serialize, save to disk
        using(var stream = new MemoryStream())
        {
            JsonSerializer.SerializeToStream<BasicObject>(largeObject, stream);
            File.WriteAllBytes("C:\Temp\Large", stream.ToArray());
        }
        using (var stream = new MemoryStream())
        {
            JsonSerializer.SerializeToStream<BasicObject>(mediumObject, stream);
            File.WriteAllBytes("C:\Temp\Medium", stream.ToArray());
        }
        using (var stream = new MemoryStream())
        {
            JsonSerializer.SerializeToStream<BasicObject>(smallObject, stream);
            File.WriteAllBytes("C:\Temp\Small", stream.ToArray());
        }

        var watch = new Stopwatch();

        using (var stream = new MemoryStream(File.ReadAllBytes("C:\Temp\Large")))
        {
            watch.Start();
            var test = JsonSerializer.DeserializeFromStream<BasicObject>(stream);
            watch.Stop();
        }
        var timeTakenLarge = watch.Elapsed.Milliseconds;
        watch.Restart();

        using (var stream = new MemoryStream(File.ReadAllBytes("C:\Temp\Medium")))
        {
            watch.Start();
            var test = JsonSerializer.DeserializeFromStream<BasicObject>(stream);
            watch.Stop();
        }
        var timeTakenMedium = watch.Elapsed.Milliseconds;
        watch.Restart();

        using (var stream = new MemoryStream(File.ReadAllBytes("C:\Temp\Small")))
        {
            watch.Start();
            var test = JsonSerializer.DeserializeFromStream<BasicObject>(stream);
            watch.Stop();
        }
        var timeTakenSmall = watch.Elapsed.Milliseconds;
        watch.Restart();

        Console.Clear();
        Console.WriteLine(string.Format("{0}  {1}  {2}", timeTakenLarge, timeTakenMedium, timeTakenSmall));
        Console.ReadKey();
    }

很简单,拿一个基本对象,用 3 个不同范围的数据填充它。序列化它,将它保存到磁盘。然后稍后将保存的文件放入内存流中,看看 ServiceStack 反序列化需要多长时间。这是我的输出(以毫秒为单位的时间)

Deserialization Order: Large Med Small
Display Order: Large Med Small
209  61  1
222  73  1
178  57  1
174  60  1

Deserialization Order: Small Med Large
Display Order: Large Med Small
145  48  66
128  72  63   
100  58  79
98  55  57

Deserialization Order: Med Small Large
Display Order: Large Med Small
106  130  1
155  114  1
106  199  1
115  217  1

出于某种原因,先发生反序列化会有点慢。为什么是这样? ServiceStack 是否可能在幕后缓存有关对象的信息?

大多数序列化程序都针对 运行 时间性能进行了优化,其中第一次迭代会由于创建和编译包含最佳代码路径的所有必需委托而变慢,然后将其缓存并重新用于后续序列化缓存类型的数量。