ViewState compression using BZip2...latest prototype...

类别:.NET开发 点击:0 评论:0 推荐:

Just a first stab at this, uses some classes I used in another project (I'll attribute the compression code later...I believe I got it from the Sharpziplib stuff - with some modifications - I'll stick the comment headers back in to comply with the Licensing stuff later - right now consider it an example only) the actual code to compress the viewstate is VERY simple:

using System;
using System.Web.UI;
using System.IO;
namespace ViewStateCompression
{
    /// 
    /// Summary description for CompressedVSBasePage.
    /// 
    public class CompressedVSBasePage :System.Web.UI.Page
    {
        private LosFormatter  _formatter = new LosFormatter();
        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            StringWriter sw = new StringWriter();
            _formatter.Serialize(sw, viewState);
            string outStr = Compression.Compress(sw.ToString());
            Page.RegisterHiddenField("__COMPRESSEDVIEWSTATE",outStr);
        }
        protected override object LoadPageStateFromPersistenceMedium()
        {
            string vsString = Request.Form["__COMPRESSEDVIEWSTATE"];
            string outStr = Compresssion.DeCompress(vsString);
            return _formatter.Deserialize(outStr);
        }
    }
}

To use it, just inherit from this page instead of the normal System.Web.UI.Page. Obviously you lose some of the normal Viewstate functions such as encryption - but these should be easy to slot back in...I am currently seeing pretty large savings in ViewState size from using this - using BZip2 compression - as it's a piece of code I had lying about..., please try it out. Any comments / suggestions are, as always, appreciated.

UPDATE (27/05/2004):  I've updated the demo project, you can now download this from here. Main changes are that it now uses my Compression helper object - this uses the standard SharpZipLib, is a bit more efficient and lets you switch between all the SharpZipLib compression types very easily. My experience has shown that this method is best when you're using objects like DataSets which under.1.1 serialize pretty poorly (as in DataSets really serialize to a form of XML - which tends to compress really well). In addition, do not turn on encryption when doing this - the compression is REALLY poor then. It's really simple to add encryption back in again - just do it to the string you get from the Stringwriter after serialization - I'll add an example later (using Rijndael is also gonna be more secure and much faster than 3DES).  Also, someone called 'Mark' left a comment with some great performance figures, here's the comment:

Played around with this a little. For smaller view states (did a short comparison with a view state of 100200 bytes) it's faster on a normal network not to use GZip or BZip2. Roughly: The original file size was 224 Kb. GZip got the file size down to 197 Kb but it took about 0.5 seconds longer, BZip2 got the file size down to 190 Kb, but it took about 1.3 seconds longer. With a larger view state (used a view state of 1115624 bytes) the original file size was 893 Kb. GZip got the file size down to 501 Kb and was actually almost 0.4 seconds faster. The BZip2 got the file size down to 434 Kb, but it was still about 1 second slower than using no compression.

These figures really don't mean much, since I didn't take many samples. However, for me it confirmed that BZip2 might only really make it go faster when the view state is very very big. BZip2 provides very nice compression, but it's slower. GZip doesn't provide as high as a compression rate as BZip2, but it's faster. Below a certain threshold (which is hard to determine since network speed influences this), compression really just slows it down. I am looking at using a fast compression algorithm like LZO. Hope this helps anyone interested in this.

本文地址:http://com.8s8s.com/it/it44668.htm