Monday, 30 November 2015

ASP.NET Tips #52 - NGen your EntityFramework.dll to improve startup performance

From Entity Framework version 6, core assemblies of Entity Framework are not part of the .NET framework and so a native image is not generated automatically. This despite the fact that a native image can improve memory when the assembly is shared among multiple processes.

Generating a native image of EntityFramework.dll and EntityFramework.SqlServer.dll will not only avoid multiple JIT compilations, but also improve the startup process significantly.

Friday, 27 November 2015

ASP.NET Tips #51 - Don’t use interop (WinFormsHost) with WPF when display performance is critical

When creating a Win32 window in particular, the display performance will fall by a few hundred milliseconds if there are several interop controls involved.

Thursday, 26 November 2015

ASP.NET Tips #50 - Consolidate your images with sprites

If you have a standard collection of images with the same height and width (like social icons), use a sprite generator to consolidate the images into one file, and then use CSS to position the separate images on your website. Instead of multiple browser requests, you then make one request for the consolidated image, and the CSS positions the images for you.

Tuesday, 24 November 2015

ASP.NET Tips #49 - Flush HTML responses early

By flushing HTML responses, the browser gets a jump start on downloading critical resources. It’s also pretty easy to do in ASP.NET.

// Buffer response so that page is sent
// after processing is complete.
Response.BufferOutput = true; 
...
// Send the output to the client.
Response.Flush();

Monday, 16 November 2015

ASP.NET Tips #48 - Move to ASP.NET MVC

With the new release of Visual Studio 2015 and ASP.NET 5.0, there is no better time to move to ASP.NET MVC. With WebForm's ViewState taking up a hefty amount of space on the return trip to the browser (and possibly a mobile device), it could take a long time to render the content to the page. Particularly when web pages are now becoming larger and larger.

ASP.NET MVC makes your HTML more granular, returns a smaller HTML footprint to the browser, and provides a better separation of concerns for your development.

Read more about moving from ASP.NET Web Forms to MVC

Thursday, 12 November 2015

ASP.NET Tips #47 - Consider front-end performance issues as well as back-end performance issues

Many applications written today are web applications, so it is important to consider front-end performance issues as well as back-end performance issues. While the processing of a web page on the web server may only take a few hundred milliseconds, for example, it can take several seconds for the browser to load and render the page to the user.

Tools like Google PageSpeed Insights and Yahoo YSlow can analyze your pages for adherence to known front-end performance best practices. You not only get an overall score for your page, but a prioritized list of issues so you know exactly what items need to be addressed. Given how easy these tools are to use, every web developer should install them in their browser and analyze their pages on a regular basis.

Thursday, 5 November 2015

ASP.NET Tips #46 - Eagerly create common object types to prevent constant evaluation

Many of us use code frequently in applications. For example:

if (myobject.GetType() == typeof(bool)) 
{ 
  // Do something 
}

-- Or --

if (myobject.GetType() == typeof()(MyCustomObject))
{ 
  // Do something 
}

Instead, consider eagerly creating these type of instances:

public static class CommonTypes
{
  public static readonly Type TypeOfBoolean = typeof(bool);
  public static readonly Type TypeOfString = typeof(string);
}

You can then do the following:

if (arg.GetType() == CommonTypes.TypeOfBoolean)
{
  // Do something
}

Or:

if (arg.GetType() == CommonTypes.TypeOfString)
{
  // Do something
}

And save the cost of performing the typeof(bool) or typeof(string) repeatedly in your code.

Wednesday, 4 November 2015

ASP.NET Tips #45 - Remember that type casting and conversions incur performance penalties

If you must perform type casting and conversions, try doing as little of them as possible. For example, rather than ...

if (obj is SomeType) then { ((SomeType)obj) ……. };

… which causes two casts (one when performing the 'if' evaluation and one in the body of the If statement), use this:

if (obj is SomeType) then { ((SomeType)obj) ……. };

That way, the cast is only done once with the 'as' parameter. If it fails, the value is NULL. If not, you can go ahead and use it, resulting in only one cast.

For more information, see: https://msdn.microsoft.com/en-us/library/ms173105.aspx

Monday, 2 November 2015

ASP.NET Tips #44 - Take advantage of spans

Spans – regions of code to explicitly demarcate on a timeline graph – are a great feature in Concurrency Visualizer. There are two big things to watch out for.

Firstly, when instrumenting spans, the documentation suggests you use:

var span = Markers.EnterSpan(description);
// do stuff here
span.Leave();

That pattern is more error prone than it needs to be – you must manually ensure and remember to "close" your span through all code pathways.

Secondly, you can enhance their disposable nature further with the using statement:

using (Markers.EnterSpan(description))
{
   // do stuff here
}