views:

20

answers:

1

Hi,

My asp.net app is heavily using user controls and is high load; my concern is that loading user controls for every request (Page.LoadControl(controlPath)) is sub-optimal at best. I was thinking of caching an instance of loaded user control in HttpRuntime, but I don't know how's it going to behave when multiple connections (threads) will try to fetch it from the cache and modify properties - I guess trouble is waiting there. Any good way to optimize user control loading?

Again, I am talking about caching user control, not it's html output (which it makes no sense caching as it's [html output] going to be different every time you use it)

Thanks!

+1  A: 

Do not cache a user control, or anything that inherits from WebControl, ever.

The reason is that it contains a reference to the Page instance on which it is instantiated. By caching the control, you are effectively caching the Page and the entire control subtree with it. You're right when you say that there is trouble waiting there!

Far, far better to cache the data that your usercontrol needs, create a new usercontrol and initialize it with the data.

Some other suggestions for optimization:

  • If you're only using one or two controls per page, rather than loading them dynamically, just declare them as part of the page and set their visibility to false initially, and then to true when you need them.

  • If usercontrols are really just too slow for you, you could try rewriting it as a WebControl and set up the layout in code. I'm not sure if the extra work would be worth any optimizations that might be gained there.

I wouldn't pre-optimize anything until you know it's an issue. Are you actually running into performance problems with LoadControl()?

womp
I haven't run into problems with LoadControl yet, I just assumed that reading it from hard drive every time I need it is suboptimal to getting it from memory. But I guess I have to deal with it
Andrey