heweitykc / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
0 stars 0 forks source link

Performance problem when serializing references (suggested patch included) #215

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?
1. Run the following example (it's nonesense but it demonstrates the problem)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using ProtoBuf;
using System.IO;
using System.Collections;

namespace ConsoleApplication1
{
    [ProtoContract]
    class Person
    {
        [ProtoMember(1, AsReference=true)]
        public List<Adress> adresses;
    }

    [ProtoContract]
    class Adress
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.adresses = new List<Adress>();
            for (int i = 0; i < 50000; i++) p.adresses.Add(new Adress());

            var s = new FileStream(@"c:\temp\test.proto", FileMode.Create);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Serializer.Serialize(s, p);
            sw.Stop();
            s.Close();
            s.Dispose();
            Console.WriteLine("Done in" + sw.Elapsed);
            Console.ReadKey();
        }
    }
}

What is the expected output? What do you see instead?
There is a huge speed difference when running with AsReference=true

What version of the product are you using? On what operating system?
v2 r437 on windows 7

Please provide any additional information below.
BasicList.IndexOfReference was taking more than 90% of running time.
see attached patch for a proposed solution, I did not test it a lot, it's just 
a quick fix so that I can keep on working...

I can be reached at alexandre.mainville at ibwave dot com

This is a wonderful piece of code, thanks a lot for sharing!

Original issue reported on code.google.com by alexandr...@ibwave.com on 4 Aug 2011 at 8:46

Attachments:

GoogleCodeExporter commented 9 years ago
I haven't looked at the patch yet (on mobile), but I expect it refers to 
RuntimeHelpers.GeHashCode(object) - if so this is already on my list of things 
to throw in. I'll review when I get chance. If the patch is unrelated, then 
sorry in advance :)

Marc

Original comment by marc.gravell on 4 Aug 2011 at 9:55