views:

241

answers:

2

Dear all,

After experiencing some performances issues on my client side, we decided to give a try to some of the performance profilers to try to find the bottleneck or identify the guilty parts of the code. Of course, as many performance investigations, the problems comes from various things, but something I find out is that the ComponentResourceManager.ApplyResources of my user controls takes way too much time in the construction of my forms: more than 24% of the construction time is spent in the ApplyResources inside the InitializeComponent(). This seems rather a lot for only "finding a resource string and putting it in it's container".

What is exactly done in the ComponentResourceManager.ApplyResources ? I guess more than searching the string, if not it wouldn't take that long.

Is there a way to enhance the performances of the localization? Our software is localized in several languages, so we do need to keep this multi-lingual feature.

Any recommendations regarding this issue?

Thanks!

PS: We are coding in C#, .NET 3.5 SP1.

+1  A: 

I would grab Reflector and take a look at the ApplyResources method to see what it actually does.

I would also recommend profiling using JetBrains dotTrace 4 (currently in EAP but trials can be downloaded), as it can also show times spent inside system classes. This makes it much more transparent where the time is actually spent. For instance, you can find out whether the time is spent looking up keys in a dictionary, accessing files, etc.

You could also do a micro benchmark and measure the time it takes to look up X keys in a Y-sized dictionary of strings, with X being the number of localized resources on a particular form and Y being the total resource pool. It will at least give you an idea of how fast you could look up the resources if you were to cache them in a dictionary, which may help you decide whether it is worthwhile to write your own resource provider.

Morten Mertner
Well, looking at the reflector didnt really help: basically it creates a hashtable and look for the key. once found it creates a property and assigns the value inside. nothing that could take that long.We used ANTS performance profiler, basically the 'ApplyRessources' trigers changes of the items size (guess to make the text and such fill the controls correctly).I reckon that wrapping the resource pool in a resource manager could help, but it would not update the components, which is actually what is done in the background by the ApplyResources. Feels weird that it's so slow...
Srodriguez
A: 

The ApplyResources method uses reflection to find the properties which will be updated with the resource values:

property = value.GetType().GetProperty(name, bindingAttr);

Reflection is notoriously slow. Assign the resource values by hand to the properties (e.g using ResourceManager.GetString(...)). This is tedious to code, but should improve the performance.

Jozef Kosik