Performance issue and the solution on FindSiteMapNode( HttpContext context )

Feb 1, 2010 at 9:29 PM

I am using this mvc sitemap in one of my projects and found a big performance issue in the function "public override SiteMapNode FindSiteMapNode( HttpContext context )" which is called by CurrentNode and it calls a even slower function node = FindControllerActionNode( this.RootNode, routeData.Values );

I did not get chance to fix FindControllerActionNode( this.RootNode, routeData.Values ) but I found public override SiteMapNode FindSiteMapNode(string rawUrl) is hundreds times faster. Below is the performance data.

Before change:

One call to SiteMapPath() invokes 1285 + 970 calls to GetChildNode() which takes 1356ms.

After change:

It takes 5ms for one call to SiteMapPath() which has no call  to GetChildNode(). It improves 250 times. Thinking of that every page needs to call SiteMapPath().



This change was tested only for my MVC1 application.  I hope this can give you the idea.


-- Changes

The changes is simple in the function to replace the call to FindControllerActionNode( this.RootNode, routeData.Values ); with two lines of code.


                routeValues.Add( "area", routeData.DataTokens[ "area" ] ?? string.Empty );
                System.Web.Routing.VirtualPathData vpd = System.Web.Routing.RouteTable.Routes.GetVirtualPath(
                  routeData.Values );
                string rawUrl = vpd.VirtualPath;
                node = SiteMap.Provider.FindSiteMapNode( rawUrl ) as MvcSiteMapNode;
                //node = FindControllerActionNode( this.RootNode, routeData.Values );



                routeValues.Add( "area", routeData.DataTokens[ "area" ] ?? string.Empty );

		//below two lines are added

                System.Web.Routing.VirtualPathData vpd = System.Web.Routing.RouteTable.Routes.GetVirtualPath(null, routeData.Values );

                node = SiteMap.Provider.FindSiteMapNode( vpd.VirtualPath ) as MvcSiteMapNode;

                //node = FindControllerActionNode( this.RootNode, routeData.Values );






Feb 1, 2010 at 10:05 PM

Good grief this change makes a difference.  I was beginning to get worried about how slow MvcSiteMap was behaving.  Nice catch!  I will be checking to see your fix on the FindControllerActionNode() function.

Feb 2, 2010 at 9:48 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.