How costly is .NET reflection?
Asked Answered
R

13

232

I constantly hear how bad reflection is to use. While I generally avoid reflection and rarely find situations where it is impossible to solve my problem without it, I was wondering...

For those who have used reflection in applications, have you measured performance hits and, is it really so bad?

Ranson answered 24/8, 2008 at 23:40 Comment(4)
You might also want to check out this question. stackoverflow.com/questions/224232/…Lilt
Use the api at fasterflect.codeplex.com. It will speed up reflection by like 500x for getters/setters/invokers and some other stuff. Source and info on how it works is there too if you need to extend it.Jehius
How does this info check out in 2014? Anything changed in these 4 years?Distressed
The simple task of assigning a value to an instance property is roughly 150 times slower doing it with reflection (PropertyInfo.SetValue(instance, value)) than with straightforward coding (instance.property = value) This is in .NET 4.0Sterrett
G
146

It is. But that depends on what you're trying to do.

I use reflection to dynamically load assemblies (plugins) and its performance "penalty" is not a problem, since the operation is something I do during startup of the application.

However, if you're reflecting inside a series of nested loops with reflection calls on each, I'd say you should revisit your code :)

For "a couple of time" operations, reflection is perfectly acceptable and you won't notice any delay or problem with it. It's a very powerful mechanism and it is even used by .NET, so I don't see why you shouldn't give it a try.

Goodwill answered 24/8, 2008 at 23:48 Comment(4)
I've been using reflection to get method, class name of current method to log the error in try-catch. basically to avoid hardcoding the function name while logging error. Do i need to worry?Neptune
@Sangram nope, that is fineSpoilage
@Sangram no, unless you're having a lot of errors that required constantly catching, which then should be a different problem :)Goodwill
@Sangram while reflection performance shouldn't be the issue in your case, it does seem like you're trying to re-implement what plain old exceptions provide in a much more elegant way out of the box...Dilapidation
R
170

In his talk The Performance of Everyday Things, Jeff Richter shows that calling a method by reflection is about 1000 times slower than calling it normally.

Jeff's tip: if you need to call the method multiple times, use reflection once to find it, then assign it to a delegate, and then call the delegate.

Rosas answered 27/8, 2008 at 5:42 Comment(1)
I have attended Devscovery too, and concur with these results for .NET 3.5. Recompiling the Devscovery performance benchmark program for .NET 4 shows massive improvement! The cost drops down to 100 times slower. Using reflection for typeof() lookups are unchanged between .NET 3.5 and .NET 4.Spittoon
G
146

It is. But that depends on what you're trying to do.

I use reflection to dynamically load assemblies (plugins) and its performance "penalty" is not a problem, since the operation is something I do during startup of the application.

However, if you're reflecting inside a series of nested loops with reflection calls on each, I'd say you should revisit your code :)

For "a couple of time" operations, reflection is perfectly acceptable and you won't notice any delay or problem with it. It's a very powerful mechanism and it is even used by .NET, so I don't see why you shouldn't give it a try.

Goodwill answered 24/8, 2008 at 23:48 Comment(4)
I've been using reflection to get method, class name of current method to log the error in try-catch. basically to avoid hardcoding the function name while logging error. Do i need to worry?Neptune
@Sangram nope, that is fineSpoilage
@Sangram no, unless you're having a lot of errors that required constantly catching, which then should be a different problem :)Goodwill
@Sangram while reflection performance shouldn't be the issue in your case, it does seem like you're trying to re-implement what plain old exceptions provide in a much more elegant way out of the box...Dilapidation
G
65

Reflection performance will depend on the implementation (repetitive calls should be cached eg: entity.GetType().GetProperty("PropName")). Since most of the reflection I see on a day to day basis is used to populate entities from data readers or other repository type structures I decided to benchmark performance specifically on reflection when it is used to get or set an objects properties.

I devised a test which I think is fair since it caches all the repeating calls and only times the actual SetValue or GetValue call. All the source code for the performance test is in bitbucket at: https://bitbucket.org/grenade/accessortest. Scrutiny is welcome and encouraged.

The conclusion I have come to is that it isn't practical and doesn't provide noticeable performance improvements to remove reflection in a data access layer that is returning less than 100,000 rows at a time when the reflection implementation is done well.

Graph of time (y) against number of entities populated(x)

The graph above demonstrates the output of my little benchmark and shows that mechanisms that outperform reflection, only do so noticeably after the 100,000 cycles mark. Most DALs only return several hundred or perhaps thousands of rows at a time and at these levels reflection performs just fine.

Gove answered 14/12, 2010 at 15:20 Comment(5)
Not necessarily. Your DAL conversions may only be on a few thousand items, but multiply that by concurrent users using your application (if it's web) and it may add up just as if you'd convert million items. If particular method is 100-times slower it will be that much slower on small and big sets. Slower is slower.Paunch
@RobertKoritnik That's assuming that the web methods on your server are not AsynchronousOakes
@kurren asynchronicity doesn't impact reflection but rather server resources. Asynchronous Web methods will of course be able to serve more users, but reflection will still be slow. And reflection by itself AFAIK is a synchronous process anyway. Data fetching on the other hand will be the only part that will play nicely with asynchronous design.Paunch
What is the Hyper method on the chart? How's it different from Reflector?Kurys
I should have referenced this @LoneCoder: codeproject.com/Articles/18450/… by stackoverflow.com/users/23354/marc-gravellGove
D
13

Not massively. I've never had an issue with it in desktop development unless, as Martin states, you're using it in a silly location. I've heard a lot of people have utterly irrational fears about its performance in desktop development.

In the Compact Framework (which I'm usually in) though, it's pretty much anathema and should be avoided like the plague in most cases. I can still get away with using it infrequently, but I have to be really careful with its application which is way less fun. :(

Dagmardagna answered 1/9, 2008 at 10:4 Comment(2)
+1 for teaching me a new word: anathema. Also for mention of irrational fears. I fear programmers who fear irrationally - it shows that they don't really know what they're doing and just basing what they do on what other people tell them. cough cargo cult coughSend
Ahhhh Cargo Cult. Now there is a fine example of curious human behaviour.Dagmardagna
L
13

If you're not in a loop, don't worry about it.

Lixiviate answered 2/5, 2009 at 1:9 Comment(0)
C
12

My most pertinent experience was writing code to compare any two data entities of the same type in a large object model property-wise. Got it working, tried it, ran like a dog, obviously.

I was despondent, then overnight realised that wihout changing the logic, I could use the same algorithm to auto-generate methods for doing the comparison but statically accessing the properties. It took no time at all to adapt the code for this purpose and I had the ability to do deep property-wise comparison of entities with static code that could be updated at the click of a button whenever the object model changed.

My point being: In conversations with colleagues since I have several times pointed out that their use of reflection could be to autogenerate code to compile rather than perform runtime operations and this is often worth considering.

Clomp answered 31/8, 2008 at 12:18 Comment(1)
Considering that Visual Studio has such an excellent template support, it is a practical way to use code generationUndertrump
K
9

It's bad enough that you have to be worried even about reflection done internally by the .NET libraries for performance-critical code.

The following example is obsolete - true at the time (2008), but long ago fixed in more recent CLR versions. Reflection in general is still a somewhat costly thing, though!

Case in point: You should never use a member declared as "Object" in a lock (C#) / SyncLock (VB.NET) statement in high-performance code. Why? Because the CLR can't lock on a value type, which means that it has to do a run-time reflection type check to see whether or not your Object is actually a value type instead of a reference type.

Kidnap answered 31/8, 2008 at 2:34 Comment(7)
to be fair, a reflection type check is fast.Paperback
For such 'performance critical code' should you really be using .NET to begin with?Conch
@Seph: Dynamic/reflection portions of .NET, no. But usual C#/.NET, why not? C++ vs C# speedups are marginal at application layer (C++ is still a few % faster on intensive math routines). And I'm guessing you're not suggesting assembly ...Religious
A boxed value type (ie. object) can be locked on. @BryceWagner is correct.Petrel
"The CLR can't lock on a value type" actually means that if you tried e.g. lock(53) then the int would be boxed and the object produced locked on, which would be different to the object locked on by another call, and hence not really lock anything. Doing private lockObj = 53 and then lock(lockObj) would work perfectly well. The "run-time reflection type check` bit of this answer is just plain nonsense.Mahatma
To be fair (to me), it is more accurate to say that the answer is "obsolete", rather than "plain nonsense". My remarks about the behavior of lock(obj) WERE accurate at the time they were written, but that implementation-specific behavior of the CLR is long gone.Kidnap
I strongly disagree with this answer.Mozambique
J
5

As with all things in programming you have to balance performance cost with with any benefit gained. Reflection is an invaluable tool when used with care. I created a O/R mapping library in C# which used reflection to do the bindings. This worked fantastically well. Most of the reflection code was only executed once, so any performance hit was quite small, but the benefits were great. If I were writing a new fandangled sorting algorithm, I would probably not use reflection, since it would probably scale poorly.

I appreciate that I haven't exactly answered your question here. My point is that it doesn't really matter. Use reflection where appropriate. It's just another language feature that you need to learn how and when to use.

Junto answered 25/8, 2008 at 0:4 Comment(0)
N
4

Reflection can have noticeable impact on performance if you use it for frequent object creation. I've developed application based on Composite UI Application Block which is relying on reflection heavily. There was a noticeable performance degradation related with objects creation via reflection.

However in most cases there are no problems with reflection usage. If your only need is to inspect some assembly I would recommend Mono.Cecil which is very lightweight and fast

Nedry answered 25/8, 2008 at 0:1 Comment(0)
T
3

As with everything, it's all about assessing the situation. In DotNetNuke there's a fairly core component called FillObject that uses reflection to populate objects from datarows.

This is a fairly common scenario and there's an article on MSDN, Using Reflection to Bind Business Objects to ASP.NET Form Controls that covers the performance issues.

Performance aside, one thing I don't like about using reflection in that particular scenario is that it tends to reduce the ability to understand the code at a quick glance which for me doesn't seem worth the effort when you consider you also lose compile time safety as opposed to strongly typed datasets or something like LINQ to SQL.

Travelled answered 24/8, 2008 at 23:57 Comment(0)
T
3

Reflection is costly because of the many checks the runtime must make whenever you make a request for a method that matches a list of parameters. Somewhere deep inside, code exists that loops over all methods for a type, verifies its visibility, checks the return type and also checks the type of each and every parameter. All of this stuff costs time.

When you execute that method internally theres some code that does stuff like checking you passed a compatible list of parameters before executing the actual target method.

If possible it is always recommended that one caches the method handle if one is going to continually reuse it in the future. Like all good programming tips, it often makes sense to avoid repeating oneself. In this case it would be wasteful to continually lookup the method with certain parameters and then execute it each and everytime.

Poke around the source and take a look at whats being done.

Trolley answered 25/5, 2009 at 5:41 Comment(0)
C
2

Reflection does not drastically slow the performance of your app. You may be able to do certain things quicker by not using reflection, but if Reflection is the easiest way to achieve some functionality, then use it. You can always refactor you code away from Reflection if it becomes a perf problem.

Collincolline answered 15/10, 2008 at 22:3 Comment(0)
A
1

I think you will find that the answer is, it depends. It's not a big deal if you want to put it in your task-list application. It is a big deal if you want to put it in Facebook's persistence library.

Anelace answered 2/5, 2009 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.