Feed on
Posts
Comments

PLINQ to Sitecore

A lot of exiting weeks have passed the way. The introduction of VS2008 on MSDN, Silverlight 1.1 is renamed to 2.0 and… PLINQ appeared! Last friday, Runi spent some time on showing what LINQ can do for you in a Sitecore environment. As it is Runi’s second post in 6 months, so I don’t expect him to do some log-series. I won’t do that as well, but when possible, I definitely try to show the advantage of using LINQ with Sitecore.

But uhm, he Alex, a second ago you were talking about PLINQ and now about LINQ again? Indeed, today’s topic is PLINQ. That’s an extremely cool new library written by Microsoft with the name ‘Parallel Language Integrated Query’. This means that your queries will be running against all your cores and resources available in your machine!
Have you ever mentioned that you aren’t really using that second core of the so cool Duo core machine? Well, I can promise you, you aren’t! I’ve written the following test:

    public partial class Sample_layout : System.Web.UI.Page
    {
        protected Item CurrentItem
        {
            get { return Sitecore.Context.Item; }
        }
        protected IEnumerable<Item> Children
        {
            get { return CurrentItem.Children.Cast<Item>(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<hr>");
            ClearCache();
            PerformWorkSequential();
            ClearCache();
            PerformWorkParallel();
            Response.Write("<hr>");
        }

        private void ClearCache()
        {
            Sitecore.Caching.CacheManager.ClearAllCaches();
        }

        private void PerformWorkParallel()
        {
            using(new SectionTimer("Parallel running trough items", true))
            {
                var res = from i in Children.AsParallel<Item>()
                          where i.Appearance.Sortorder > 10
                          orderby i.DisplayName
                          select i;

                Parallel.ForEach<Item>(res, delegate(Item i) { Response.Write(i.DisplayName + "<br />"); });
            }
        }

        private void PerformWorkSequential()
        {
            using (new SectionTimer("Sequential running trough items", true))
            {
                var res = from i in Children
                          where i.Appearance.Sortorder > 10
                          orderby i.DisplayName
                          select i;

                foreach (Item item in res)
                {
                    Response.Write(item.DisplayName + "<br />");
                }
            }
        }

What does it do? It makes sure that the cache is cleared and then it runs trough the children of the current item, select the ones which have a Sortorder which is greater then 10 and write the output the the Response-buffer.

The result is amazing. In all cases, the Parallel query is executed faster. Here’s a short list of the result in ms I’ve seen:

Sequential Parallel
960.3 * 40.5
57.1 32.3
49.3 35.8
55.1 24.6
53.1 47.1
47.7 28.4
49.2 33.1
52.0 33.9

* = First run, the underlaying dataproviders also had to initialize so this result might not be very useful.

To make sure that the results are not influenced by the order in which way the data is called, I’ve done the same with the parallel first:

Parallel Sequential
290* 59.7
32.1 48.6
32.5 51.5
33.4 53.5
30.3 57.2
31.7 53.9
30.5 51.6
33.0 51.4

Overall, minus the highest and the lowest score, the Parallel processing is done 20ms faster then the sequential(33ms against 53ms). An impressive result, don’t you think? Even more when you look at the initialization results and when you keep in mind that I’m working on a simplistic developer notebook. I expect the results on a quad core server with a bunch of data to be even more significant.

Well, that’s nearly it for now. This small test took me more time then expected.

For additional info about PLINQ, visit the Parallel computing center at MSDN. Some other resources:

  • Parallel Extensions to the .NET Framework Team Blog
  • MSDN Forum for Parallel Extensions to the .NET Framework
  • Parallel Extensions to the .NET Framework Connect site
  • And for those who are really interested the how and why’s about concurrency. Read ‘the free lunch is over’ by Microsoft’s C++ Architect, Herb Stutter. Also mention these two blogpost when you’re interested in the CTP as it describes the issues with the current CTP of PLINQ.

    13 Responses to “PLINQ to Sitecore”

    1. Lars Nielsen says:

      This is cool, – and very interesting.

    2. Runi Thomsen says:

      Nice – I cant wait for the day where we get a released version. Good to get some real life numbers on the performance improvements.

    3. Kim Hornung says:

      Cool!
      But I wonder: How much is the performance gain/impact of using LINQ compared to using the old-fashioned Sitecore API (Item.Children and an if statement)?
      Not that I find the old-fashioned way very elegant, but it would be interesting to see how much faster PLINQ is?

    4. Hi Kim,

      I’ll create similar benchmarks for the old fashioned ways(Sitecore Query and Item.Children with an If) later today.

      - Alex

    5. Martin Knudsen says:

      Sick stuff! Great… A whole new world of Performance is opening up thanks to Multi-core and the guys that create PLINQ. Good work describing it Alex.

    6. Dmitry Kostenko says:

      Hmm… Not sure it will help real-world web server environments. Each time you claim another CPU/core for your task, you steal one from somebody’s request.

    7. I just love your weblog! Very nice post! Still you can do many things to improve it.

    8. Cruser says:

      Fantastic post, very much useful information. I found your blog a few days ago on Technorati and have been reading it over the past few days. I am goint to subscribe your blog.

    9. I havent any word to appreciate this post…..Really i am impressed from this post….the person who create this post it was a great human..thanks for shared this with us.

    10. propanetanks says:

      I want to thank for the time you have contributed in writing this post. I am hoping the same best blogpost from you in the upcoming as well. In fact your creative writing abilities has inspired me to begin my own blog now. Truly the blogging is spreading its wings quickly. Your write up is a good model of it.

    11. I’m pleased I discovered this weblog, I couldnt discover any knowledge on this subject prior to. I also manage a site and if you want to ever interested in a little bit of visitor writing for me please feel free to let me know, im always look for people to check out my website. Please stop by and leave a comment sometime!

    12. forex says:

      beautiful site

    Leave a Reply