Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Reflection vs Generics - performance

I'm about to write a set of methods on a server application that take messages received from TCP socket, encode/decode them, and encrypt/decrypt them.

Considering messages are defined as special types, each with its own set of properties and total length, I need to opt for one of the following solutions: 1. Make methods that use generics, such as Encode<T>(...) where T : IMessage and then implement encoder/decoder for each type of message and have ResolveEncoder<T> that would pick encoder for wanted message or 2. Make method that uses any type of message, as long as it implements IMessage, such as Encode(IMessage message), and use System.Reflection to determine everything I need to know about it.

I'm more interested in doing solution #2 as it makes my code 30 times smaller. However, I'm worried whether constant reflection of properties will hit the performance. Now as I'm time limited to finish the project, I can't really "experiment".

I would be grateful for any personal experiences or links with benchmarks on how performance falls with either of solutions.

like image 539
Admir Tuzović Avatar asked Apr 18 '12 03:04

Admir Tuzović


2 Answers

Now as I'm time limited to finish the project, I can't really "experiment".

Then your real constraint is not performance, but rather which can you code and test and debug in the given time constraint. Supposing that your claim that the reflection version is 30x smaller, it sounds like that's what you should lean towards.

However, five points:

  1. I doubt that 30x is the right estimate, it's probably much smaller.
  2. Reflection will be less performant than using generics. There's no question about that.
  3. "However, I'm worried whether constant reflection of properties will hit the performance." You can mitigate some of this by aggressively caching the PropertyInfos and what not that you load by reflection.
  4. You can do something akin to reflection using expression trees, and you'll see a significant performance boost from doing so. Here's a blog post that will push you in the right direction on this issue. However, if you're not already familiar with working with expression trees, given your time constraint, it is a risk to take on coding, testing and debugging using a concept that you're not familiar with.
  5. Are you sure that that this is even a performance bottleneck? It wouldn't shock me if network latency dominates here, and you're prematurely optimizing.
like image 73
jason Avatar answered Sep 27 '22 22:09

jason


Reflection can be fast enough. But need to be implemented correctly.

Reflection Performance -

Fast and Light Functions

typeof
Object.GetType
typeof == Object.GetType
Type equivalence APIs (including typehandle operator overloads)
get_Module
get_MemberType
Some of the IsXX predicate APIs
New token/handle resolution APIs in the .NET Framework 2.0

Costly Functions

GetXX  APIs (MethodInfo, PropertyInfo, FieldInfo, and so on)
GetCustomAttributes
Type.InvokeMember
Invoke APIs (MethodInfo.Invoke, FieldInfo.GetValue, and so on)
get_Name (Name property)
Activator.CreateInstance

Source - Dodge Common Performance Pitfalls to Craft Speedy Applications

MethodInfo can be speed up - Improving performance reflection , what alternatives should I consider

Good links:
How costly is .NET reflection?
http://www.codeproject.com/Articles/18450/HyperDescriptor-Accelerated-dynamic-property-acces

like image 31
Aseem Gautam Avatar answered Sep 27 '22 21:09

Aseem Gautam