mardi 21 janvier 2020

.NET reflection 3 times slower in .NET 4.7.x than in 3.5

I have an application that we upgraded form .NET 3.5 to .NET 4.7.2. The only problem really is performance from part of our code that uses reflection. Entire situation is best explained in a simple sample I uploaded to Gist: https://gist.github.com/vkocjancic/3e8a6b3496c412a75b1c85a1d2ba1111

Basically, we have a POCO class, which property methods throw exceptions, if value is not set for properties of non-nullable types.

A reflection is then used to get property names and values of instances of POCO class and popluate it to DataTable.

The sample and real life project source code is exactly the same. The only difference is that in one case it is compiled using .NET 3.5 and in second using .NET 4.7.2.

This is the average time elapsed in milliseconds for 10 invocations:

.NET 3.5    -> 231.1 ms
.NET 4.7.2  -> 713.5 ms

Can anyone elaborate why reflection is about 3 times slower in .NET 4.7.2 than in .NET 3.5 and how to fix that. The lag only happens, when properties are not set and throw exceptions. If you populate property values, there is no difference in performance.

If I run sample in debugger, .NET 3.5 build never triggers MissingFieldException. Build using .NET 4.7.2 triggers every exception.





Aucun commentaire:

Enregistrer un commentaire