Thursday, June 25, 2009

 

Chunked transfer and Response.Flush

I've been attending O'Reilly's Velocity Conference over the past days and I must say that I've learned quite a few things about web performance that I didn't know. One subject that came up in several talks was using "Transfer-Encoding: Chunked" to allow for sending to topmost part of the page to the client while most of the work for the page hasn't even started. Example:

If you look at this result page from Google, you'll note that the upper section of the page, with links to other Google properties, the logged-in indicator, the logo and the search box can be generated without doing the actual search. Most of the time on the server is spent looking for results matching the query, but you could create this first bit of HTML before even starting on the query. This is exactly what Google does. When your request comes in, the first send this header and only after that they start on the search query. This makes the time before the user sees any change a lot shorter and will be perceived as better performance. What's more, as the browser gets the references to external images and scripts sooner, it can start loading them earlier, resulting in a real (not just perceived) better performance.

So how could we achieve this in ASP.NET? Problem is of course that we normally do any heavy lifting required in a page during the PreRender or EventRaising phases. The actual rendering of the page, including the very first segment, happens later: during the Render phase. It depends very much on the structure of your page, but in certain situations, you could maybe do something like this: Now if you wrap the start of the page inside this control, you will render that part of your page way before the Render phase. The flushing makessure that the automatic buffering is bypassed. ASP.NET will then automatically set Transfer-Encoding: chunked.

Drawbacks:

  • You must make sure that no control or literal text comes before the Flusher or the order will get mixed up
  • Not sure how this will work with master pages
  • You cannot flush part of the HtmlForm control (this contains the hidden ViewState). Keep everything you may want to flush outside the form
  • Anyone with deeper thoughts around?

    Labels:


    This page is powered by Blogger. Isn't yours?