Introduction
With increasing business demands, ASP.NET projects become more and more complex and 'large' resulting in higher project build and load times. While buying a faster processor, better hardware and increasing RAM sizes are always options to consider to improve performance. Secondly, making architectural changes in ASP.NET project like separating project logically, minimizing number of assemblies and creating separate solutions can also be considered as long term strategies for the same. However these are not preferred approaches, if one wants to see the improvement instantly. To overcome such limitations, there are number of tips which can be used to quickly improve the application build times and thereby improving development productivity many folds.
Background
Personally, I felt that when an application starts taking more than a minute to build, then it's time to sit down and think about making appropriate adjustments so that the application build time remains in acceptable limits. Broadly, the improvement factors can be categorized as below:
- Optimizing Visual Studio settings
- Overall Project architecture changes
- Use of improved hardware like processor, hard disk, RAM, etc.
For the purpose of this tip, we will mainly focus on #1, Optimizing Visual Studio settings, while touching briefly on #2, Project architectural changes.
Improvement Tips
I will start with making configuration changes in Visual Studio first and then will touch some project level settings followed by easy architectural changes and finally with some other options which are worth mentioning.
1. Use Parallel Builds
This is a magical setting which builds project in your solutions in parallel. Although, there could be project dependencies set in your project which will allow some project to build in particular order, still this setting would automatically deduce the parallelism among the projects during build. Set this value to an optimum level for your machine, e.g., for i7 processor which can execute up to 8 threads, you can set this setting to 8. This can be set in Visual Studio by Tools -> Options -> Projects and Solutions -> Build and Run. Refer below:
2. Only Build Startup and Dependencies on Run
While being on the "Build and Run" settings mentioned in the previous step, check "Only build startup and dependencies on Run". This could be especially helpful when there are a large number of projects and you would like to build and run startup project only without building all the projects in solution even if they are not dependent and further those unrelated project could include pre or post build tasks. Refer Build the currently selected project.
3. Set Build Verbosity
You can set the build verbosity based on your choice. However, updating this setting would also decrease the build overhead and reduces the build time. Again referring to the same screenshot provided in tip #1, set the MSBuild project build output verbosity to "Minimal
". This will show minimum build detail during build in build output window. You can even set it to quiet if you need not see output or occasionally you may set it to detailed or even diagnostics to troubleshoot issues in build depending upon the requirement.
4. PreCompiled MVC Razor Views
By default, MVC views are compiled at runtime. We can build MVC views during MVC project build, using MVCBuildViews
as true
. However, MVCBuildViews
comes with performance penalty so pre-compiling MVC views is the most preferred approach to manage MVC view effectively. You can refer to Precompile your MVC Razor views using Razor Generator to try it yourself.
5. Build Required Projects Only
There could be certain projects in your solution such as setup projects or projects which are not directly required to execute your start up project and can be excluded from build. It would be a good idea to exclude them from build. To exclude a project; on the menu bar, choose Build, Configuration Manager. In the Project contexts table, locate the project you want to exclude from the build. In the Build column for the project, clear the check box. Refer to the screenshot below:
6. Disable On-Access Virus scanner
This seems unrelated to Visual Studio build time, however on-access scanner scans file during file operations. Thus, it a good idea to investigate virus scanner and exclude development folders including .NET framework temporary files and folders.
7. Mystical Project Settings
There are some project settings which could also improve the build time of your Visual Studio project. It is not mandatory to use these properties as you may face inconsistency while setting these on your project; project may fail to build. However, these settings can have a huge impact on performance in the development system.
Set optimizeCompilations to true
<Compilation debug="true" targetFramework="4.5" optimizeCompilations="true"/>
This flag is used to optimize ASP.NET compilation behavior and it caters to development environment. The setting detects minor updates done during development like update in a single method, addition of new method, rename or deletion of APIs, etc. which do not require full recompile. Thus, only a specific portion of project is compiled. Refer to A new flag to optimize ASP.NET compilation behavior for more details.
Set MVCBuildViews to false
As the name suggest, this setting is used for building MVC views in MVC based ASP.NET project. If you are not using pre-compiled views, then it is important to note that during development MVCBuildview
takes around 80% of application build time in MVC application. This setting must be set to true
if any MVC view is updated, added so that updates are reflected after compilation.
However, in case there are no changes in views, this setting can be set to false
, which drastically reduces the build time. As a workaround, you can set MVCBuildView false
for Debug configuration and true
for Release configuration. In case view debugging is required, then this setting can optionally be set to true
manually for the time being.
<MvcBuildViews>false</MvcBuildViews>
To set its value, open your MVC project's csproj file in Text editor of your choice and manually edit the property in YourProjecct.csproj file.
Set Build Verbosity to Diagnostic
As per point #3, setting minimal build verbosity also has a impact on build. Setting Build output verbosity to diagnostic will let you know the details of individual operations being called during the build process. You can better a finer view and better control over minute activities during build and can fine tune based on your needs. Refer to the screen shot below to see a small snippet for the same.
Build Solution from Command Line
On a side note, if you build your solution from command line, you may see improved build time than using Visual Studio IDE as it does not have fancy UI and you can use a variety of Command Line options:
ms build overvaluations /verbosity:diagnostic
8. Set Common Output Directory
This technique can also be handy in making sure that there are no duplicate assemblies and content created during build. It can provide significant improvement on build time as same content will not be copied to local project folders. Refer to the screenshot below to set output directory for your build output.
An alternative technique, "Copy Local" to false can also be used to ensure that assemblies are not copied locally. However, this technique should be used diligently to ensure that it does not break the build itself. For more details, refer to Setting Copy Local to true.
Note: It is also worth mentioning to use RAM disk as output folder instead of hard drive. However, I have not tried this setting though.
9. Remove Unused Files and Dead Code from your Projects
Let us explore some other aspects on our code structure which can be improved for better performance. Firstly, remove any unused files from your projects; classes which are not used, files which are included in project but never referenced. Unnecessary code acts as a technical debt and bring down performance of almost every application.
Removing dead code can also be used to make your code size smaller and manageable. It should also be practiced for the long run if not for instant performance optimization. There are many Static Code Analysis tools available across many platforms to achieve clean code. You can also use Visual Studio built-in code analysis tool to diagnose code related issues.
10. Keep Optimum Number of Projects and Assemblies
This is rather an architectural concern, however it fits well to improve build time. More the number of projects and assemblies, more time it takes Compiler to perform the compilation. There is no such exact number which can be considered good, however based on the complexity of project and business requirement, the number of project and assemblies can vary. Keeping this to optimum level results in quick application build time and run time performance even though it is time-consuming and painful process to change the project architecture and setup.
Summary
There can be many aspects to improve build time of a particular application and it depends on many factors. The performance improvement cannot be done overnight and should be an on-going process. I have not provided exact benchmarking results as results can vary greatly depend on your projects.
I hope this gives you some ideas to improve your project compilation time and I know that there could be many other improvement points left which could be part of this tip.
If you have any such tips and tricks, please share!
Related Readings
History
First version of tips to improve Visual Studio Build performance.