Yes, really. It's got to be the best name for an open source library out there. It's a great double entendre and a great name for this useful little library. Perhaps English isn't your first language, so I'll just say that a courtesy flush gives the next person a fresh bowl. ;)
However, in the computer world "flushing a buffer" means forcing a buffer to be moved along, usually to a file or the network. Rather than holding data, you flush it, and move it along.
Nik from Glimpse has a small NuGet package called Courtesy Flush. He's got a good write-up on his blog.
It's a library to enable easier flushing of your buffer in ASP.NET MVC. From their site:
Why Flush Early?
Flushing early can provide performance improvements in web applications and has been a recomended best practice in the web performance community since 2007.
To find out more, check out my blog where I covered the benefits of flushing early in two posts:
It builds on top of ASP.NET ActionFilters, which you can apply as attributes to your methods, or call within controllers.
Let's say that you have some server-side work that's very slow. That slow operation could hold up the rendering of your page until it completes. With a pre-flush like this you can get bytes onto the network and into the user's browser faster.
Here we render some information and get it out fast before we do something that's unavoidably slow.
public ActionResult About()
{
ViewBag.Title = DateTime.Now.Second;
this.FlushHead();
Thread.Sleep(2000);
ViewBag.Message = "Your application description page.";
return View();
}
Let's think about really specifically. It's important to know WHY you would want to do this and what exactly happens in the browser.
If you have a long running, but important process (we are pretending that Thread.Sleep(2000) is important) that takes 2 seconds, no HTML is sent to the browser. It's just waiting. The timeline looks like this:
See that blue line? We waited almost 5 seconds for the /about page, and while we were waiting, no Javascript and no CSS were being loaded. Why not? How could the browser know if it isn't seen the
of your HTML?For an optimization, we could FLUSH the buffers that we have up to this point, putting the HTML that we have so far onto the network.
The Layout.cshtml we have a call to @Html.FlushHead() to get the the _Head.cshtml out and into the hands of the browser. It might look like this:
@if (ViewBag.Description != null)
{
}@ViewBag.Title - My ASP.NET Application
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
Here's what the SAME page looks like with the
flushed out first.Look closely at the timeline. Here, I'll do it for you...below shows when we flushed early versus just waiting.
See how when we flushed the head it gave the browser enough information to stat loading some css and javascript we had in the
The whole page took 5 seconds, but when we flush at least we get things going early, while when we don't flush we can't do a thing until the long running task finishes.See the difference? Now, to be clear, don't blindly go and add optimizations without reading the code and understanding what's going on, but this is a nice tool for our ASP.NET toolbox.
We may se a similar but even more powerful technical in ASP.NET vNext that includes the to do async flushes at multiple points, while updating the model as you have more data.
Sponsor: Big thanks to Aspose for sponsoring the feed this week! Working with Files? Aspose.Total for .NET has all the APIs you need to create, manipulate and convert Microsoft Office documents and many other formats in your applications. Start a free trial today.
© 2014 Scott Hanselman. All rights reserved.