I did some per­for­mance opti­miza­tion work a lit­tle while back, and one of the changes which yielded a sig­nif­i­cant result was when I migrated some server side com­po­nents (which are CPU inten­sive and per­forms a large num­ber of loops) from using ADO.NET DataSets to using POCOs (plain old CLR object).

The loop­ing was then done using LINQ to Objects, and I dis­cov­ered a nice lit­tle exten­sion to LINQ called i4o — which stands for Index for Objects — to help make the loops faster. How­ever, I wasn’t able to observe any dif­fer­ence in per­for­mance, which con­tra­dicts with the find­ings on Aaron’s Tech­nol­ogy Mus­ing

Dig­ging a lit­tle deeper into the i4o source code (admit­tedly I didn’t do this myself, credit to Mike Barker for doing this!), it turns out that there are a num­ber of draw­backs in i4o which aren’t imme­di­ately obvi­ous or men­tioned any­where in the doc­u­men­ta­tion. The biggest prob­lem for us was that it only sup­ports equal­ity com­par­i­son, which means it would sim­ply ignore the index you have on the MatchID prop­erty if you try to run this query:

var result = from m in Matches where m.MatchID >= 1 select m;

but it’ll use the index on MatchID if you run this query instead:

var result = from m in Matches where m.MatchID == 1 select m;

The con­clu­sion?

i4o is an awe­some tool that can turbo boost your LINQ query, but ONLY put indices on prop­er­ties which you will be doing equal­ity com­par­i­son in your queries oth­er­wise you’ll just be wast­ing some mem­ory space hold­ing indices which would be used at all.

Share

I saw this arti­cle on D. Patrick Caldwell’s blog a lit­tle while back:

http://dpatrickcaldwell.blogspot.com/2009/03/validate-parameters-using-attributes.html

It was this arti­cle that got me inter­ested in Post­Sharp and the pos­si­bil­i­ties that it can bring. Post­Sharp, in short, is a light­weight frame­work which intro­duces some Aspect-Oriented Pro­gram­ming into .Net.

Some of the com­mon usages I have seen include trac­ing and the ‘mem­o­rizer’ (again, from D. Patrick Caldwell’s blog) being one of the more inter­est­ing. There is also a blog entry over at Richard’s Brain­dump which high­lights how you can use Post­Sharp to imple­ment the INo­ti­fyProp­er­ty­Changed interface.

One thing I’d like to point out though, is that the para­me­ter val­i­da­tion tech­nique in D. Patrick Caldwell’s blog entry above should be used with care and you should avoid apply­ing the [Check­Pa­ra­me­ters] attribute at class/assembly level as it does carry some per­for­mance hits. After play­ing around and exper­i­ment­ing with it for a lit­tle while, I have set­tled on apply­ing the [Check­Pa­ra­me­ters] attribute only on meth­ods whose para­me­ters require validation.

In my line of work, we have a lot of prob­lems with dead­locks in the Data­Base due to the num­ber of dif­fer­ent appli­ca­tions using the same Tables in the Data­Base and the dif­fer­ent way they use these tables (some uses nolock, oth­ers don’t). As a result, there are a lot of boil­er­plate code in the DAL classes which catches SqlEx­cep­tions and in case of dead­locks or con­nec­tion time­outs retry up to x num­ber times. This, of course, is a cross-cutting con­cern, and by employ­ing Post­Sharp I am able to deal with them with a sim­ple attribute like the one below instead of hun­dreds and hun­dreds lines of code.

[Serializable]
[AttributeUsage(AttributeTargets.Method)]
public class RetryOnSqlDeadLockOrConnectionTimeOutExceptionAttribute : OnMethodInvocationAspect
{
     [NonSerialized]
     private int CurrentAttempt;

     public override void OnInvocation(MethodInvocationEventArgs eventArgs)
     {
          CurrentAttempt++;

          try
          {
               eventArgs.Proceed();
          }
          catch (SqlException sqlException)
          {
               if (sqlException.Number == -2 || sqlException.Number == 1205)
               {
                    // put retry logic here
               }
               else
                    throw;
          }
     }
}

and to use it:

[RetryOnSqlDeadLockOrConnectionTimeOutException]
public void SomeDataBaseBoundOperationWhichNeedsRetryOnDataBaseDeadLock()
{
     ...
}
Share

Wel­come to my blog! It’s Xmas and with so much time on my hands, I thought this might be as good a time to start a blog as any!

What am I gonna blog? Well, imag­ine this, you’re going through your favorite blogs/websites and you read some­thing cool, but when it comes to a time you can actu­ally apply it in your line of work you can’t remem­ber where you read it in the first place!

This has hap­pened to me so much I had started to sim­ply book­mark every­thing I come across which I think might be use­ful later on, but that results in a rather long and dis­or­ga­nized list of book­marks which doesn’t really make life any eas­ier… so I’ve been mean­ing to start blog­ging things I learn (in a way that I can under­stand of course) and hope­fully it’ll help oth­ers who are in a sim­i­lar posi­tion as me.

Share