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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With