KevinUp

real world programming

optimizing viewstate

Posted by kevinup on May 24, 2007

I’ve always heard of optimizing viewstate, like you hear about an urban legend. I never understood what you would optimize or how to go about doing it. Let me be clear. I have made pages such that certain controls had their enable viewstate property disabled. I’ve moved things around such that the view state is smaller, but I’ve never really looked at manipulating view state directly. I think it would be really cool if you could do this with using as an httpModule mas, c’est la vie. There are two methods which are important:

protected override void SavePageStateToPersistenceMedium(object state)
protected override object LoadPageStateFromPersistenceMedium()

These methods do pretty much what you think, drop them on your page, and then you can manipulate the view state. Viewstate comes in as an object on the save. Then you can do anything crazy you feel like doing. Like maybe we could gzip it. Here’s a quicky little gzip class.

 

public static class Compressor
{
    public static byte[] Compress(byte[] data)
    {
        MemoryStream output = new MemoryStream();
        GZipStream gzip = new GZipStream(output,
                          CompressionMode.Compress, true);
        gzip.Write(data, 0, data.Length);
        gzip.Close();
        return output.ToArray();
    }
    public static byte[] Decompress(byte[] data)
    {
        MemoryStream input = new MemoryStream();
        input.Write(data, 0, data.Length);
        input.Position = 0;
        GZipStream gzip = new GZipStream(input,
                          CompressionMode.Decompress, true);
        MemoryStream output = new MemoryStream();
        byte[] buff = new byte[64];
        int read = -1;
        read = gzip.Read(buff, 0, buff.Length);
        while (read > 0)
        {
            output.Write(buff, 0, read);
            read = gzip.Read(buff, 0, buff.Length);
        }
        gzip.Close();
        return output.ToArray();
    }
}

Now lets put it all together:

protected override void SavePageStateToPersistenceMedium(object state)
{
    LosFormatter formatter = new LosFormatter();
    StringWriter writer = new StringWriter();
    formatter.Serialize(writer, state);
    string viewStateString = writer.ToString();
    byte[] bytes = Convert.FromBase64String(viewStateString);
    bytes = Compressor.Compress(bytes);
    string base64string = Convert.ToBase64String(bytes);
    ClientScript.RegisterHiddenField("__VSTATE", base64string);
}

 

protected override object LoadPageStateFromPersistenceMedium()
{
    string viewState = Request.Form["__VSTATE"];
    byte[] bytes = Convert.FromBase64String(viewState);
    bytes = Compressor.Decompress(bytes);
    LosFormatter formatter = new LosFormatter();
    return formatter.Deserialize(Convert.ToBase64String(bytes));
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: