Zipping/Compressing ViewState in ASP.NET
Here's an interesting, odd, but obvious idea. If you're not able to use HttpCompression like the Blowery HttpCompression module that we use with dasBlog due to the pile of bugs in older versions of IE around compression, you can "zip" up the ViewState on fat (usually DataGrid related bloat).
This is some VB code that Vlad Olifier did, that he said I could post on my blog. It's also a new submission at GotDotnet. To use it, you just derive your ASP.NET page class from it.
Using Zipped ViewState:
Public Class CompressPage
Inherits PageViewStateZip
Just deriving from System.Web.UI.Page as usual:
Public Class RegularPage
Inherits System.Web.UI.Page
The "trick" is pretty simple. There are little-known virtuals in Page that you can override - specifically LoadPageStateFromPersistanceMedium (where that persistance medium is a hidden input box) and SavePageStateToPersistenceMedium.
When it's time to load view state, we pull it out of the form and un-base64 the string into a byte array, un-zip the bytes, then deserialize the ViewState. When it's time to save, reverse the process - Serialize, zip, store. There's some overhead, certainly. The amount of compression is usually about 50%, but your mileage may vary (YMMV).
The real decision flow is this:
- Can you use HttpCompression (via XCompress or an HttpModule)?
- If so, use it.
- Can't? (Bugs, SSL, Compatibility, bosses, don't want to take the perf hit, etc)
- Got Fat ViewState on a few pages?
- Use Zipped ViewState on a few pages as needed.
Imports System.IO
Imports Zip = ICSharpCode.SharpZipLib.Zip.Compression
Public Class PageViewStateZip : Inherits System.Web.UI.Page
Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
Dim vState As String = Me.Request.Form("__VSTATE")
Dim bytes As Byte() = System.Convert.FromBase64String(vState)
bytes = vioZip.Decompress(bytes)
Dim format As New LosFormatter
Return format.Deserialize(System.Convert.ToBase64String(bytes))
End Function
Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
Dim format As New LosFormatter
Dim writer As New StringWriter
format.Serialize(writer, viewState)
Dim viewStateStr As String = writer.ToString()
Dim bytes As Byte() = System.Convert.FromBase64String(viewStateStr)
bytes = vioZip.Compress(bytes)
Dim vStateStr As String = System.Convert.ToBase64String(bytes)
RegisterHiddenField("__VSTATE", vStateStr)
End Sub
End Class
Note that this sample uses SharpZipLib from ICSharpCode, but I assume you could use others, and I'd probably use System.IO.Compression if I was using .NET 2.0. The buffer sizes are hard-coded, but the only one that really matters it the one in Compress(). Again, salt to taste.
Imports System.IO
Imports Zip = ICSharpCode.SharpZipLib.Zip.Compression
'//--Download ICSharpCode.SharpZipLib from
'//--http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
Public Class vioZip
Shared Function Compress(ByVal bytes() As Byte) As Byte()
Dim memory As New MemoryStream
Dim stream = New Zip.Streams.DeflaterOutputStream(memory, _
New Zip.Deflater(Zip.Deflater.BEST_COMPRESSION), 131072)
stream.Write(bytes, 0, bytes.Length)
stream.Close()
Return memory.ToArray()
End Function
Shared Function Decompress(ByVal bytes() As Byte) As Byte()
Dim stream = New Zip.Streams.InflaterInputStream(New MemoryStream(bytes))
Dim memory As New MemoryStream
Dim writeData(4096) As Byte
Dim size As Integer
While True
size = stream.Read(writeData, 0, writeData.Length)
If size > 0 Then memory.Write(writeData, 0, size) Else Exit While
End While
stream.Close()
Return memory.ToArray()
End Function
End Class
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
See http://philiprieck.com/blog/archive/2004/06/17/ViewRestate.aspx
http://www.eggheadcafe.com/articles/20040613.asp
Compressed transmission is the only alternative in that case.
-Mohan
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Compression;
using System.IO;
namespace YourNamespace.Web.Compression
{
public class ViewStateCompressor
{
static public byte[] Compress(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
// Use the newly created memory stream for the compressed data.
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress);
compressedzipStream.Write(buffer, 0, buffer.Length);
compressedzipStream.Close();
return ms.ToArray();
}
static public byte[] Inflate(byte[] buffer)
{
MemoryStream ms = new MemoryStream();
ms.Write(buffer, 0, buffer.Length);
ms.Seek(0, SeekOrigin.Begin);
GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress);
List<byte> inflated = new List<byte>();
while (true)
{
int b = zipStream.ReadByte();
if (b == -1)
break;
inflated.Add((byte)b);
}
return inflated.ToArray();
}
}
}
protected override object LoadPageStateFromPersistenceMedium()
{
string vState = Request.Form["__VSTATE"];
byte[] bytes = System.Convert.FromBase64String(vState);
bytes = ViewStateCompressor.Inflate(bytes);
LosFormatter format = new LosFormatter();
return format.Deserialize(System.Convert.ToBase64String(bytes));
}
protected override void SavePageStateToPersistenceMedium(object state)
{
LosFormatter format = new LosFormatter();
StringWriter writer = new StringWriter();
format.Serialize(writer, state);
string viewStateStr = writer.ToString();
byte[] bytes = System.Convert.FromBase64String(viewStateStr);
bytes = ViewStateCompressor.Compress(bytes);
string vStateStr = System.Convert.ToBase64String(bytes);
ClientScript.RegisterHiddenField("__VSTATE", vStateStr);
}
Comments are closed.
Store state:
base64_encode(gzdeflate(serialize($this->_state)))
Read state:
unserialize(gzinflate(base64_decode($postedState)))