Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid VBCSCompiler perf hit on Roslyn powered ASP.NET Razor MVC views?

In order to support C# 6 in our Razor views on MVC5, we turned on the Roslyn compiler platform via web.config:

<system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
    </compilers>
</system.codedom>

However, after production deployments, each view/controller appears to have a noticeable "First Load" delay that is worse than without this compiler being enabled.

Importantly, this delay is in addition to the regular JIT delay you get from a new site being deployed. Pages are noticably slower, while it appears VBCSCompiler.exe runs in the background to "further compile" these pages.

Is there a best practice for pre-compiling/optimizing this situation to eliminate the first-load runtime delay post deployment? Ideally VBCSCompiler.exe is not running after a deployment occurs, and is performed at build-time.

I've seen mentions of aspnet_compiler.exe and have come across StackExchange.Precompilation (see https://blog.stackoverflow.com/2015/07/announcing-stackexchange-precompilation/) and wonder if this is the right fix.

Does anyone have any experience with this particular problem? Thank you.

like image 970
Andrew Avatar asked Oct 11 '16 11:10

Andrew


People also ask

Can I delete the Roslyn folder?

NET Compiler Platform, also known by its nickname Roslyn, is a set of open-source compilers and code analysis APIs for C# and Visual Basic languages from Microsoft. Deleting this cache folder will not affect Visual Studio, but it may affect the loading speed of .

Why is there a Roslyn folder in bin?

@Dmitry The job of the csc.exe in /bin/Roslyn is to invoke the VBCSCompiler.exe , which sits in the same folder. VBCSCompiler.exe is the process that does the actual compilation work.


1 Answers

You can use the RoslynRazorViewEngine from StackExchange.Precompilation:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RoslynRazorViewEngine());

The main goal of this view engine, however, is not to get rid of the startup perf hit. With it you just get C#6 support. The views still have to be compiled on first load, but the roslyn assemblies end up in the app domain afterwards, and you get a higher memory footprint. Since roslyn is called in the app, you don't need special permissions on the web server for executing another .exe from the /bin folder. We mostly use this for development on our dev machines.

I strongly recommend you just precompile all the views. This way you get no startup perf hit, and you gain compile time verification of your views. You could even run analyzers on the generated view code. That's what StackOverflow currently runs in production.

I mention aspnet_compiler.exe in that blog post, because it's the original tool for precompilation on ASP.NET (sans MVC). Unfortunatelly it's very slow.

like image 173
m0sa Avatar answered Sep 27 '22 20:09

m0sa