REFLECTION OPTIMIZATION METHOD IN .NET USING DYNAMIC COMPILATION OF LAMBDA FUNCTIONS

Authors

DOI:

https://doi.org/10.31891/2307-5732-2024-339-4-62

Keywords:

metaprogramming, .NET, C#, reflection, Expression

Abstract

Reflection optimization methods in .NET are analyzed. The main method of optimization is the use of dynamic compilation of lambda functions. One approach to achieve the desired result is to use Expression compilation. Calling a compiled lambda function is faster than calling reflection, but the compilation itself takes some time. To avoid this drawback, it is necessary to store the delegate of the compiled function in memory so that it can be called again. In the course of experiments, it was determined that on versions .NET7 and higher, searching in the cache and calling such a function takes the same amount of time as calling reflection. Thus, to improve the speed of the method, it is necessary to optimize the data structure that stores the lambda function. Two-level hash-table was used to optimize search and storage of lambda functions. According to the results of the experiments, it was established that such optimization made it possible to improve the performance of the method. The new data structure also made it possible to use implementations of hash tables optimized for working with unchanged data, which allowed to further increase the performance of the method. However, this approach is only possible for use in .NET8 and higher versions. To optimize the method in cases of successive calls for the same data type, caching of information about the last data type for which the method was called was used. Experiments were conducted to determine the speed of the developed method in comparison with reflection and methods using a similar principle. The Benchmark.NET library was used to conduct experiments. According to the results of the experiments, it was established that the developed method has a better speed compared to reflection and analogues.

Published

2024-08-30

How to Cite

POZUR, M., & VOITKO, V. (2024). REFLECTION OPTIMIZATION METHOD IN .NET USING DYNAMIC COMPILATION OF LAMBDA FUNCTIONS. Herald of Khmelnytskyi National University. Technical Sciences, 339(4), 414-419. https://doi.org/10.31891/2307-5732-2024-339-4-62