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

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.

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用和给出了出现"System.NullReferenceException: 未将对象引用设置到对象的实例"错误的可能原因,包括ViewState对象为Null、DateSet为空、sql语句或Datebase导致DataReader为空、声明字符串变量时未赋空值就应用变量、未用new初始化对象、Session对象为空、对控件赋文本值时值不存在、使用Request.QueryString()时所获取的对象不存在或在值为空时未赋初始值、使用FindControl时控件不存在却没有做预处理、重复定义造成未将对象引用设置到对象的实例错误。 而引用是对问题的进一步说明,指出在执行当前Web请求期间出现了未处理的异常,具体异常是"System.NullReferenceException: 未将对象引用设置到对象的实例"。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [System.NullReferenceException: 未将对象引用设置到对象的实例](https://blog.csdn.net/wedypei/article/details/2883523)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [关于“System.NullReferenceException: 未将对象引用设置到对象的实例”问题原因。](https://blog.csdn.net/weixin_30455023/article/details/96382012)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ar7_top

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值