tags:

views:

270

answers:

3

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.

+7  A: 

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.

pbz
It runs slower than I like but nothing official. It is strange but I think I can actually notice the difference when only a thousand strings are stuffed in there with longer names. My home PC is a pile though. What profiling tool would you recommend?
ojblass
You can try turning the page tracing on (http://quickstarts.asp.net/quickstartv20/aspnet/doc/monitoring/pagetrace.aspx) and see if that gives you any clues. As far as actual profilers there's the CLR profiler or some of the commercial ones like Ants or DotTrace.
pbz
Thank you for the link to profiling information. I need to apply more rigor to my analysis but my conclusion is that it does actually affect the loading characteristics.
ojblass
+4  A: 

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.

Andrew Robinson
But it does increase traffic yes?
ojblass
Yes is increases the size of your page. Your traffic will increase.
Andrew Robinson
large is a bit of an arbitrary word. Are 120 and 270 considered large?
ojblass
I would argue that any time your viewstate size > 1k you are likely abusing it. Some would set that bar even lower. If you have a high volume site, that could be an issue.
Andrew Robinson
A: 

Using performance metrics the answer is actually yes the names of the keys you put into the ViewState does impact the loading characteristics. The impact is not severe and does not appear to grow liinearly.

The critical point seemed to be right at 120 objects. The next statistical difference appeared at about 270 objects. Please note I only varied the size of the keys and kept shoving in Calendar.SelectionMode objects. I am selecting the answer that helped me reach my conclusion above.

Benchmark Final:

The effect is PAGELOAD TIME USING BIG KEYS - PAGELOAD TIME USING SMALL KEYS

  • 0000 - 0120 0.007 seconds
  • 0120 - 0270 2.254 seconds
  • 0270 - 1050 2.956 seconds
  • 1050 - 2000 4.873 seconds

The results are accurate to 0.05 seconds with 99% confidence interval.

ojblass