0

I have seen this and many similar posts and blogs across the net saying reflection performance penalty isn't noticeable. I also have seen posts like this one claiming (rightfully in my mind) that reflection is extra code running impacting performance noticeably. Let's imagine for a moment that you can write code that is cleaner using reflection or a bit longer and more complex code without it. Which one would you choose?

This is the question in my mind. As a particular example I'm talking about O/RM, MVVM helper and IoC containers that use reflection.

Should I use Prism for example? That makes the code much cleaner but uses A LOT of reflection or not?

And last but not least is the performance of reflection any different in Xamarin and mobile platforms in contrast with desktop and native Windows platforms?

Emad
  • 3,809
  • 3
  • 32
  • 44
  • 1
    Reflection penalty is noticeable if you are running a particularly intense app on xamarin. For regular apps, mostly consulting apps that get data from a Database and display it (CRUD) and etc... It makes absolutely no difference. So i'd say it depends on what you are doing in your app. Also i'd recommend caliburn.micro as it is slightly easier than prism to use – Andre Motta Aug 13 '18 at 14:14
  • 1
    I try to stay away from questions/answers that are subjective, but..... ;-) While only you can determine if your usage of reflection is a code maintenance help vs. too large of a performance impact for your app, I can say that I have written entire Xamarin-based apps that did not use reflection (and if the internal framework was using it, I avoided and|or rewrote that feature) and *yes* they are user noticeably *faster* (single-core performance of a mobile ARM processor is a limiting factor here), not a maintenance problem at all (I tend to use Viper, but same holds true for MVVM), etc... – SushiHangover Aug 13 '18 at 15:51
  • @SushiHangover Thank you. I tend to agree with you more. I'm already implementing a sufficiently large application both ways comparing both approaches from code and performance point of views. – Emad Aug 13 '18 at 16:33

1 Answers1

4

Frameworks like prism and other mvvm frameworks do take care of performance very well. They are designed by experts who know limitations and problems of reflection.

Reflection is not bad. It has lot of advantage over traditional way to write code.

Here is an example.

Bad Reflection

foreach(var pair in pairs) {
    PropertyInfo sourceProp = sourceType.GetProperty("Value");
    PropertyInfo destProp = destType.GetProperty("Value");
    destProp.SetValue(pair.Dest, sourceProp.GetValue(pair.Source));
}

Good Reflection...

PropertyInfo sourceProp = sourceType.GetProperty("Value");
PropertyInfo destProp = destType.GetProperty("Value");

foreach(var pair in pairs) {
    destProp.SetValue(pair.Dest, sourceProp.GetValue(pair.Source));
}

There is even a better approach but due to limitation of dynamic runtime, it is not possible to use inside xamarin.

Above example outlines the basic problem associated with reflection is people don't cache members, and searching for members in list of members inside type is always costly operation.

Without Reflection

Without reflection you will end up writing lots of code that will not give you huge performance gain. If you do not use Binding and MVVM, you will end up writing code for copying data from UI to model and model to UI, and duplicating code everywhere. It may not improve performance at all as you will have to write lots of ifs, and testing them will be another nightmare.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • Thanks for the answer but I'm still not convinced. The example you provided can work in the case of ORMs but reflection code needed to start an IoC Container or ViewModelLocators or named base navigation that we see in Xamarin Forms frameworks can't be avoided. In another option I can write code that don't use reflection at all since the code is meant only for the types I know about. The code will be messier but is it faster? – Emad Aug 13 '18 at 14:45
  • 1
    Read my last paragraph, anything you will want to write, for all types you know about, the size of code will be huge, repetitive and will not be unit testable. And it will not at all be faster, the difference will be in few milliseconds, for example, if a user will tap a button to open a new page, a page will usually take 500 ms, but with reflection it will take 600 ms, I don't think 100ms will make any noticeable difference to user at all. – Akash Kava Aug 13 '18 at 14:49