locked
snapshot based changing tracking faster then notification based for smaller model. RRS feed

  • Question

  • i find that snapshot based change tracking for pure poco model is faster then poco that uses change tracking proxy.

    public class Account
    {
        public virtual int AccountId { get; set; }
        public virtual string Name { get; set; }
        public virtual decimal Balance { get; set; }
        public virtual ICollection<Payment> Payments { get; set; }

    }
    public class Payment
    {
        public virtual int PaymentId { get; set; }
        public virtual string PaidTo { get; set; }
        public virtual decimal Paid { get; set; }
        public virtual int AccountId { get; set; }
    }

    //inserting
    var watch = new Stopwatch();
    watch.Start();
    using (var db = new POCOContext())
    {
        for (int i = 0; i < 5000; i++)
        {
            var account = db.CreateObject<Account>();
            account.Name = "test";
            account.Balance = 5000;
            account.Payments.Add(new Payment { PaidTo = "test", Paid = 5 });
            db.Accounts.AddObject(account);
        }
        db.SaveChanges();
    }
    watch.Stop();
    Console.WriteLine("Total Time for Insert:{0}",watch.Elapsed.TotalSeconds);

    //reading proxy objects
    using (var db = new POCOContext())
    {
        watch.Restart();
        var accounts = db.Accounts.Include("Payments").ToList();
        watch.Stop();
        Console.WriteLine("Time to read proxy objects:{0}",watch.Elapsed.TotalSeconds);

        //updating proxy objects.
        watch.Restart();
        foreach (var account in accounts)
        {
            account.Balance += 10;
            account.Payments.First().Paid += 1;
        }
        db.SaveChanges();
        watch.Stop();
        Console.WriteLine("Updating objects took:{0}",watch.Elapsed.TotalSeconds);
    }

    Now to run the same example with pure poco, i removed all the virtuals and change creation of account to as follows.
    var account = new Account { Payments = new HashSet<Payment>() };

    these are some of the averages i get.

    change tracking proxy.

     

    Insert

    Read

    Update

    4.28

    .507

    3.13

    4.27

    .506

    2.82

    4.26

    .506

    2.71

    4.30

    .501

    2.75

    4.14

    .507

    3.19

    4.23

    .515

    2.76

    4.26

    .508

    3.07

    Average

    4.24

    .507

    2.91



    Pure POCO

     

    Insert

    Read

    Update

    4.15

    .447

    2.86

    4.25

    .400

    2.81

    4.17

    .394

    2.90

    4.28

    .404

    2.87

    4.12

    .407

    2.90

    4.10

    .390

    3.07

    4.10

    .402

    2.82

    Average

    4.16

    .447

    2.89


    It looks like insert, and reads are definately slower but updates are pretty much same. that's with inserting 5000 records with a single payment. I would think snap gets expensive when the model is complicated with lots of associations

    Zeeshan Hirani

    Wednesday, November 4, 2009 7:12 PM

Answers

  • Hi Zeeshan,

    The performance characterstics are really model dependent and you are correct that as model complexity grows, so too does the cost of various operations. For the pure-POCO snapshot based change tracking, we typically do see that this is slower for updates than using EntityObject based entities or change tracking proxies. The cost comes from the DetectChanges method that is called during SaveChanges to sync up changes made to the entities with the ObjectStateManager. The more entities you have in your application and the more complicated the model, the more expensive DetectChanges can become. On the flip side, when doing pure inserts, there is a cost in creating the change tracking proxy (mainly from initializing all of the EntityCollections), but that cost usually comes out in the wash of mixed operations. There is also the memory footprint to consider. With snapshots, you have a snapshot of the entity (so you have memory overhead always). With change tracking proxies you don't have this snapshot.

    Jeff
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, January 19, 2010 5:23 AM
    Moderator