Performance of Html.RenderPartial

by Kenny Eliasson 27. October 2010 10:56

I was using Html.RenderPartial to loop out a Filestructure. (Parents have children that can have children ...).


<% foreach (var entry in Model) { %>
    <li>
        <span class="Clickable"><%=entry.Name%></span>
        <% if (entry.Children.Any())
                Html.RenderPartial("../FileEntry/Item.ascx", entry.Children); 
        %>
    </li>
    <% } %>
</ul>


This worked flawlessy on small data-amouns but when the tree contained over 500 entries the page took over 5-10 seconds to load.

My first thought was of course that the database query was slow, I fired up NHProf only to find out that the query took 50ms.

My memory then told me that I had read a blog post about the performance isnt all that good on Html.RenderPartial. I googled a bit but didnt really find a solution for it, so I tried to change the way I locate the view. I changed the Html.RenderPartial part of the code from

Html.RenderPartial("../FileEntry/Item.ascx", entry.Children);

to


Html.RenderPartial("~/Views/FileEntry/Item.ascx", entry.Children); 


and the page once again took under 1 second to load. Pretty impressive performance boost with a one-line change.

The culprit of the problem is probably when searching for the view on the current filesystem. It probably looks in the "Shared"-folders, dont find it, look somewhere else til it finds the correct one. Hitting the filesystem *is* expansive, especially over 500 times.

Categories: C# | MVC2

EditorTemplates and MVC2

by Kenny Eliasson 19. April 2010 15:10

Been upgrading our MVC-project at work to MVC2. A pleasant journey without much hassle. After upgrading I of course wanted to play around with the EditorTemplates.

I easily implemented my own EditorTemplates for Strings, Int32's, MultilineText, Bools and other "simple" types. I then realized that it would be sweet if all properties ending with "Color" would use my Color-template that included some Javascript for a color-picker. I remembered that i saw something like that in Tekpubs MVC-movies. What I needed to do was to create my own ModelMetadataProvider. Luckily enough yu can just inherit from the default DataAnnotationsModelMetadataProvider and override CreateMetaData().

My ModelMetadataProvider looked like this after my small tweak.

public class CustomMetaDataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(System.Collections.Generic.IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
        {
            var metaData = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
            if(propertyName != null) {
                if (propertyName.EndsWith("Color")) {
                    metaData.TemplateHint = "Color";
                }   
            }
            return metaData;
        }
    }

After that we tell our MVC-app to use our Provider in Global.asax with 1 line.

ModelMetadataProviders.Current = new CustomlMetaDataProvider();

And as easy as that I've implemented my own convention! :)

Tags: , ,
Categories: C# | MVC2 | Templates