Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.RenderPartial slow

Our web app contains tens of partial views, some of them are childs of others. We noticed app comes slow on the first load, it takes 0.5 to 1 second to initialize each view it calls for the first time. I traced timing and discovered that this:

Html.RenderPartial("~/Full/Relative/Path/To/View.cshtml", null);  

can take about 1 second even if the view is absolutely empty. In the same time this:

var view = ViewEngines.Engines.FindPartialView(
ViewContext.Controller.ControllerContext, "~/Full/Relative/Path/To/View.cshtml");

takes 1 millisecond, so no time is spent for finding file.
Questions:
1. Is that normal for the view compiler to take so long?
2. Is there other way to make the first call fast but not having the view precompiled with the directive in the csproj?

ps: the weird thing is that the same views loaded faster in the beginning of the app development.

like image 582
LINQ2Vodka Avatar asked Apr 21 '15 14:04

LINQ2Vodka


2 Answers

The delay you are seeing is purely down to the initial compile of the view on first use. The speed will be down to the server (processor and drive speed, memory, usage etc).

You either live with it, or set you project to pre-compile views to avoid the compile at runtime.

like image 151
Gone Coding Avatar answered Oct 29 '22 15:10

Gone Coding


Well, the TrueBlueAussie's answer is correct but i have something to add.
While compiling views (during application compile time or later, depending on the project configuration) the compiler creates *.dll's, one per each folder containing views. When the application is about to display a view for the first time, the whole DLL containing that view is loaded by the IIS server. You can easily discover it watching output window in the VS during that process, and this can take a second or even more. After the DLL has been loaded, all other views that are in the same folder are rendered quickly. Thus the more views folders you have the more often IIS loads them and spends time. Hope this helps.

like image 44
LINQ2Vodka Avatar answered Oct 29 '22 17:10

LINQ2Vodka