Improving Performance of ASP.Net Core Applications
Speed is a key feature of ASP.Net framework so you need to make sure you maximize its performance to get the best out of your ASP.Net applications. The framework runs on Windows, Linux and Mac and allows you to build fast applications that consume minimal resources. Here are some tips to make sure you get the best out of your ASP.Net core apps.
Making sure that all your statements are inlined by the Just-In-Time (JOT) compiler can vastly increase performance. Throw statements can slow things down so using a static helper can help keep things moving fast by containing them.
Less Virtual Calls
Minimize the amount of calls to virtual members as these use resources. If you can de-virtualize the calls your JIT compiler can then inline the calls. Follow these 2 basic steps to help get rid of unnecessary virtual calls:
- Seal overridden methods and mark classes and methods as sealed (by default).
- Use concrete types not interfaces
Pool HTTP Connections
Use Http Client Factory. This was introduced in ASP.Net Core 2.1 to help you pool HTTP connections thereby increasing performance and reliability.
New types have been introduced which can be leveraged to increase performance for example, the introduction of SystemValueTuple. Taking advantage of these will avoid data copying and array allocations and improve the core performance.
Make sure any stale data is cached immediately. There is middleware supported by ASP.Net Core to assist with this. Ncache is something that helps cache stale date which is fast acting and easy to set up.
A smaller response improves the apps performance by reducing the amount of data transfer. ASP.Net Core supports response compressing as a middleware component allowing you to add this to the pipeline easily.
Reduce HTTP Requests
A good way of optimizing performance is to reduce the number of number of HTTP requests or by using HTTP/2. This combined with caching and reducing redirects can have a good impact on performance.
Avoid Blocking Calls
Avoid blocking calls using Task.Wait or Task.Result — just make asynchronous code paths and utilize PerfView or similar performance profile programs.
Lessen Large Object Allocations
Although .Net Core’s own program can release memory doing so uses CPU time, ArrayPool is the better option. This will cache frequently used objects rather than focus time and space freeing up large objects.
Use Exceptions Sparingly
Take care to avoid unnecessary exceptions. Using them only when needed will up performance.
Optimize Data Access
Data access APIs should be accessed asynchronously, avoiding multiple data collection trips and only pick up the necessary data. Data access is one of the key factors that slow an application.
Tuning a web application to enhance performance is complex but there are tools and techniques in place to help. MiniProfile, Application Insights, Stackify Retrace and MiniProfiler are application performance measuring tools to look out for. These can look at the issues and determine how to easily fix them, making the process easier for the developer.