Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Does performance degrade if I use a large ViewState names?

I personally like option one below for maintainability but I could see option two getting me better performance. Option three is probably complete garbage.

1. ViewState["Calendar1.SelectionMode"] = Calendar1.SelectionMode;
2. ViewState["CSM"] = Calendar1.SelectionMode;
3. ViewState["Calendar1_SelectionMode"] = Calendar1.SelectionMode;

Am I applying old school habits of thinking about the maintenance? Does it matter only when the number of objects is large? I cannot see the internals using anything but a very efficient hash. I have read up on methods to speed up the page loading but nothing directly advising as this being even a slight factor. All the literature talks about is prefering the viewstate over database access reads, using compact types, populating only those values that take on non default values.

like image 320
ojblass Avatar asked Dec 17 '22 08:12

ojblass


2 Answers

Option two will probably give you better performance, but the difference is extremely small. If you're experiencing performance problems this would be one of the very last places I would look for bottlenecks. Have you run any kind of profiling on your page? That's where I would start looking.

like image 196
pbz Avatar answered May 20 '23 13:05

pbz


The key names become part of the viewstate hidden field. Crude example:

protected void Page_Load(object sender, EventArgs e)
{
    // ViewState["a"] = 1;
    // <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"   
    // value="/wEPDwUJNzgzNDMwNTMzDxYCHgFhAgFkZCdtAzza2+uuoGpYdGLBUdCkUGe7" />

    // ViewState["this is a very very very very long key"] = 1;
    // <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
    // value="/wEPDwUJNzgzNDMwNTMzDxYCHiZ0aGlzIGlzIGEgdmVyeSB2ZXJ5IHZlcnkgdmVyeSBsb25nIGtleQIBZGSmj9cou408+XXRLxCLKcEoLngriA==" />

}

Bottom line: unless you are storing a large number of keys, likely not an issue.

like image 24
andleer Avatar answered May 20 '23 12:05

andleer