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

原创 2004年06月15日 15:41:00

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.

Git学习笔记(2)

Git学习笔记(2)9.远程仓库 你可能会想,如果我的一切东西都存储在自己的电脑上,要是我换了一个电脑,想继续工作;如果电脑硬盘崩掉了,那岂不是我所有的努力都白费了吗?不会的,git当然不会那么局限的...
  • IT_DREAM_ER
  • IT_DREAM_ER
  • 2015年07月24日 10:29
  • 618

git 详细教程,从头开始,看简单英文比中文好懂

Beginner   PLEASE NOTE, Section I and II below, are not to be followed in order, they are 2 se...
  • hepeng597
  • hepeng597
  • 2014年07月11日 14:53
  • 10702

页面ViewState体积庞大影响性能解决方案

我们都知道asp.net 服务器控件状态都是通过ViewState对象存储的,实际上是存储在客户端页面上的隐藏域中,当数据量过大时频繁和服务器传输,严重影响性能。直接想到的办法就是把这些数据储存到别的...
  • taoerchun
  • taoerchun
  • 2015年05月07日 17:15
  • 1100

ViewState的使用与配置

 ViewState就是页面中的名字叫做__VIEWSTATE的隐藏控件,它使我们页面体积膨胀,不利于页面的seo优化;页面返送间都要携带更多的大量的数据,耗费更多的流量、延长响应时间。 ...
  • yygzs2012
  • yygzs2012
  • 2017年04月06日 16:03
  • 379

纠正对viewstate的误解

前言        最近在招聘面试的过程中,我经常会问应聘者一些基础的问题,例如:“谈谈你对viewstate的理解”。发现很少有人能回答出来。         可能很多人会笑话,这真是个老掉牙的而且...
  • dinglang_2009
  • dinglang_2009
  • 2014年03月05日 22:42
  • 2272

手游性能优化之深入理解Texture Compression

http://gad.qq.com/article/detail/7154875 一、引子   手游项目开发日常里,经常有美术同学搞不清Photoshop制图软件与Unity3D游戏引...
  • OnafioO
  • OnafioO
  • 2016年04月15日 17:21
  • 3527

使用PHP的CURL模拟POST采集开了viewstate的asp.net网页数据

/**     QQ群:223494678     函数:模拟post得到所有分页的页面信息     参数:         string $EVENTARGUMENT         st...
  • Allen_Tsang
  • Allen_Tsang
  • 2015年07月27日 18:35
  • 1161

sybase 备份命令

Adaptive Server Enterprise 15.7 SP100 > Reference Manuals: Commands 15.7 ESD #2 > Commands   ...
  • duanbeibei
  • duanbeibei
  • 2016年05月28日 15:10
  • 1220

Ted Mosby - 一个MVP框架的软件架构

Ted Mosby - 软件架构 作者:Hannes Dorfmann原文链接 : [http://hannesdorfmann.com/android/mosby/]  (http://ha...
  • miao309410364
  • miao309410364
  • 2015年11月11日 19:19
  • 657

IDF实验室:包罗万象--图片里的英语

地址: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=34 题目: 一恒河沙中有三千世界,一张图里也可以有很多东西。 不多说了...
  • Shinukami
  • Shinukami
  • 2015年05月25日 21:40
  • 4560
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ViewState compression using BZip2...latest prototype...
举报原因:
原因补充:

(最多只允许输入30个字)