Oct 3, 2008

Very elegant way to cut few more bytes from your ASP.NET markup and optimize your bandwidth a bit

I was thinking about how can I optimize the markup of a page a bit.
I found a great way (great but not very very very efficient) to do so.
It is based on a strategy that came in my mind recently.

Let's think a bit.
We already have very interesting methods to cut bandwith of an ASP.NET page like for example compressing the view state and so on and so on.
So provided we already have applied those techniques, if we still want to optimize what should we do?

I call my method : iterative profit, you will soon understand why ...

If we have something that repeats few times in the markup and is relatively long as name, if we make it shorter we should win some bytes right?
And if the page is big enough and "the thing" is repeated more times, we
will win more and more bytes.
So each time we cut the name of something, we win few bytes.

The next question is what can we cut in order to optimize our page?
What is the thing that repeats in each web control?

The answer is the "__doPostBack" function. I tried to replace it during the Rendering of the page and was able to cut a bit of the page size, while keeping the source relatively clean of errors (please note the word "relatively").

Here is a sample code:

     protected override void Render(HtmlTextWriter writer)
     {
        System.IO.
StringWriter stringWriter = new System.IO.StringWriter();
        
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        
base.Render(htmlWriter);
        
string html = stringWriter.ToString();
        html = html.Replace(
"\"javascript:__doPostBack", "\"__dpb");
        writer.Write(html);
     }

I had some issues I haven't yet identified, as I was in a rush to post this as it seems to me a brand new way to optimize pages - to look for repeating elements and cut their names... (I will be happy if there are volunteers to help with this one ;)...


Other things : DoPostBackWithOptions should also be replaced by dpbw for example, this will be done in the future, after I clean the _doPostBack as it still have few problems.


And some more things - we may consider optimizing some of the ASP.NET controls which are known to be "markup unoptimized" like the treeview, gridview for example (At least I think there may be some things in them to optimize).

Any comments are welcome.

4 comments:

Veselin Kolev said...

Hi, Pavel,

Actually optimizing the page is a very interesting topic.
In ASP.NET there is sometimes a big problem with the size of the markup. But what is the biggest part of any page? In most cases - the view state. How can we try to solve this? Or better - to optimize this?

Two strategies - try to avoid using view state where not necessary. The second one - try compressing and decompressing the view state. Of course the second technicue needs additional CPU time on the server side, but sometimes this is a better approach and the drowbacks are not a problem.

All the best,
Vesko Kolev

Павелъ Дончевъ said...

You are right as always, but consider the following text:

"We already have very interesting methods to cut bandwith of an ASP.NET page like for example compressing the view state and so on and so on.
So provided we already have applied those techniques, if we still want to optimize what should we do?"

My topic was more about "final touches" you may apply to your page after you already have applied the "more serious" solutions and you need to cut few more bytes.

Anyway, I have examined few of the .NET controls and I think they can be optimized a bit also.

Veselin Kolev said...

:-) That's my mistake. Nevertheless I think that optimizing this part is good enough. Few bytes more or less cannot solve the problem in general.

Actually what is the general benefit of using this technique? Can you give some byte metrics for before and after this optimization?

Thanks again!

Павелъ Дончевъ said...

I am doing just this - comparing before and after. I think I will be ready tomorrow or the day after tomorrow (depends on how much will I sleep tonight ;). And I will post some results.

As you said there isn't a big impact, but the good thing is that the more controls you add, the more benefit you get.

If you have one control you may save only 3-4 characters, but if you have a big page you will earn more.

I am working on some more examples (I even messed up with JSON, no matter how strange it may sond :)/ I will post as soon as I get some real results.


Happy that you are interested.
Cheers!