Compiled MVC views still display slowly on first request

I have an MVC 5.x application that has its views precompiled and combined into one assembly. Even after all this, the first view request after running AppDomain is slow, it does not matter which view is requested, since they are all slow when the view is first requested. Digging into MiniProfiler, I see that this is the "Render" time for the view, which takes up most of the time, and after additional viewing requests, the Render time is reduced by about 90%. Therefore, to be clear, it is this first render that takes time. I did not expect to see this slight slowness due to the fact that all the views were precompiled, and I do not think it is a JIT that causes a strong slowdown ... who has any guesses or ideas?

Please note that this is not the first request of a web application, this is the first request of a certain type / page. For example, for the test below, the application worked for several hours and constantly accepted requests, but the test captured the first request for this view.

First request

Capture of MiniProfiler

Second request

enter image description here

+5
source share
2 answers

Weird! To be 100% sure what exactly is happening, I would do the following:

  • I redirect compilation at runtime (yes, at least Global.asax will need to be compiled) as follows:

    <system.web> <compilation debug="true" targetFramework="4.5" tempDirectory="c:\temp\asp.net" /> ... </system.web> 
  • I will examine the contents of the folder before and after the first run. Perhaps it would be even better to have a copy of the original.

From your description of the problem, it seems that everything should be fine and there will be no difference between runs. Yet comparing the contents of the folder before and after may reveal some difference.

Another reason might be some caching, but I can guess that you already figured it out.

0
source

I struggled with the same problem for several months: this happened to me in only a few looks.

It seems that rendering the views is not as simple as I thought, since MVC needs to do a couple of operations to determine the exact .cshtml to use for the action, its partial views, if you have any, etc. etc.

What work for me was to use the Razor Generator , which processes Razor files during development instead of runtime . (if you want to take a picture, I suggest you try it first so that you know that it takes time to load the first time you try).

From the website:

One reason for this is to avoid any harm at runtime when your site starts up, since there is nothing to compile at runtime. This can be significant on sites with many kinds.

The disadvantage of implementing Razor Generator is that you no longer need .cshtml files: therefore, if you want to change the view, you need to recompile the project. But this is not a problem, at least not for me.

First install the razor generator from the VS extension gallery (as described on the website), and then the nuget package.

Trust me: you'll be so glad you cry ☺

0
source

Source: https://habr.com/ru/post/1201695/


All Articles